35
Chapter 11 Inheritance and Composition

Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the

Embed Size (px)

Citation preview

Chapter 11

Inheritance and Composition

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

Chapter Objectives

• Examine abstract classes

• Become aware of interfaces

• Learn about composition

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

Inheritance

modifier(s) class ClassName extends ExistingClassName modifier(s){ memberList}

Inheritance: class Circle Derived from Shape

public class Circle extends Shape{ . . .}

Inheritance

• Private members of superclass– private to superclass; they cannot be accessed

directly by subclass

• Subclass can override public methods of the superclass; redefinition applies only to object of subclass

Inheritance

• To write a method 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)

UML Diagram: class Rectangle

UML Diagram: class Box

Defining Constructors of the Subclass

• Call to constructor of superclass – must be first statement– specified by: super parameter list

Objects myRectangle and myBox

Rectangle myRectangle = new Rectangle(5, 3);Box myBox = new Box(6, 5, 4);

Protected Members of a Class

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

The class Object: Equivalent Definitions

public class Clock{ //Declare instance variables as given in Chapter 8 //Definition of instance methods as given in Chapter 8 //...}The class Object 603 is, in fact, equivalent to the following:public class Clock extends Object{ //Declare instance variables as given in Chapter 8 //Definition of instance methods as given in Chapter 8 //...}

Some Constructors and Methods of the class Object

Hierarchy of Java Stream Classes

Objects of Superclasses and Subclasses

• You cannot automatically make reference variable of subclass type point to object of its superclass

• Dynamic binding: method executed determined at execution time, not compile time

• Operator instanceof: determines whether reference variable that points to object is of particular class type

• ClassCastException thrown if class cast is not allowed

Abstract Methods and Classes

• Abstract method: method that has only the heading with no body– must be declared abstract

• Abstract class: class that is declared with the

reserved word abstract in its heading

Abstract Class

• Can contain instance variables, constructors, finalizer, abstract and nonabstract methods

• You cannot instantiate object of abstract class type; can only declare reference variable

• You can instantiate an object of a subclass of an abstract class, but only if the subclass gives definitions of all abstract methods of the superclass

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; }}

Interfaces

• Definition: 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

Some Interface Definitions

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 – E.g. “every person has a date of birth”

Composition Example

Programming Example: Grade Report

• Components: student, course

• Operations on course– Set course information– Print course information– Show credit hours– Show course number– Show grade

Components Course and Student

Components Course and Student

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

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

UML diagram of class Student

Sample Output: Grade Report Program

Sample Output: After Clicking Next in Grade Report

Program

Chapter Summary

• Inheritance– Single and multiple

– Rules

– Uses

– Superclasses/subclasses (objects)

– Overriding/overloading methods

• The class Object– Constructors

– Rules

Chapter Summary

• Java Stream Classes

• Abstract methods

• Abstract classes

• Interfaces

• Composition