Inheritance In Java

ArnabBhaumik2 1,514 views 18 slides Sep 24, 2018
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

A Presentation On Inheritance In Java.


Slide Content

INHERITANCE
IN ARNAB BHAUMIK
ECE, 3
RD
YEAR
UNIVERSITY ROLL NO. :
23900315009
GUIDED BY :
SANKHAMITRA ROY

•WHY PROGRAMMING?
•WHY JAVA?
•CLASSES AND OBJECTS
•INTRODUCTION TO INHERITANCE


TYPES OF INHERITANCE
ADVANTAGES AND DISADVANTAGES
CONTENTS

WHY PROGRAMMING?
•Programming is the term that refers to teaching, instructing or giving commands to the computer.

WHY JAVA?
•Simple
•Object-Oriented
•Platform Independent
•Secure
•Robust
•Multithreaded

CLASSES AND OBJECTS
•A class is a blueprint from which individual objects are created.
colour
name

INTRODUCTION TO INHERITANCE
•Inheritance in javais a mechanism in which one class acquires all the properties and behaviours of
another class.
•Sub Class :The class that inherits properties and behaviours from another class is called Sub class or
Derived Class.
•Super Class : The class whose properties and behaviours are inherited by sub class is called Base
Class or Super class.
•Syntax of Java Inheritance
classSubclass-nameextendsSuperclass-name
{
//methodsandfields
}

INTRODUCTION TO INHERITANCE
•Why and when to use inheritance?

TYPES OF INHERITANCE
•On the basis of class, there can be mainly three types of inheritance in java:
1.Single
2.Multilevel
3.Hierarchical

SINGLE INHERITANCE
Class A
Class B

SINGLE INHERITANCE
PROGRAM
classAnimal {
voideat() {
System.out.println("eating...");
}
}
classDogextendsAnimal {
voidbark() {
System.out.println("barking...");
}
}
classTestInheritance{
publicstaticvoidmain(Stringargs[ ]) {
Dogd=newDog();
d.bark();
d.eat();
}
}
OUTPUT
barking...
eating...

MULTILEVEL INHERITANCE
Class A
Class B
Class C

MULTILEVEL INHERITANCE
PROGRAM
classAnimal {
voideat() {
System.out.println("eating...");
}
}
classDogextendsAnimal {
voidbark() {
System.out.println("barking...");
}
}
classBabyDogextendsDog {
voidweep() {
System.out.println("weeping...");
}
}
classTestInheritance2 {
publicstaticvoidmain(Stringargs[ ]) {
BabyDogd=newBabyDog();
d.weep();
d.bark();
d.eat();
}
}
OUTPUT
weeping...
barking...
eating...

HIERARCHICAL INHERITANCE
Class A
Class B Class C

HIERARCHICAL INHERITANCE
PROGRAM
classAnimal {
voideat() {
System.out.println("eating...");
}
}
classDogextendsAnimal {
voidbark() {
System.out.println("barking...");
}
}
classCatextendsAnimal {
voidmeow() {
System.out.println("meowing...");
}
}
classTestInheritance3 {
publicstaticvoidmain(Stringargs[ ]) {
Catc=newCat();
Dog d=new Dog();
c.meow();
c.eat();
d.bark();
d.eat();
}
}
OUTPUT
meowing...
eating...
barking…
eating…

MULTIPLE AND HYBRID INHERITANCE
Class A Class B
Class C
Class B Class C
Class D
Class A

ADVANTAGES AND DISADVANTAGES
ADVANTAGES
•Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of
inherited class.
•Reusability enhanced reliability. The base class code will be already tested and debugged.
•As the existing code is reused, it leads to less development and maintenance costs.
DISADVANTAGES
•Inherited functions work slower than normal function as there is indirection.
•Improper use of inheritance may lead to wrong solutions.
•Often, data members in the base class are left unused which may lead to memory wastage.
•Inheritance increases the coupling between base class and derived class. A change in base class will affect all the
child classes.

References:
•“The Complete Reference Java”byHerbert Schildt.
•“Java Programming Black Book”by KogentLearning solution.
Tags