Top Banner
Chapter 11 Inheritance and Composition
35

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

Dec 29, 2015

Download

Documents

Osborn Bond
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.

Chapter 11

Inheritance and Composition

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

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

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

Chapter Objectives

• Examine abstract classes

• Become aware of interfaces

• Learn about composition

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

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

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

Inheritance

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

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

Inheritance: class Circle Derived from Shape

public class Circle extends Shape{ . . .}

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

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

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

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)

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

UML Diagram: class Rectangle

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

UML Diagram: class Box

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

Defining Constructors of the Subclass

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

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

Objects myRectangle and myBox

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

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

Protected Members of a Class

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

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

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

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 //...}

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

Some Constructors and Methods of the class Object

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

Hierarchy of Java Stream Classes

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

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

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

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

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

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

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

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

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

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

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

Some Interface Definitions

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

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”

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

Composition Example

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

Programming Example: Grade Report

• Components: student, course

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

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

Components Course and Student

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

Components Course and Student

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

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

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

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

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

UML diagram of class Student

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

Sample Output: Grade Report Program

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

Sample Output: After Clicking Next in Grade Report

Program

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

Chapter Summary

• Inheritance– Single and multiple

– Rules

– Uses

– Superclasses/subclasses (objects)

– Overriding/overloading methods

• The class Object– Constructors

– Rules

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

Chapter Summary

• Java Stream Classes

• Abstract methods

• Abstract classes

• Interfaces

• Composition