Inheritance, Polymorphism, and Interfaces. Oh My

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
What is inheritance? It is the ability to create a new class from an existing class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
COMP Inheritance Basics Yi Hong June 09, 2015.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Lecture 12 Inheritance.
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
An Introduction to Inheritance
UML Class Diagram: class Rectangle
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces
Inheritance Chapter 7 Chapter 7.
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Computer Programming with JAVA
Defining Classes and Methods
Overriding Methods & Class Hierarchies
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 7 Inheritance.
Presentation transcript:

Inheritance, Polymorphism, and Interfaces. Oh My Chapter 8 Modified by JJ

Objectives Describe polymorphism and inheritance in general Define interfaces to specify methods Describe dynamic binding Define and use derived classes in Java

Inheritance Cat Example

Inheritance Basics: Outline Derived Classes Overriding Method Definitions Overriding Versus Overloading The final Modifier Private Instance Variables and Private Methods of a Base Class UML Inheritance Diagrams

Inheritance Basics Inheritance allows programmer to define a general class Later you define a more specific class Adds new details to general definition New class inherits all properties of initial, general class Not lazy we are efficient

Derived Classes Figure 8.1 A class hierarchy

Derived Classes Class Person used as a base class Also called superclass Now we declare derived class Student Also called subclass Inherits methods from the superclass General form for inheritance class Student extends Person View demo program, listing 8.3 class InheritanceDemo Sample screen output

Overriding Method Definitions Note method writeOutput in class Student Class Person also has method with that name Method in subclass with same signature overrides method from base class Overriding method is the one used for objects of the derived class Overriding method must return same type of value

Overriding Versus Overloading Do not confuse overriding with overloading Overriding takes place in subclass – new method with same signature Overloading New method in same class with different signature

The final Modifier Possible to specify that a method cannot be overridden in subclass Add modifier final to the heading public final void specialMethod() An entire class may be declared final Thus cannot be used as a base class to derive any other class

Private Instance Variables, Methods Consider private instance variable in a base class It is not inherited in subclass It can be manipulated only by public accessor, modifier methods Similarly, private methods in a superclass not inherited by subclass

UML Inheritance Diagrams Figure 8.2 A class hierarchy in UML notation

UML Inheritance Diagrams Figure 8.3 Some details of UML class hierarchy from figure 8.2

Constructors in Derived Classes A derived class does not inherit constructors from base class Constructor in a subclass must invoke constructor from base class Use the reserve word super Must be first action in the constructor

The this Method – Again Also possible to use the this keyword Use to call any constructor in the class When used in a constructor, this calls constructor in same class Contrast use of super which invokes constructor of base class

Calling an Overridden Method Reserved word super can also be used to call method in overridden method Calls method by same name in base class

Programming Example Person Student A derived class of a derived class View sample class, listing 8.4 class Undergraduate Has all public members of both Person Student This reuses the code in superclasses

Programming Example Figure 8.4 More details of the UML class hierarchy

Example Person Student Undergrad Undergrad Demo

Type Compatibility In the class hierarchy Each Undergraduate is also a Student Each Student is also a Person An object of a derived class can serve as an object of the base class Note this is not typecasting An object of a class can be referenced by a variable of an ancestor type

Type Compatibility Be aware of the "is-a" relationship A Student is a Person Another relationship is the "has-a" A class can contain (as an instance variable) an object of another type If we specify a date of birth variable for Person – it "has-a" Date object

The Class Object Java has a class that is the ultimate ancestor of every class The class Object Thus possible to write a method with parameter of type Object Actual parameter in the call can be object of any type Example: method println(Object theObject)

The Class Object Class Object has some methods that every Java class inherits Examples Method equals Method toString Method toString called when println(theObject) invoked Best to define your own toString to handle this