File

advertisement

 Inheritance introduction

 is – a relationship & has – a relationship

 Super classes and Sub classes

 Types of inheritance

 Single inheritance

 Hierarchical inheritance

 Multilevel inheritance

 Multiple inheritance

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.

 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.

 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.

The is a relationship is expressed with inheritance and has a relationship is expressed with composition.

For Example: For Example:

} is a --- House is a Building class Building {

.......

class House extends Building {

}

.........

has a -- House has a bathroom

{ class House

Bathroom room = new Bathroom() ;

....

}

{ public void getTotMirrors() room.getNoMirrors();

}

....

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.

 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

.

 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.

 Example

 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

relationship

up to the topmost superclass.

is-a

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

Pictorial Representation:

 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();

}

}

 One Parent class has many sub classes. And this is the concept of Hierarchical

Inheritance.

 For Example:

Output:

 Derived from a derived class. Such concept is called Multilevel inheritance.

 For Example:

Output:

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

.

Download