Chapter 10: Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Java 212: Inheritance and Polymorphism. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override (not.
Advertisements

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
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.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Java 212: Inheritance and Polymorphism. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override (not.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Inheritance and Composition.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 12: Inheritance and Composition.
UML Class Diagram: class Rectangle
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 13: Inheritance and Composition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Intro to OOP with Java, C. Thomas Wu
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
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.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
Inheritance in the Java programming language J. W. Rider.
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.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
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,
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Object-Oriented Programming: Polymorphism Chapter 10.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 11 Polymorphism. Contents I. Polymorphism 1. Polymorphism 2. Polymorphism and Dynamic Binding 3. The “is-a” Relationship Does Not Work in Reverse.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Modern Programming Tools And Techniques-I Inheritance
Inheritance, Polymorphism, and Interfaces
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Polymorphism CT1513.
Week 6 Object-Oriented Programming (2): Polymorphism
CSE 1030: Implementing GUI Mark Shtern.
Introduction to Java Programming
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

Chapter 10: Inheritance and Polymorphism Java Programming: From Problem Analysis to Program Design, Fourth Edition

Chapter Objectives Learn about inheritance. Learn about subclasses and superclasses. Explore how to override the methods of a superclass. Examine how constructors of superclasses and subclasses work. Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Objectives Learn about polymorphism. Examine abstract classes. Become aware of interfaces. Learn about composition. Java Programming: From Problem Analysis to Program Design, Second Edition

Inheritance “is-a” relationship Single inheritance: Subclass is derived from one existing class (superclass). Multiple inheritance: Subclass is derived from more than one superclass. Not supported by Java. In Java, a class can only extend the definition of one class. Java Programming: From Problem Analysis to Program Design, Second Edition

Inheritance modifier(s) class ClassName extends ExistingClassName { memberList } Java Programming: From Problem Analysis to Program Design, Second Edition

Inheritance: class Circle Derived from class Shape public class Circle extends Shape { . } Java Programming: From Problem Analysis to Program Design, Second Edition

Inheritance The private members of the superclass are private to the superclass. The subclass can directly access the public members of the superclass. The subclass can include additional data and method members. The subclass can override (redefine) the public methods of the superclass. However, this redefinition applies only to the objects of the subclass, not to the objects of the superclass. All data members of the superclass are also data members of the subclass. Similarly, the methods of the superclass (unless overridden) are also the methods of the subclass. (Remember Rule 1 when accessing a member of the superclass in the subclass.) Java Programming: From Problem Analysis to Program Design, Second Edition

Inheritance To write a method’s definition of a subclass, specify a call to the public method of the superclass. If subclass overrides public method of superclass, specify call to public method of superclass: super.MethodName(parameter list) If subclass does not override public method of superclass, specify call to public method of superclass: MethodName(parameter list) Java Programming: From Problem Analysis to Program Design, Second Edition

UML Class Diagram: class Rectangle Java Programming: From Problem Analysis to Program Design, Second Edition

UML Class Diagram: class Box Java Programming: From Problem Analysis to Program Design, Second Edition

class Box public void print() { super.print(); System.out.print("; Height = " + height); } public void setDimension(double l, double w, double h) super.setDimension(l, w); if (h >= 0) height = h; else height = 0; public double area() return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); Java Programming: From Problem Analysis to Program Design, Second Edition

Defining Constructors of the Subclass Call to constructor of superclass: Must be first statement. Specified by super parameter list. public Box() { super(); height = 0; } public Box(double l, double w, double h) super(l, w); height = h; Java Programming: From Problem Analysis to Program Design, Second Edition

Objects myRectangle and myBox Rectangle myRectangle = new Rectangle(5, 3); Box myBox = new Box(6, 5, 4); Java Programming: From Problem Analysis to Program Design, Second Edition

Protected Members of a Class Java Programming: From Problem Analysis to Program Design, Second Edition

The class Object Directly or indirectly becomes the superclass of every class in Java. Public members of class Object can be overridden/invoked by object of any class type. Java Programming: From Problem Analysis to Program Design, Second Edition

The class Object: Equivalent Definition of a Class public class Clock { //Declare instance variables as given in Chapter 8 //Definition of instance methods as given in Chapter 8 //... } public class Clock extends Object //Declare instance variables as given in Chapter 8 //Definition of instance methods as given in Chapter 8 Java Programming: From Problem Analysis to Program Design, Second Edition

Some Constructors and Methods of the class Object Java Programming: From Problem Analysis to Program Design, Second Edition

Hierarchy of Java Stream Classes Java Programming: From Problem Analysis to Program Design, Second Edition

Polymorphism Java allows us to treat an object of a subclass as an object of its superclass. In other words, a reference variable of a superclass type can point to an object of its subclass. Person name, nameRef; PartTimeEmployee employee, employeeRef; name = new Person("John", "Blair"); employee = new PartTimeEmployee("Susan", "Johnson", 12.50, 45); nameRef = employee; System.out.println("nameRef: " + nameRef); nameRef: Susan Johnson wages are: $562.5 Java Programming: From Problem Analysis to Program Design, Second Edition

Polymorphism Late binding or dynamic binding (run-time binding): Method to be executed is determined at execution time, not compile time. The term polymorphism means to assign multiple meanings to the same method name. In Java, polymorphism is implemented using late binding. The reference variable name or nameRef can point to any object of the class Person or the class PartTimeEmployee. These reference variables have many forms, that is, they are polymorphic reference variables. They can refer to objects of their own class or to objects of the classes inherited from their class. Java Programming: From Problem Analysis to Program Design, Second Edition

Polymorphism You can declare a method of a class final using the keyword final. For example, the following method is final. public final void doSomeThing() { //... } If a method of a class is declared final, it cannot be overridden with a new definition in a derived class. In a similar manner, you can also declare a class final using the keyword final. If a class is declared final, then no other class can be derived from this class. Java does not use late binding for methods that are private, marked final, or static. Java Programming: From Problem Analysis to Program Design, Second Edition

Polymorphism You cannot automatically make reference variable of subclass type point to object of its superclass. Suppose that supRef is a reference variable of a superclass type. Moreover, suppose that supRef points to an object of its subclass: You can use an appropriate cast operator on supRef and make a reference variable of the subclass point to the object. On the other hand, if supRef does not point to a subclass object and you use a cast operator on supRef to make a reference variable of the subclass point to the object, then Java will throw a ClassCastException—indicating that the class cast is not allowed. Java Programming: From Problem Analysis to Program Design, Second Edition

Polymorphism Operator instanceof: Determines whether a reference variable that points to an object is of a particular class type. This expression evaluates to true if p points to an object of the class BoxShape; otherwise it evaluates to false: p instanceof BoxShape Java Programming: From Problem Analysis to Program Design, Second Edition

Abstract Methods A method that has only the heading with no body. Must be declared abstract. public void abstract print(); public abstract object larger(object, object); void abstract insert(int insertItem); Java Programming: From Problem Analysis to Program Design, Second Edition

Abstract Classes A class that is declared with the reserved word abstract in its heading. An abstract class can contain instance variables, constructors, finalizers, and non-abstract methods. An abstract class can contain abstract methods. If a class contains an abstract method, the class must be declared abstract. You cannot instantiate an object of an abstract class type. You can only declare a reference variable of an abstract class type. You can instantiate an object of a subclass of an abstract class, but only if the subclass gives the definitions of all the abstract methods of the superclass. Java Programming: From Problem Analysis to Program Design, Second Edition

Abstract Class Example public abstract class AbstractClassExample { protected int x; public void abstract print(); public void setX(int a) x = a; } public AbstractClassExample() x = 0; Java Programming: From Problem Analysis to Program Design, Second Edition

Interfaces A class that contains only abstract methods and/or named constants. How Java implements multiple inheritance. To be able to handle a variety of events, Java allows a class to implement more than one interface. Java Programming: From Problem Analysis to Program Design, Second Edition

Some Interface Definitions public interface WindowListener { public void windowOpened(WindowEvent e); public void windowClosing(WindowEvent e); public void windowClosed(WindowEvent e); public void windowIconified(WindowEvent e); public void windowDeiconified(WindowEvent e); public void windowActivated(WindowEvent e); public void windowDeactivated(WindowEvent e); } public interface ActionListener public void actionPerformed(ActionEvent e); Java Programming: From Problem Analysis to Program Design, Second Edition

Composition Another way to relate two classes. One or more members of a class are objects of another class type. “has-a” relation between classes. For example, “every person has a date of birth.” Java Programming: From Problem Analysis to Program Design, Second Edition

Composition Example Java Programming: From Problem Analysis to Program Design, Second Edition

Composition Example Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Grade Report Components: Student, course. Operations on course: Set course information. Print course information. Show credit hours. Show course number. Java Programming: From Problem Analysis to Program Design, Second Edition

Components Course and Student Java Programming: From Problem Analysis to Program Design, Second Edition

Components Course and Student Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Grade Report Operations on student: Set student information. Print student information. Calculate number of credit hours taken. Calculate GPA. Calculate billing amount. Sort the courses according to the course number. Java Programming: From Problem Analysis to Program Design, Second Edition

Programming Example: Grade Report Main algorithm: Declare variables. Open input file. Open output file. Get number of students registered and tuition rate. Load students’ data. Print grade reports. Java Programming: From Problem Analysis to Program Design, Second Edition

Sample Output: Grade Report Program Java Programming: From Problem Analysis to Program Design, Second Edition

Sample Output: After Clicking Next in Grade Report Program Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Summary Inheritance: The class Object Single and multiple Rules Uses Superclasses/subclasses (objects) Overriding/overloading methods Constructors The class Object Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Summary Java stream classes Polymorphism Abstract methods Abstract classes Interfaces Composition Java Programming: From Problem Analysis to Program Design, Second Edition