[go: up one dir, main page]

0% found this document useful (0 votes)
6 views7 pages

Unit 1 - Inheritence

Inheritance in Java allows one class to inherit properties and methods from another, with the subclass extending the superclass using the 'extends' keyword. There are various types of inheritance, including single, multilevel, hierarchical, and hybrid, while multiple inheritance is not supported due to ambiguity issues like the diamond problem. Method overriding and the use of the super keyword are key concepts that facilitate polymorphism and access to superclass methods and variables.

Uploaded by

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

Unit 1 - Inheritence

Inheritance in Java allows one class to inherit properties and methods from another, with the subclass extending the superclass using the 'extends' keyword. There are various types of inheritance, including single, multilevel, hierarchical, and hybrid, while multiple inheritance is not supported due to ambiguity issues like the diamond problem. Method overriding and the use of the super keyword are key concepts that facilitate polymorphism and access to superclass methods and variables.

Uploaded by

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

Inheritance in Java Definition:

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

General format for Inheritance

class​superclass
{
// superclass data variables
// superclass member functions
}
class​subclass ​extends​superclass
{
// subclass data variables
// subclass member functions
}

● Inheritance uses the “extends” keyword to create a derived class by reusing base
class code.

Extends keyword in Java:

● 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

The different types of Inheritance are:

● 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:

2. Multiple Inheritance in Java


● Defining derived classes from numerous base classes is known as ‘Multiple
Inheritance’.
● In this case, there is more than one superclass, and there can be one or more
subclasses.
● Multiple inheritance is available in object-oriented programming with C++, but it is
not available in Java.
● Java has interface concepts expecting the developers to achieve multiple
inheritances by using multiple interfaces.

Ex: class Myclass implements interface1, interface2,….

3. Multilevel Inheritance in Java


● In Multilevel Inheritance in Java, a class extends to another class that is already
extended from another class.
● For example, if there is a class A that extends class B and class B extends from
another class C, then this scenario is known to follow Multi-level Inheritance.
4. Hierarchical Inheritance in Java
● In Hierarchical Inheritance in Java, more than one derived class extends a single
base class.

5. Hybrid Inheritance in Java:


● Hybrid Inheritance in Java is a combination of Inheritances.
● In this type of Inheritance, more than one kind of inheritance is observed.
Note: If methods and variables are called using the Parent class object, then it results in
an error.

Method Overriding in Java

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

Rules for Method overriding are:

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

Super keyword in Java

1. We can invoke the superclass variables.


2. We can invoke the superclass methods.
3. We can invoke the superclass constructor.

Example for Super Keyword in Java:

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:

Why is Multiple Inheritance not supported in Java?

● Consider a class A, class B and class C.


● Now, let class C extend class A and class B.
● Now, consider a method read() in both class A and class B.
● The method read() in class A is different from the method read() in class B.
● But, while inheritance happens, the compiler has difficulty in deciding on which
read() to inherit.
● So, in order to avoid such kind of ambiguity, multiple inheritance is not supported
in Java.

Diamond problem

● The "diamond problem" ("Deadly Diamond of Death") is an


ambiguity that arises when two classes B and C inherit from A,
and class D inherits from both B and C.
● It is called the "diamond problem" because of the shape of the
class inheritance diagram in this situation.
● Virtual inheritance solves the classic “Diamond Problem”. It
ensures that the child class gets only a single instance of the
common base class.

You might also like