0% found this document useful (0 votes)
0 views4 pages

inheritancein java

Inheritance in Java is an OOP concept that allows classes to acquire properties and behaviors from other classes, promoting code reusability and method overriding. There are several types of inheritance: single, multilevel, hierarchical, multiple (through interfaces), and hybrid (also through interfaces). While inheritance has advantages like reduced code duplication, it can also create tight coupling and complicate maintenance.

Uploaded by

Dr Sapna Jain
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)
0 views4 pages

inheritancein java

Inheritance in Java is an OOP concept that allows classes to acquire properties and behaviors from other classes, promoting code reusability and method overriding. There are several types of inheritance: single, multilevel, hierarchical, multiple (through interfaces), and hybrid (also through interfaces). While inheritance has advantages like reduced code duplication, it can also create tight coupling and complicate maintenance.

Uploaded by

Dr Sapna Jain
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

TOPIC : Inheritance in Java

Inheritance in Java is an Object-Oriented Programming (OOP) concept that allows one


class to acquire the properties (fields) and behaviors (methods) of another class. It promotes
code reusability, method overriding, and hierarchical classification.
Syntax
class Parent {
// Fields and methods
}

class Child extends Parent {


// Additional fields and methods
}
Example
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}

class Dog extends Animal {


void bark() {
[Link]("The dog barks.");
}
}

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
[Link](); // Inherited method
[Link](); // Child class method
}
}

TYPES OF INHERITANCE IN JAVA ARE :-


1)Single Inheritance
One child class inherits from one parent class.
class Animal {
void eat() {
[Link]("Eating");
}
}

class Dog extends Animal {


void bark() {
[Link]("Barking");
}
}
Hierarchy:
Animal
|
Dog

2. MULTILEVEL INHERITANCE
A class inherits from another class, which is itself inherited by another class.
class Animal {
void eat() {
[Link]("Eating");
}
}

class Dog extends Animal {


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

class Puppy extends Dog {


void weep() {
[Link]("Weeping");
}
}

public class Main {


public static void main(String[] args) {
Puppy p = new Puppy();
[Link]();
[Link]();
[Link]();
}
}
Hierarchy:
Animal
|
Dog
|
Puppy

3. HIERARCHICAL INHERITANCE
Multiple child classes inherit from the same parent class.
class Animal {
void eat() {
[Link]("Eating");
}
}

class Dog extends Animal {


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

class Cat extends Animal {


void meow() {
[Link]("Meowing");
}
}
Hierarchy:
Animal
/ \
Dog Cat

4. MULTIPLE INHERITANCE (THROUGH INTERFACES)


Java does not support multiple inheritance with classes because it can lead to ambiguity
(the "Diamond Problem"). However, it supports multiple inheritance through interfaces.
interface A {
void show();
}

interface B {
void display();
}

class C implements A, B {
public void show() {
[Link]("Show method");
}

public void display() {


[Link]("Display method");
}
}

public class Main {


public static void main(String[] args) {
C obj = new C();
[Link]();
[Link]();
}
}
Hierarchy:
A B
\ /
C
5. HYBRID INHERITANCE
Hybrid inheritance is a combination of two or more types of inheritance. Java does not
support hybrid inheritance with classes, but it can be achieved using interfaces.
interface Animal {
void eat();
}

interface Pet {
void play();
}

class Dog implements Animal, Pet {


public void eat() {
[Link]("Dog eats");
}

public void play() {


[Link]("Dog plays");
}
}

Advantages of Inheritance
 Promotes code reusability.
 Reduces code duplication.
 Supports method overriding and runtime polymorphism.
 Makes code easier to maintain and extend.
 Represents real-world relationships between objects.
Disadvantages of Inheritance
 Creates tight coupling between parent and child classes.
 Changes in the parent class may affect child classes.
 Deep inheritance hierarchies can make code harder to understand and maintain.
 Summary Table
Type Description Supported in Java
Single One child inherits from one parent ✅ Yes
Multilevel Child inherits from another child class ✅ Yes
Multiple children inherit from one
Hierarchical ✅ Yes
parent

Multiple One class inherits from multiple classes ❌ No (Supported through


interfaces)

Hybrid Combination of inheritance types ❌ No (Supported through


interfaces)

You might also like