Transcript
Page 1: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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

Page 2: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 3: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 4: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 5: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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(); .... } }

Page 6: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 7: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 8: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 9: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Superclasses and Subclasses

Example

Page 10: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 11: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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

Page 12: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Type of Inheritance

Pictorial Representation:

Person

Student

Single Inheritance

Person

Student

Multilevel Inheritance

Day Scholar

Person

Employee Student

Hierarchical Inheritance

Employee Student

Employee - Student

Multiple Inhertiance

Page 13: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

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

For Example

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

Page 14: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Hierarchical Inheritance

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

For Example:

Page 15: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Hierarchical Inheritance

Output:

Page 16: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Multilevel Inheritance

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

Page 17: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

Multilevel Inheritance

Output:

Page 18: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

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.

Page 19: Inheritance introduction  is – a relationship & has – a relationship  Super classes and Sub classes  Types of inheritance  Single inheritance  Hierarchical

?


Top Related