Presentation is loading. Please wait.

Presentation is loading. Please wait.

8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.

Similar presentations


Presentation on theme: "8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class."— Presentation transcript:

1 8. Inheritance “Is-a” Relationship

2 Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class

3 Inheritance Inheritance allows developer to derive a new class from an existing one Inheritance allows developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass The derived class is called the child class or subclass The child inherits characteristics of the parent The child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class That is, the child class inherits the methods and data defined by the parent class

4 UML Diagram UML Diagram for Parent-Child class relationship UML Diagram for Parent-Child class relationship Is-A Relationship Child is a more specific version of the parent Is-A Relationship Child is a more specific version of the parent Vehicle Car

5 Inheritance A derived class can be tailored to specific needs by A derived class can be tailored to specific needs by Adding new methods Adding new methods Adding new data Adding new data Takes advantage of design, implementation and testing result of the parent class Takes advantage of design, implementation and testing result of the parent class Promotes reusability of code, modifiability of program. Promotes reusability of code, modifiability of program.

6 Inheritance Use reserved word extends to establish inheritance. Use reserved word extends to establish inheritance. Words.java (page 442) Words.java (page 442) Words.java Book.java (page 443) Book.java (page 443) Book.java Dictionary.java (page 444) Dictionary.java (page 444) Dictionary.java Car extends Vehicle { // class contents // class contents}

7 Visibility Modifiers Visibility modifiers affect the way that class members can be used in a child class Visibility modifiers affect the way that class members can be used in a child class Private variables and methods cannot be referenced by name in a child class Private variables and methods cannot be referenced by name in a child class Public variables and methods are visible to all classes -- but public variables violate the principle of encapsulation Public variables and methods are visible to all classes -- but public variables violate the principle of encapsulation Protected variables and methods are visible to child classes, but not to others. Protected variables and methods are visible to child classes, but not to others.

8 Inheritance Protected methods or data in the parent class are inherited by a child class. Protected methods or data in the parent class are inherited by a child class. They are not visible to other classes, even in the same package. They are not visible to other classes, even in the same package.

9 super() Reference Constructors are not inherited. Constructors are not inherited. To make use of a constructor in the parent class—e.g., to use its initialization code— use super(), which is same as invoking the parent’s constructor. To make use of a constructor in the parent class—e.g., to use its initialization code— use super(), which is same as invoking the parent’s constructor. Constructor in a child class should call super() as the first statement. Constructor in a child class should call super() as the first statement. Words2.java (page 447) Words2.java (page 447) Words2.java Book2.java (page 448) Book2.java (page 448) Book2.java Dictionary2.java (page 449) Dictionary2.java (page 449) Dictionary2.java

10 Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class

11 Overriding a Method An inherited method can be overridden—so that it has the same signature by completely different body definition. An inherited method can be overridden—so that it has the same signature by completely different body definition. If such a method has a different signature, e.g., more parameters, then the method is a new (overloaded) method. If such a method has a different signature, e.g., more parameters, then the method is a new (overloaded) method. Messages.java (page 452) Messages.java (page 452) Messages.java Thought.java (page 453) Thought.java (page 453) Thought.java Advice.java (page 454) Advice.java (page 454) Advice.java

12 Overloading Vs Overriding Overloading refers to multiple methods with the same name in the same class, but with different signatures Overloading refers to multiple methods with the same name in the same class, but with different signatures Overriding refers to two methods, one in a parent class and one in a child class, that have the same signature Overriding refers to two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different parameters Overloading lets you define a similar operation in different ways for different parameters Overriding lets you define a similar operation in different ways for different object types Overriding lets you define a similar operation in different ways for different object types

13 Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class

14 Inheritance Hierarchy Business KMartMacys ServiceBusiness Kinkos RetailBusiness A child class can be the parent class of another subclass.

15 Inheritance Hierarchy Two children of the same parent are called siblings Two children of the same parent are called siblings Common features should be put as high in the hierarchy as is reasonable Common features should be put as high in the hierarchy as is reasonable An inherited member is passed continually down the line An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes Therefore, a child class inherits from all its ancestor classes There is no single class hierarchy that is appropriate for all situations There is no single class hierarchy that is appropriate for all situations

16 The Object Class A class called Object is defined in the java.lang package of the Java standard class library A class called Object is defined in the java.lang package of the Java standard class library All classes are derived from the Object class All classes are derived from the Object class If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class Therefore, the Object class is the ultimate root of all class hierarchies Therefore, the Object class is the ultimate root of all class hierarchies Java 1.5

17 The Object Class The Object class contains a few useful methods, which are inherited by all classes The Object class contains a few useful methods, which are inherited by all classes For example, the toString method is defined in the Object class For example, the toString method is defined in the Object class Every time we define the toString method, we are actually overriding an inherited definition Every time we define the toString method, we are actually overriding an inherited definition The toString method in the Object class is defined to return a string that contains the name of the object’s class along with some other information The toString method in the Object class is defined to return a string that contains the name of the object’s class along with some other information

18 Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class

19 Abstract Class An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class is a placeholder in a class hierarchy that represents a generic concept An abstract class cannot be instantiated An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract: We use the modifier abstract on the class header to declare a class as abstract:

20 Abstract Class public abstract class Product { // contents // contents}

21 Abstract Class An abstract class often contains abstract methods with no definitions (like an interface) An abstract class often contains abstract methods with no definitions (like an interface) Unlike an interface, the abstract modifier must be applied to each abstract method Unlike an interface, the abstract modifier must be applied to each abstract method Also, an abstract class typically contains non- abstract methods with full definitions Also, an abstract class typically contains non- abstract methods with full definitions A class declared as abstract does not have to contain abstract methods -- simply declaring it as abstract makes it so A class declared as abstract does not have to contain abstract methods -- simply declaring it as abstract makes it so

22 Abstract Class The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract An abstract method cannot be defined as final or static An abstract method cannot be defined as final or static The use of abstract classes is an important element of software design – it allows us to establish common elements in a hierarchy that are too generic to instantiate The use of abstract classes is an important element of software design – it allows us to establish common elements in a hierarchy that are too generic to instantiate


Download ppt "8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class."

Similar presentations


Ads by Google