5. CLASSES AND OBJECTS
• A class is a blueprint from which individual objects are created.
colour
name
6. INTRODUCTION TO INHERITANCE
• Inheritance in java is 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
class Subclass-name extends Superclass-name
{
//methods and fields
}
14. HIERARCHICAL INHERITANCE
PROGRAM
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class Cat extends Animal {
void meow() {
System.out.println("meowing...");
}
}
class TestInheritance3 {
public static void main(String args[ ]) {
Cat c=new Cat();
Dog d=new Dog();
c.meow();
c.eat();
d.bark();
d.eat();
}
}
OUTPUT
meowing...
eating...
barking…
eating…
15. MULTIPLE AND HYBRID INHERITANCE
Class A Class B
Class C
Class B Class C
Class D
Class A
16. 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.
17. References:
• “The Complete Reference Java” by Herbert Schildt.
• “Java Programming Black Book” by Kogent Learning solution.