0% found this document useful (0 votes)
4 views2 pages

Inheritance Java Assignment

Inheritance in Java is an Object-Oriented Programming concept that allows one class to acquire properties and behaviors from another, promoting code reusability. There are several types of inheritance including single, multilevel, hierarchical, and hybrid, with Java avoiding multiple inheritance through classes to prevent the diamond problem. Overall, inheritance enhances code maintenance and reduces redundancy in Java programming.

Uploaded by

shalinsunil1
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)
4 views2 pages

Inheritance Java Assignment

Inheritance in Java is an Object-Oriented Programming concept that allows one class to acquire properties and behaviors from another, promoting code reusability. There are several types of inheritance including single, multilevel, hierarchical, and hybrid, with Java avoiding multiple inheritance through classes to prevent the diamond problem. Overall, inheritance enhances code maintenance and reduces redundancy in Java programming.

Uploaded by

shalinsunil1
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 - ASSIGNMENT

What is Inheritance?
Inheritance is an Object-Oriented Programming concept where one class acquires the
properties and behaviors of another class. It helps in code reusability and reduces
duplication. In Java, we use the keyword 'extends'.

1. Single Inheritance
One parent class and one child class.

class Animal {
void eat() {
[Link]("Eating");
}
}

class Dog extends Animal {


void bark() {
[Link]("Barking");
}
}

2. Multilevel Inheritance
A chain of inheritance (Grandparent -> Parent -> Child).

class Animal {
void eat() {}
}

class Dog extends Animal {


void bark() {}
}

class Puppy extends Dog {


void weep() {}
}

3. Multiple Inheritance
One child inherits from multiple parents. Java does not support this with classes but
supports it using interfaces.

interface A {
void show();
}

interface B {
void display();
}

class C implements A, B {
public void show() {}
public void display() {}
}

4. Hierarchical Inheritance
One parent class and multiple child classes.

class Animal {}

class Dog extends Animal {}


class Cat extends Animal {}
class Cow extends Animal {}

5. Hybrid Inheritance
Combination of more than one type of inheritance. In Java, this is achieved using interfaces.

6. Diamond Problem
When two parent classes have the same method, the child class gets confused about which
method to inherit. Java avoids this by not allowing multiple inheritance with classes.

Conclusion
Inheritance improves code reuse, reduces redundancy, and makes programs easier to
maintain. It is one of the most important concepts in Java OOP.

You might also like