Chapter 11 inheritance and polymorphism
Download
1 / 40

Chapter 11: Inheritance and Polymorphism - PowerPoint PPT Presentation


  • 153 Views
  • Updated On :

Chapter 11: Inheritance and Polymorphism. J ava P rogramming: From Problem Analysis to Program Design, Second Edition. Chapter Objectives. Learn about inheritance. Learn about subclasses and superclasses. Explore how to override the methods of a superclass.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 11: Inheritance and Polymorphism' - coty


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Chapter 11 inheritance and polymorphism l.jpg

Chapter 11: Inheritance and Polymorphism

JavaProgramming:

From Problem Analysis to Program Design,

Second Edition


Chapter objectives l.jpg
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 objectives3 l.jpg
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 l.jpg
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


Inheritance5 l.jpg
Inheritance

modifier(s)class ClassName extendsExistingClassName

modifier(s)

{

memberList

}

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


Inheritance class circle derived from class shape l.jpg
Inheritance: classCircle Derived from classShape

public class Circle extends Shape

{

.

.

.

}

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


Inheritance7 l.jpg
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


Inheritance8 l.jpg
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 l.jpg
UML Class Diagram: classRectangle

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


Uml class diagram class box l.jpg
UML Class Diagram: classBox

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


Class box l.jpg
classBox

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 l.jpg
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 l.jpg
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 l.jpg
Protected Members of a Class

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


The class object l.jpg
The classObject

  • Directly or indirectly becomes the superclass of every class in Java.

  • Public members of classObject 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 l.jpg
The classObject: Equivalent Definition of a Class

public classClock

{

//Declare instance variables as given in Chapter 8

//Definition of instance methods as given in Chapter 8

//...

}

public classClock extendsObject

{

//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 l.jpg
Some Constructors and Methods of the classObject

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


Hierarchy of java stream classes l.jpg
Hierarchy of Java Stream Classes

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


Polymorphism l.jpg
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


Polymorphism20 l.jpg
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 classPerson or the classPartTimeEmployee.

  • 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


Polymorphism21 l.jpg
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


Polymorphism22 l.jpg
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


Polymorphism23 l.jpg
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 classBoxShape; otherwise it evaluates to false:

    p instanceof BoxShape

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


Abstract methods l.jpg
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 l.jpg
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 l.jpg
Abstract Class Example

public abstract classAbstractClassExample

{

protected intx;

public void abstractprint();

public voidsetX(inta)

{

x = a;

}

publicAbstractClassExample()

{

x = 0;

}

}

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


Interfaces l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
Composition Example

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


Composition example31 l.jpg
Composition Example

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


Programming example grade report l.jpg
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 l.jpg
Components Course and Student

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


Components course and student34 l.jpg
Components Course and Student

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


Programming example grade report35 l.jpg
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 report36 l.jpg
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 l.jpg
Sample Output: Grade Report Program

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


Sample output after clicking next in grade report program l.jpg
Sample Output: After Clicking Next in Grade Report Program

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


Chapter summary l.jpg
Chapter Summary

  • Inheritance:

    • 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 summary40 l.jpg
Chapter Summary

  • Java stream classes

  • Polymorphism

  • Abstract methods

  • Abstract classes

  • Interfaces

  • Composition

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


ad