SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Polymorphism & Interfaces
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Lecture 12 Inheritance.
Object-oriented Programming in Java
Overriding Method.
Polymorphism, Abstract Classes & Interfaces
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Arrays and Collections
Polymorphism, Abstract Classes & Interfaces
Chapter 11 Inheritance and Polymorphism
Java Inheritance.
Advanced Inheritance Concepts
Chapter 8 Class Inheritance and Interfaces
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism

2 Review: The Java mechanism for defining a inheritance relationship between two classes is called extension: In Dog.java: public class Dog{…} In Beagle.java: public class Beagle extends Dog{…} The extends keyword establishes the inheritance relationship extends means “is a kind of” SE-1020 Dr. Mark L. Hornick

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 3 Polymorphism allows a single variable to refer to objects from different subclasses in the same inheritance hierarchy For example, if Beagle is a subclass of Dog, then the following statements are valid: Dog myPet; myPet = new Dog(“rex”,3); // myPet can also reference a subclass of Dog myPet = new Beagle(“rusty”, 1);

Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 4 Polymorphism also allows the JVM to determine (at runtime) which specific implementation of a method should be called For example, if Beagle overrides the speak() method of Dog, the JVM will use late-binding to call Beagle’s speak() method: Dog myPet; myPet = new Dog(“rex”,3); myPet.speak(); // call Dog’s speak() method // myPet can also reference a subclass of Dog myPet = new Beagle(“rusty”, 1); myPet.speak(); // call Beagle’s speak() method

Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick 5 Sometimes, a derived class may want to use the base class implementation AND provide some additional behavior of its own Within Dog class: public void speak(){ // Dog’s method System.out.println(“Woof”); } Within a Beagle class: public void speak(){ // Beagle’s method super.speak(); // invoke Dog’s speak() System.out.println(“hooowwwl”); }

SE-1020 Dr. Mark L. Hornick 6 The instanceof operator can help us learn the class of an object The following code counts the number of Beagles in a collection of Dogs int howlerCount = 0; for ( Dog d: kennel ) { // kennel is a ArrayList if ( d instanceof Beagle ) { howlerCount++; }

Polymorphism allows us to use a derived class anywhere we would normally use a base class Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 7 ArrayList kennel = new ArrayList (); kennel.add( new Dog(“rex”, 3) ); // add a Dog kennel.add( new Dog(“sam”, 2) ); // add another Dog // The add() method nomally takes a Dog reference, but… kennel.add( new Beagle( “rusty”, 1) ); // add a Beagle

SE-1020 Dr. Mark L. Hornick 8 Every Java class has a common ancestor: the Object class If a class declaration does not explicitly designate the superclass with the extends clause, then the class’s superclass is the Object class public class SomeClass { … } Is the same as: public class SomeClass extends Object { … }

SE-1020 Dr. Mark L. Hornick 9 Because of inheritance, every Java class ultimately extends Object The Object class is the source of the following methods which every Java class inherits: clone() – makes a copy of the object equals() – compares an object to another object toString() – converts an object to its String representation and several others (see the Javadoc for the Object class)

The toString() method The Object class provides a default implementation of toString(), which is why you can always call it for any class If you create classes that don’t override toString(), you’ll inherit Object’s default implementation Which just prints out the class name and hashcode (related to the object’s memory address) Many Java API classes override toString() in order to provide an implementation that makes more sense CS-1020 Dr. Mark L. Hornick 10

The equals() method The Object class provides a default implementation of equals(), which is why you can always call it for any class If you create classes that don’t override equals(), you’ll inherit Object’s default implementation Which just compares memory addresses Many Java API classes override equals() in order to provide an implementation that makes more sense CS-1020 Dr. Mark L. Hornick 11