Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
C HAPTER 7 Better Living in Objectville. O VERVIEW Understanding Inheritance Designing an Inheritance Tree Avoiding Duplicate Code Overriding Methods.
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Object oriented design in Java (useful revision for final exam)
LECTURE 07 Programming using C# Inheritance
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
What is inheritance? It is the ability to create a new class from an existing class.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance and Access Control CS 162 (Summer 2009)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
Coming up: Inheritance
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
BY:- TOPS Technologies
Coming up Which methods are where? – Overriding Calling super’s methods Coupling and cohesion.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
Object Oriented Programming
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
Abstract Classes Page
Polymorphism Polymorphism - Greek for “many forms”
Interfaces and Abstract Classes
Object Oriented Programming
Chapter 9 Carrano Chapter 10 Small Java
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Polymorphism & Methods COMP204 Bernhard Pfahringer (with lots of input from Mark Hall) poly + morphos (greek) “many forms”

Overriding Subclass redefines a method: –different behaviour –or maybe modified behaviour class Rectangle { public String toString() { return “ ”; }

Inheritance Subclasses inherit everything from superclasses, but cannot directly access private fields or methods, only public and protected ones. Can still access overridden methods: public void roam() { super.roam(); // some additional local stuff }

Inheritance: Is-A (not Has-A) B should extend A if “B is-A A” –Triangle Is-A Shape –Surgeon Is-A Medical Doctor Bathroom Has-A Tub –Tub is an instance variable of Bathroom (Composition)

Code Inheritance Use inheritance to implement shared behaviour (code) only once! Always avoid code duplication! –easier to change (only one place), especially when debugging

Polymorphism what happens when one declares and initializes a reference variable: Dog myDog = new Dog(); allocate space for reference variable of type Dog allocate space for a new Dog object on the heap (and initialize it) point the reference to the new object

Polymorphism Reference type can be any super class or Interface implemented by actual type: Animal myDog = new Dog(); As generic as possible => more flexible code: Map myMap = new HashMap ();

Polymorphism Animal[] animals = new Animal[]{new Dog(), new Lion(), new Cat(), new Wolf(), new Hippo()}; for(Animal a: animals) { a.eat(); a.roam(); }

Polymorphism Parameters/Arguments and return values can be polymorphic too: class Vet { public void giveShot(Animal a) { a.makeNoise(); } class PetOwner { public void start() { Vet v = new Vet(); Dog d = new Dog(); Hippo h = new Hippo(); v.giveShot(d); v.giveShot(h); }

stop overriding final declaration: –class: cannot extend/subclass any further final public class String {.. } –method: cannot be overridden in subclasses class Cat { final public void makeNoise() {…} –field: cannot change value final int count = animals.length;

Overriding rules Argument and return type must be the same: class Appliance { public boolean turnOn(); } class Toaster extends Appliance { public boolean turnOn(int level); // OVERLOADING }

Overriding rules May NOT be less accessible: class Appliance { public boolean turnOn(); } class Toaster extends Appliance { protected boolean turnOn(); // illegal }

Overloading Two (or more) methods with the same name but different argument lists (see code example) Usually best avoided, can be very confusing and counter-intuitive

Which method is executed Compile time: compiler ensures that method with appropriate signature (compile-time type info for arguments) exists in “compile-time” receiver class Runtime: given the actual runtime-type of the receiver, the most specific method of appropriate signature is located and executed ==> runtime types of arguments do NOT matter (again, see code examples)