0% found this document useful (0 votes)
5 views8 pages

Inheritance in Java

Inheritance in Java allows one class to acquire properties and behaviors from another, promoting code reusability and establishing a parent-child relationship. Java supports five types of inheritance: single, multilevel, hierarchical, multiple (via interfaces), and hybrid. Key points include using 'extends' for class inheritance and 'implements' for interface inheritance, with the restriction that multiple inheritance is not supported with classes.

Uploaded by

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

Inheritance in Java

Inheritance in Java allows one class to acquire properties and behaviors from another, promoting code reusability and establishing a parent-child relationship. Java supports five types of inheritance: single, multilevel, hierarchical, multiple (via interfaces), and hybrid. Key points include using 'extends' for class inheritance and 'implements' for interface inheritance, with the restriction that multiple inheritance is not supported with classes.

Uploaded by

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

Inheritance in Java

Definition:
Inheritance is the mechanism by which one class acquires the properties (fields) and behaviors
(methods) of another class.
It provides code reusability and establishes a parent-child relationship between classes.

Syntax:

class Parent {

// properties and methods

class Child extends Parent {

// additional properties and methods

Example of Inheritance

class Animal {

void eat() {

System.out.println("This animal eats food");

class Dog extends Animal {

void bark() {

System.out.println("Dog barks");

}
}

public class Test {

public static void main(String[] args) {

Dog d = new Dog();

d.eat(); // inherited method

d.bark(); // Dog class method

Types of Inheritance in Java

Java supports 5 types of inheritance:

Type Supported in Java Description

1. Single Inheritance ✅ Yes One class inherits from another class

2. Multilevel Inheritance ✅ Yes A class inherits from a derived class

Multiple classes inherit from a single


3. Hierarchical Inheritance ✅ Yes
class

4. Multiple Inheritance (by


✅ Yes A class implements multiple interfaces
Interface)

✅ Yes (with
5. Hybrid Inheritance Combination of two or more types
Interface)

1. Single Inheritance

class A {

void show() {

System.out.println("Class A method");
}

class B extends A {

void display() {

System.out.println("Class B method");

class Test {

public static void main(String args[]) {

B obj = new B();

obj.show();

obj.display();

2. Multilevel Inheritance

class A {

void show() {

System.out.println("Class A");

}
class B extends A {

void display() {

System.out.println("Class B");

class C extends B {

void print() {

System.out.println("Class C");

class Test {

public static void main(String args[]) {

C obj = new C();

obj.show(); // From A

obj.display(); // From B

obj.print(); // From C

3.Hierarchical Inheritance

class Parent {

void parentMethod() {
System.out.println("Parent class method");

class Child1 extends Parent {

void child1Method() {

System.out.println("Child1 class method");

class Child2 extends Parent {

void child2Method() {

System.out.println("Child2 class method");

class Test {

public static void main(String args[]) {

Child1 c1 = new Child1();

c1.parentMethod();

c1.child1Method();

Child2 c2 = new Child2();

c2.parentMethod();
c2.child2Method();

4. Multiple Inheritance using Interface

Java does not support multiple inheritance with classes (to avoid ambiguity), but supports it
via interfaces.

interface Printable {

void print();

interface Showable {

void show();

class A implements Printable, Showable {

public void print() {

System.out.println("Print method");

public void show() {

System.out.println("Show method");

}
class Test {

public static void main(String args[]) {

A obj = new A();

obj.print();

obj.show();

5. Hybrid Inheritance

Combination of more than one type, achieved through interfaces.

interface A {

void methodA();

interface B {

void methodB();

class C implements A, B {

public void methodA() {

System.out.println("A's method");

public void methodB() {


System.out.println("B's method");

class Test {

public static void main(String[] args) {

C obj = new C();

obj.methodA();

obj.methodB();

✅ Key Points of Inheritance in Java

 Use extends keyword for class inheritance.

 Use implements for interface inheritance.

 Java does not support multiple inheritance with classes, only through interfaces.

 Constructor is not inherited, but the parent constructor is implicitly called in child.

 Private members of parent class are not accessible directly in child class.

You might also like