0% found this document useful (0 votes)
12 views

Inheritance

Uploaded by

shadow.range03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Inheritance

Uploaded by

shadow.range03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

INHERITANCE

IN MODULE 3
BCS306A
CONTENTS

• WHY PROGRAMMING?
• WHY JAVA?
• CLASSES AND OBJECTS
• INTRODUCTION TO INHERITANCE
TYPES OF INHERITANCE
ADVANTAGES AND DISADVANTAGES
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.

colou
r
name
INTRODUCTION TO INHERITANCE
• Inheritance in java is a mechanism in which one class acquires all the
properties and behaviors 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
class Subclass-name extends Superclass-name
{
//methods and fields
}
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
class Animal { void eat() {
System.out.println("eating...");
}
} OUTPUT
barking...
class Dog extends Animal { void bark() {
eating...
System.out.println("barking...");
}
}
class TestInheritance {
public static void main(String args[ ]) {
Dog d=new Dog();
d.bark();
d.eat();
}
MULTILEVEL INHERITANCE
Class A

Class B

Class C
MULTILEVEL INHERITANCE
PROGRAM
class Animal {
void eat() { class TestInheritance2 {
System.out.println("eating..."); public static void main(String args[ ])
{ BabyDog d=new BabyDog(); d.weep();
} d.bark();
} d.eat();
class Dog extends Animal { }
}
void bark() {
System.out.println("barking...");
}
}
class BabyDog extends Dog { void OUTPUT
weeping.
weep() {
..
System.out.println("weeping..." barking..
); .
eating...
HIERARCHICAL INHERITANCE

Class A

Class B Class C
HIERARCHICAL INHERITANCE
PROGRAM class TestInheritance3 {
public static void main(String args[ ])
class Animal { {
void eat() { Cat c=new Cat();
System.out.println("eating..."); Dog d=new Dog();
} c.meow();
c.eat();
} d.bark();
class Dog extends Animal { d.eat();
void bark() { }
System.out.println("barking...") }
;
} OUTPUT
} meowing.
..
class Cat extends Animal { eating...
void meow() { barking…
System.out.println("meowing... eating…
MULTIPLE AND HYBRID
INHERITANCE
Class A
Class A Class B

Class B Class C
Class C

Class D
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.
Inheritance
 Inheritance can be defined as the process where one class acquires the properties (methods and fields) of
another. With the use of inheritance the information is made manageable in a hierarchical order.
 The class which inherits the properties of other is known as subclass (derived class, child class) and the class
whose properties are inherited is known as superclass (base class, parent class).
extends Keyword
 extends is the keyword used to inherit the properties of a class.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
Types of Inheritance
There are various types of inheritance as demonstrated below.
A very important fact to remember is that Java
does not support multiple inheritance. This
means that a class cannot extend more than one
class. Therefore following is illegal −
Example
public class extends Animal,
Mammal{}
However, a class can implement one or more
interfaces, which has helped Java get rid of the
impossibility of multiple inheritance.
TYPES OF INHERITANCE
Reference
s:
• “The Complete Reference Java” by Herbert Schildt.
• “Java Programming Black Book” by Kogent Learning
solution.

You might also like