COMP 110/L Lecture 20. Kyle Dewey

Similar documents
COMP 110/L Lecture 19. Kyle Dewey

Inheritance and Polymorphism

Inheritance. Transitivity

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object-Oriented Programming More Inheritance

Overriding Variables: Shadowing

OVERRIDING. 7/11/2015 Budditha Hettige 82

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

COMP 110/L Lecture 24. Kyle Dewey

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance and Polymorphism. CS180 Fall 2007

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Programming II (CS300)

PROGRAMMING LANGUAGE 2

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

ECE 122. Engineering Problem Solving with Java

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

CMPSCI 187: Programming With Data Structures. Lecture 6: The StringLog ADT David Mix Barrington 17 September 2012

Practice for Chapter 11

COMP 110/L Lecture 7. Kyle Dewey

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Java Object Oriented Design. CSC207 Fall 2014

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Object Orientated Programming Details COMP360

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

C10: Garbage Collection and Constructors

Arrays Classes & Methods, Inheritance

Subclassing, con.nued Method overriding, virtual methods, abstract classes/methods. COMP 401, Spring 2015 Lecture 9 2/19/2015

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Inheritance and object compatibility

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Inheritance & Polymorphism

Lecture 18 CSE11 Fall 2013 Inheritance

Polymorphism. Arizona State University 1

Sorting. Sorting. Selection sort

CS Programming I: Inheritance

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Chapter 14 Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

Chapter 2: Java OOP I

ECE 122. Engineering Problem Solving with Java

Derived and abstract data types. TDT4205 Lecture 15

Introduction to Object-Oriented Programming

JAVA MOCK TEST JAVA MOCK TEST II

CS 116 Week 8 Page 1

Inheritance, Polymorphism, and Interfaces

C09: Interface and Abstract Class and Method

C18a: Abstract Class and Method

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Instance Members and Static Members

C11: Garbage Collection and Constructors

Java Fundamentals (II)

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

QUESTIONS FOR AVERAGE BLOOMERS

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

type conversion polymorphism (intro only) Class class

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

CMSC 202. Generics II

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Inheritance -- Introduction

Software Development (cs2500)

Introduction to Computing II (ITI 1121) Midterm Examination

First IS-A Relationship: Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS 251 Intermediate Programming Inheritance

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Constructors for classes

After a lecture on cosmology and the structure of the solar system, William James was accosted by a little old lady.

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Exercise: Singleton 1

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

CS 403/503 Exam 4 Spring 2015 Solution

Object-Oriented Concepts

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Polymorphism: Inheritance Interfaces

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Inheritance & Polymorphism

Inheritance and Polymorphism

CS 61B Discussion 4: Inheritance Fall 2018

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Testing Object-Oriented Software. COMP 4004 Fall Notes Adapted from Dr. A. Williams

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

ITI Introduction to Computing II

Chapter 5. Inheritance

Object-Oriented ProgrammingInheritance & Polymorphism

public UndergradStudent(String n, String m, String p) { programme = p; super(n, m);

Inheritance and Polymorphism

More Relationships Between Classes

Lab 2: Object-Oriented Design 12:00 PM, Jan 31, 2018

Software Practice 1 - Inheritance and Interface Inheritance Overriding Polymorphism Abstraction Encapsulation Interfaces

Transcription:

COMP 110/L Lecture 20 Kyle Dewey

Outline super in methods abstract Classes and Methods Polymorphism

super in Methods

Recap You ve seen super in constructors...

Recap You ve seen super in constructors... public class Base { public Base(int x) {...

Recap You ve seen super in constructors... public class Base { public Base(int x) {... public class Sub extends Base { public Sub(int x) { super(x);

super in Methods super can also be used in methods when overloading. Used to execute a superclass implementation of a method.

super in Methods super can also be used in methods when overloading. Used to execute a superclass implementation of a method. public class Base { public int returnnum() { return 17;

super in Methods super can also be used in methods when overloading. Used to execute a superclass implementation of a method. public class Base { public int returnnum() { return 17; public class Sub extends Base { public int returnnum() { return super.returnnum() + 3;

super in Methods super can also be used in methods when overloading. Used to execute a superclass implementation of a method. public class Base { public int returnnum() { return 17; public class Sub extends Base { public int returnnum() { return super.returnnum() + 3; Returns 17

Example Base.java Sub.java SuperMethodMain.java

abstract Classes and Methods

-The example from last time stated that we had Mammal objects, Cat objects, and Dog objects -Cat and Dog objects were both Mammal objects because of inheritance -Having just a Mammal object (which isn t a Cat, Dog, or some other actual animal) is strange Recap - A Problem Mammal

abstract Classes Allows a class to be extended, but disallows the creation of instances of that class

abstract Classes Allows a class to be extended, but disallows the creation of instances of that class public class Mammal { public Mammal(String s) {... -Before we defined this code...

abstract Classes Allows a class to be extended, but disallows the creation of instances of that class public class Mammal { public Mammal(String s) {... new Mammal( some string ) -And we could create instances of this class directly

abstract Classes Allows a class to be extended, but disallows the creation of instances of that class public class Mammal { public Mammal(String s) {... new Mammal( some string ) public abstract class Mammal { public Mammal(String s) {... -If, however, we declare Mammal as an abstract class...

abstract Classes Allows a class to be extended, but disallows the creation of instances of that class public class Mammal { public Mammal(String s) {... new Mammal( some string ) public abstract class Mammal { public Mammal(String s) {... new Mammal( some string ) Does not compile -If, however, we declare Mammal as an abstract class...

Example AbstractBase.java AbstractSub.java AbstractMain.java

abstract Methods Methods of abstract classes can also be defined abstract To be overridden later abstract methods have no bodies

abstract Methods Methods of abstract classes can also be defined abstract To be overridden later abstract methods have no bodies public abstract class Abstract { public abstract int getvalue();

abstract Methods Methods of abstract classes can also be defined abstract To be overridden later abstract methods have no bodies public abstract class Abstract { public abstract int getvalue(); public class Sub extends Abstract { public int getvalue() { return 5;

Example ArithmeticOperation.java Add.java Subtract.java

Polymorphism

Revisit Mammal breathe -From last time: mammals breathe, so transitively cats and dogs breathe, too -Phrased another way, all mammals breathe, so if I have any mammal I can ask it to breathe

Cat cat = new Cat( Tom ); Dog dog = new Dog( Rover ); cat.breathe(); dog.breathe(); -Snippet of code from the last time: have variables which explicitly track that they point to Cat and Dog objects, and we ask them both to breathe

Cat cat = new Cat( Tom ); Dog dog = new Dog( Rover ); cat.breathe(); dog.breathe(); Tom the mammal takes a breath Rover the mammal takes a breath -The above code produced the output that each Mammal took a breath

Cat cat = new Cat( Tom ); Dog dog = new Dog( Rover ); cat.breathe(); dog.breathe(); Tom the mammal takes a breath Rover the mammal takes a breath Mammal m1 = new Cat( Tom ); Mammal m2 = new Dog( Rover ); m1.breathe(); m2.breathe(); -Alternative version: we only track that the Cat and the Dog are Mammals

Cat cat = new Cat( Tom ); Dog dog = new Dog( Rover ); cat.breathe(); dog.breathe(); Tom the mammal takes a breath Rover the mammal takes a breath Mammal m1 = new Cat( Tom ); Mammal m2 = new Dog( Rover ); m1.breathe(); m2.breathe(); Tom the mammal takes a breath Rover the mammal takes a breath -Output does not change at all. m1 knows it s really a Cat and m2 knows it s really a dog

Polymorphism many-forms A Mammal could be a Cat or a Dog Specific use in Java: a variable with a superclass type can hold an instance of any subclass, too

Polymorphism many-forms A Mammal could be a Cat or a Dog Specific use in Java: a variable with a superclass type can hold an instance of any subclass, too Mammal m1 = new Cat( Tom ); Mammal m2 = new Dog( Rover );

Polymorphism Significance Can write code without knowing exactly which implementation is used.

Polymorphism Significance Can write code without knowing exactly which implementation is used. public static void method(mammal m) { m.breathe(); -I don t need to know if m is a Dog or a Cat in order to write the above code, only that m is a Mammal so I can call the breathe() method -Key point: breathe() can do different things

Example Car.java SportsCar.java SemiTruck.java CarMain.java

Example MammalRevisited.java CatRevisited.java DogRevisited.java MammalMainRevisited.java