Unit 1 - Inheritence
Unit 1 - Inheritence
● Inheritance in Java is a concept that acquires the properties from one class to
other classes; for example, the relationship between father and son.
● In Java, a class can inherit attributes and methods from another class.
● The class that inherits the properties is known as the sub-class or the child class.
● The class from which the properties are inherited is known as the superclass or
the parent class.
● In Inheritance, the properties of the base class are acquired by the derived
classes.
classsuperclass
{
// superclass data variables
// superclass member functions
}
classsubclass extendssuperclass
{
// subclass data variables
// subclass member functions
}
● Inheritance uses the “extends” keyword to create a derived class by reusing base
class code.
● The extends keyword extends a class and is an indicator that a class is being
inherited by another class.
● When you say class B extends a class A, it means that class B is inheriting the
properties(methods, attributes) from class A.
● Here, class A is the superclass or parent class and class B is the subclass or
child class.
Example:
class Base
{
public void M1()
{
System.out.println(“ Base Class Method ”);
}
}
class Derived extends Base
{
public void M2()
{
System.out.printIn(“ Derived Class Methods “);
}
}
class Test
{
public static void main(String[] args)
{
Derived d = new Derived(); // creating object
d.M1(); // print Base Class Method
d.M2(); // print Derived Class Method
}
}
Output:
● The main advantage of inheritance is code reusability and also method overriding
(runtime polymorphism).
● Inheritance is also known as the IS-A relationship.
Types of Inheritance in Java
● Single Inheritance
● Multiple Inheritance
● Multi-Level Inheritance
● Hierarchical Inheritance
● Hybrid Inheritance
1. Single Inheritance
● Creating subclasses from a single base class is called single inheritance.
class A
{
int a, b;
void display()
{
System.out.println(“Inside class A values =”+a+” ”+b);
}
}
class B extends A
{
int c;
void show()
{
System.out.println(“Inside Class B values=”+a+” “+b+” “+c); }
}
class SingleInheritance
{
public static void main(String args[])
{
B obj = new B(); //derived class object
obj.a=10;
obj.b=20;
obj.c=30;
obj.display();
obj.show();
}
}
Output:
● If the child class has the same method in its implementation as that of its parent
class, then the concept of method overriding comes into play.
● In method overriding, the child class has the same method as that of the parent
class.
● The main use of this is to achieve runtime polymorphism.
● Methods must share the same name in child and parent class.
● It must have the same parameter as in the superclass.
● There must be an IS-A type of Inheritance.
class Superclass
{
int i =20;
void display()
{
System.out.println(“Superclass display method”);
}
}
class Subclass extends Superclass
{
int i = 100;
void display()
{
super.display();
System.out.println(“Subclass display method”);
System.out.println(“ i value =”+i);
System.out.println(“superclass i value =”+super.i);
}
}
class SuperUse
{
public static void main(String args[])
{
Subclass obj = new Subclass();
obj.display();
}
}
Output:
Diamond problem