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

Inheritance in Java

Inheritance in Java allows one class to inherit properties and methods from another class, similar to how children inherit traits from parents. There are several types of inheritance: single, multilevel, hierarchical, multiple (using interfaces), and hybrid (a combination of types). Each type has specific structures and examples demonstrating how classes can be related through inheritance.
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)
2 views8 pages

Inheritance in Java

Inheritance in Java allows one class to inherit properties and methods from another class, similar to how children inherit traits from parents. There are several types of inheritance: single, multilevel, hierarchical, multiple (using interfaces), and hybrid (a combination of types). Each type has specific structures and examples demonstrating how classes can be related through inheritance.
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

Inheritance in Java

Inheritance in Java means:

One class can use the properties and methods of another class.

It is like children inheriting qualities from parents.

For example:

 A child gets eyes, hair color, or habits from parents.


 In Java, a new class gets variables and methods from an existing class.

1. Single Inheritance
One child class inherits from one parent class.

Example
class Animal {

void eat() {}

class Dog extends Animal {

void bark() {}

2. Multilevel Inheritance
A class inherits from another class, and then another class inherits from it.

Example
class Animal {

void eat() {}

class Dog extends Animal {

void bark() {}

}
class Puppy extends Dog {

void weep() {}

3. Hierarchical Inheritance
Multiple child classes inherit from one parent class.

Example
class Animal {

void eat() {}

class Dog extends Animal {

void bark() {}

class Cat extends Animal {

void meow() {}

}
4. Multiple Inheritance
One child class inherits from multiple parent classes.

Example

interface A {
void show();
}

interface B {
void display();
}

class Test implements A, B {

public void show() {


[Link]("Show");
}

public void display() {


[Link]("Display");
}
}

More Example

1. Single Inheritance
One child class inherits from one parent class.

Example: Addition and Multiplication

class Addition {

int a = 10, b = 5;

void add() {
[Link]("Addition = " + (a + b));
}
}
class Multiplication extends Addition {

void multiply() {
[Link]("Multiplication = " + (a * b));
}
}

public class Main {

public static void main(String[] args) {

Multiplication m = new Multiplication();

[Link]();
[Link]();
}
}

2. Multilevel Inheritance
One class inherits another class, and another class inherits from it.

Example: Addition → Subtraction → Multiplication


class Addition {

int a = 20, b = 10;

void add() {
[Link]("Addition = " + (a + b));
}
}

class Subtraction extends Addition {

void subtract() {
[Link]("Subtraction = " + (a - b));
}
}

class Multiplication extends Subtraction {

void multiply() {
[Link]("Multiplication = " + (a * b));
}
}
public class Main {

public static void main(String[] args) {

Multiplication m = new Multiplication();

[Link]();
[Link]();
[Link]();
}
}

3. Hierarchical Inheritance
Multiple child classes inherit from one parent class.

Example: Addition shared by Subtraction and Division

class Calculation {

int a = 20, b = 5;

void add() {
[Link]("Addition = " + (a + b));
}
}

class Subtraction extends Calculation {

void subtract() {
[Link]("Subtraction = " + (a - b));
}
}

class Division extends Calculation {

void divide() {
[Link]("Division = " + (a / b));
}
}

public class Main {

public static void main(String[] args) {


Subtraction s = new Subtraction();
[Link]();
[Link]();

Division d = new Division();


[Link]();
[Link]();
}
}

4. Multiple Inheritance (Using Interfaces)


Java does not support multiple inheritance with classes, but it supports it using interfaces.

Example: Addition and Multiplication Interfaces


interface Addition {

int a = 10;
int b = 5;

void add();
}

interface Multiplication {

void multiply();
}

class Calculation implements Addition, Multiplication {

public void add() {


[Link]("Addition = " + (a + b));
}

public void multiply() {


[Link]("Multiplication = " + (a * b));
}
}

public class Main {


public static void main(String[] args) {
Calculation c = new Calculation();
[Link]();
[Link]();
}
}
5. Hybrid Inheritance (Using Interfaces)
Hybrid inheritance is a combination of inheritance types.

Example: Interface + Class Inheritance

interface Addition {

void add();
}

class Subtraction {

int a = 20, b = 10;

void subtract() {
[Link]("Subtraction = " + (a - b));
}
}

class Calculation extends Subtraction implements Addition {

public void add() {


[Link]("Addition = " + (a + b));
}

void multiply() {
[Link]("Multiplication = " + (a * b));
}
}

public class Main {

public static void main(String[] args) {

Calculation c = new Calculation();


[Link]();
[Link]();
[Link]();
}
}
Type Meaning
Multilevel Inheritance Parent → Child → Grandchild
Single Inheritance One parent → One child
Hierarchical Inheritance One parent → Many children
Multiple Inheritance Many parents → One child
Hybrid Inheritance Combination of inheritance types

You might also like