Chapter 5 Hierarchies IS-A associations superclasses subclasses

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
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 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
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.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Topic 4 Inheritance.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 More About Derived Classes and Inheritance Chapter 9.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Web Design & Development Lecture 9
Inheritance ITI1121 Nour El Kadri.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9 Object-Oriented Programming: Inheritance
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
CS18000: Problem Solving and Object-Oriented Programming
Java – Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
CS 240 – Advanced Programming Concepts
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chapter 5 Hierarchies IS-A associations superclasses subclasses inheritance abstract classes

Chapter 5 Hierarchies – Practitioner example Example. Practitioner hierarchy Consider that we have an application dealing with doctors and pharmacists As doctors and pharmacists have lots in common we generalize … and introduce practitioner We can state: A doctor is a practitioner A pharmacist is a practitioner IS-A relationships

Chapter 5 Hierarchies – Practitioner example Example. Practitioner hierarchy Practitioner is a generalization of Doctor and Pharmacist Doctors and Pharmacists are specializations of Practitioner. Now we have 3 classes Superclass: Practitioner Subclasses: Doctor & Pharmacist

Chapter 5 Hierarchies – Practitioner example Example. Practitioner hierarchy BlueJ class diagram: When we create a Doctor object, that object is also an instance of Practitioner Similarly … Pharmacist … If we explicitly create a Practitioner object then that object is not an instance of Doctor nor Pharmacist.

Chapter 5 Hierarchies – Practitioner example Practitioner: All the fields and methods common to both Doctor & Pharmacist Doctor: fields and methods pertinent to Doctor but not Pharmacist Pharmacist : fields and methods pertinent to Pharmacist but not Doctor

Practitioner: A more complete UML class diagram A doctor has these fields/methods plus all those of Practitioner too A pharmacist has …

Java code for Practitioner class: Nothing here to suggest Practitioner is a superclass public class Practitioner { private String firstName; private String lastName; private String gender; … normal class with constructors, getters, setters and: } public String toString(){ return getName()+" "+gender; public String getName(){ return firstName+" "+lastName;

Hierarchies – Practitioner example, creating practitioners Nothing unusual here /** * Demonstration class to create practitioners */ public class CreatePractitioners { public static void main(String[] args){ Practitioner john = new Practitioner(); Practitioner tom = new Practitioner("Tom","Smith","male"); System.out.println("Practitioners:\n"+john+"\n"+tom); } Output 

Hierarchies – Practitioner example, creating practitioners via no-arg constructor via other constructor

Java code for Pharmacist class: /** * The Pharmacist class * - a subclass of Practitioner * - a pharmacist "is a" practitioner */ public class Pharmacist extends Practitioner { private String location; * by default, the no-arg constructor calls * the no-arg constructor in Practitioner public Pharmacist() location = "unknown"; } Specifies this is a subclass of Practitioner No-arg constructor -automatic call to superclass no-arg constructor

Java code for Pharmacist class: /** * constructor for when information is available */ public Pharmacist(String firstName, String lastName, String gender, String location) { // note the explicit call to a Practitioner constructor super(firstName, lastName, gender); this.location = location; } Invoke the superclass constructor by using super

Java code for Pharmacist class: // getters public String getLocation(){ return location; } // setters public void setLocation(String location){ this.location = location; Normal getter/setter for specific fields added for pharmacists

Java code for Doctor class: /** * The Doctor class * - a subclass of Practitioner * - an instructor "is a" practitioner */ public class Doctor extends Practitioner { private String specialty; * no-arg constructor, recall default call * to Practitioner no-arg constructor public Doctor() { specialty = "unknown"; } Specifies this is a subclass of Practitioner No-arg constructor -automatic call to superclass no-arg constructor

Java code for Doctor class: /** * constructor with firstname etc */ public Doctor(String firstName, String lastName, String gender, String specialty) { // note call to superclass constructor super(firstName, lastName, gender); this.specialty = specialty; } Invoke the superclass constructor by using super

Java code for Doctor class: Getters/setters for specialty public String getSpecialty(){ return specialty; } public void setSpecialty(String specialty){ this.specialty = specialty; public String getName(){ return "Dr. "+getFirstName()+" "+getLastName()+", "+getSpecialty(); Same signature as method in superclass - overrides getName() in superclass

Hierarchies - Creating practitioners, doctors, pharmacists The class Practitioners (next slide) Creates objects Adds each object to a list Iterates through the list displays the type of object instanceof is used to determine the type of an object displays the object’s first name getFirstName is defined in Practitioner

Hierarchies - Creating practitioners, doctors, pharmacists public class Practitioners { public static void main(String[] args){ // List of practitioners ArrayList<Practitioner> practitioners = new ArrayList(); // Create some practitioners Practitioner pr = new Practitioner("Sam","Smith","female"); Doctor dr = new Doctor("Jill","Jones","female","Dermatology"); Pharmacist ph = new Pharmacist("Eddy","Edwards","male","Drugco"); practitioners.add(pr); practitioners.add(dr); practitioners.add(ph); for (Practitioner p: practitioners) { String type="practitioner"; if (p instanceof Doctor) type="doctor"; if (p instanceof Pharmacist) type="pharmacist"; System.out.println(type+" "+p.getFirstName()); } Create objects Add to list Instanceof operator Display type and firstname getFirstName() is defined in Practitioner

Output from previous program First names type

Overriding methods – getName() The Doctor class has a method with the same name and argument list Practitioner has methods… A subclass can override … getName()

Overriding methods – getName() If not a Doctor object JVM uses: If a Doctor object JVM uses: In Practitioner: public String getName(){ return firstName+" "+lastName; } In Doctor: public String getName(){ return "Dr. "+getFirstName()+" "+getLastName()+", "+getSpecialty(); } Different behaviour according to the type of object The method used is determined at runtime … polymorphically … happens automatically

Overriding methods – getName() Consider: for (Practitioner p: practitioners) { // display name // getName() in Doctor overrides // getName() in Practitioner System.out.println( p.getName() ); } Output A Doctor object … different behaviour

super prefix Methods in a subclass automatically override methods in a superclass You can specify a particular superclass method to execute if you use the super prefix, as in: super.getName() Need to be careful… suppose getName in Doctor was coded as: public String getName() { return "Dr. "+ getName()+", "+getSpecialty(); } No prefix, hence this method is referring to itself and this is now a recursive call … a later topic

Abstract classes and methods An abstract class cannot be instantiated.

Abstract classes and methods Suppose we never instantiate a Practitioner object. We can make Practitioner abstract to prevent that from accidentally happening: public abstract class Practitioner … And now you get a compile time error if you code: Practitioner p = new Practitioner();

Abstract classes and methods - Shapes example Shape is abstract and so cannot be instantiated. Shape also has an abstract method, area … no code, just a header … subclasses must have code Square and Circle are not abstract … we say they are concrete subclasses … these can be instantiated

Abstract classes and methods - Shapes example public abstract class Shape { public abstract double area(); } public class Circle extends Shape { private int radius; public Circle (int radius) { this.radius = radius; } public double area(){ return Math.PI*radius*radius; public class Square extends Shape { private int length; public Square(int length) { this.length = length; } public double area() { return length*length;

Abstract classes and methods – display shapes & area import java.util.ArrayList; public class UseShapes { public static void main(String[] args) { ArrayList<Shape> shapes = new ArrayList(); shapes.add( new Square(5) ); shapes.add( new Circle(5) ); for (Shape s: shapes) System.out.println( s +" "+ s.area() ); } Default toString for a type Output Of course, different area calculations/methods used

Summary All classes are subclasses of Object Object has the default definitions for toString, equals, … In a class diagram use the symbol to designate the superclass An object instantiated from a subclass is an instance of that subclass, and at the same time it is an instance of its superclass instanceof is a binary operator used to test an object’s type Subclass methods override superclass methods At runtime the JVM determines the method to run for an object based on the object’s type … i.e. polymorphically

Summary Abstract classes cannot be instantiated An abstract method has no implementation … implementation is left to the subclasses Subclasses can have only one superclass … hence, we have hierarchies Enums cannot be arranged in a hierarchy