Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics Inheritance introduction

Similar presentations


Presentation on theme: "Topics Inheritance introduction"— Presentation transcript:

1 Topics Inheritance introduction
is – a relationship & has – a relationship Super classes and Sub classes Types of inheritance Single inheritance Hierarchical inheritance Multilevel inheritance Multiple inheritance

2 Introduction Inheritance
A form of software reuse in which a new class is created by absorbing an existing class’s members and adding them with new or modified capabilities. Can save time during program development by basing new classes on existing proven and debugged high-quality software. Increases the likelihood that a system will be implemented and maintained effectively.

3 Introduction When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. Existing class is the superclass New class is the subclass Each subclass can be a superclass of future subclasses. A subclass can add its own fields and methods. A subclass is more specific than its superclass and represents a more specialized group of objects. The subclass exhibits the behaviors of its superclass and can add behaviors that are specific to the subclass.

4 Introduction 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. 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.

5 is – a relationship & has – a relationship
The is a relationship is expressed with inheritance and has a relationship is expressed with composition. For Example: is a --- House is a Building class Building { } class House extends Building { For Example: has a -- House has a bathroom class House { Bathroom room = new Bathroom() ; .... public void getTotMirrors() room.getNoMirrors(); }

6 is – a relationship & has – a relationship
Inheritance Vs Composition Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom. Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.

7 Superclasses and Subclasses
Classes can be derived from other classes. The derived class (the class that is derived from another class) is called a subclass. The class from which its derived is called the superclass.

8 Superclasses and Subclasses
In fact, in Java, all classes must be derived from some class. Which leads to the question "Where does it all begin?" The top-most class, the class from which all other classes are derived, is the Object class defined in java.lang. Object is the root of a hierarchy of classes.

9 Superclasses and Subclasses
Example

10 Superclasses and Subclasses
Figure shows a sample university community class hierarchy. Also called an inheritance hierarchy. Each arrow in the hierarchy represents an is-a relationship. Follow the arrows upward in the class hierarchy An Employee is a CommunityMember” “a Teacher is a Faculty member.” CommunityMember is the direct superclass of Employee, Student and Alumnus and is an indirect superclass of all the other classes in the diagram. Starting from the bottom, you can follow the arrows and apply the is-a relationship up to the topmost superclass.

11 Type of Inhertiance Four kinds of inheritance in JAVA. They are.
Single Inheritance (Only one super class). Multilevel Inheritance (Derived from a derived class). Hierarichal Inheritance (one Super class, many subclasses). Multiple Inheritance (Several Super classes).

12 Hierarchical Inheritance Multilevel Inheritance
Type of Inheritance Pictorial Representation: Single Inheritance Hierarchical Inheritance Person Person Student Employee Student Multilevel Inheritance Person Employee Student Student Multiple Inhertiance Employee - Student Day Scholar

13 Single Inheritance When a subclass is derived simply from it's parent class then this mechanism is known as single inheritance. In case of single inheritance there is only a sub class and it's parent class. It is also called one level inheritance. For Example package yuc.edu.sa; public class SingleInhertiance { int x; int y; public void set(int p, int q){ x=p; y=q; } void Show(){ System.out.println(x); package yuc.edu.sa; public class SingleInhertianceApp extends SingleInhertiance { public static void main(String[ ] args) { SingleInhertiance singleInhertiance = new SingleInhertiance(); singleInhertiance.set(10, 20); singleInhertiance.Show(); }

14 Hierarchical Inheritance
One Parent class has many sub classes. And this is the concept of Hierarchical Inheritance. For Example:

15 Hierarchical Inheritance
Output:

16 Multilevel Inheritance
Derived from a derived class. Such concept is called Multilevel inheritance. For Example:

17 Multilevel Inheritance
Output:

18 Multiple Inheritance The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.

19 ?


Download ppt "Topics Inheritance introduction"

Similar presentations


Ads by Google