Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the."— Presentation transcript:

1 Chapter 11 Inheritance and Composition

2 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

3 Chapter Objectives Examine abstract classes Become aware of interfaces Learn about composition

4 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

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

6 Inheritance: class Circle Derived from Shape public class Circle extends Shape {. }

7 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

8 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)

9 UML Diagram: class Rectangle

10 UML Diagram: class Box

11 Defining Constructors of the Subclass Call to constructor of superclass –must be first statement –specified by: super parameter list

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

13 Protected Members of a Class

14 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

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

16 Some Constructors and Methods of the class Object

17 Hierarchy of Java Stream Classes

18 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

19 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

20 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

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

22 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

23 Some Interface Definitions

24 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”

25 Composition Example

26 Programming Example: Grade Report Components: student, course Operations on course –Set course information –Print course information –Show credit hours –Show course number –Show grade

27 Components Course and Student

28

29 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

30 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

31 UML diagram of class Student

32 Sample Output: Grade Report Program

33 Sample Output: After Clicking Next in Grade Report Program

34 Chapter Summary Inheritance –Single and multiple –Rules –Uses –Superclasses/subclasses (objects) –Overriding/overloading methods The class Object –Constructors –Rules

35 Chapter Summary Java Stream Classes Abstract methods Abstract classes Interfaces Composition


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

Similar presentations


Ads by Google