0% found this document useful (0 votes)
14 views5 pages

Understanding Java Inheritance Types

Inheritance is a fundamental concept in object-oriented programming that allows one class to acquire properties and methods from another class, promoting code reusability and polymorphism. There are various types of inheritance including single, multi-level, hierarchical, and hybrid, with Java not supporting multiple inheritance directly. The document also covers the use of the 'super' keyword for accessing parent class members and explains method overriding as a way to redefine parent class methods in child classes.

Uploaded by

abrarhabib75
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)
14 views5 pages

Understanding Java Inheritance Types

Inheritance is a fundamental concept in object-oriented programming that allows one class to acquire properties and methods from another class, promoting code reusability and polymorphism. There are various types of inheritance including single, multi-level, hierarchical, and hybrid, with Java not supporting multiple inheritance directly. The document also covers the use of the 'super' keyword for accessing parent class members and explains method overriding as a way to redefine parent class methods in child classes.

Uploaded by

abrarhabib75
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

Inheritance

- Acquiring the properties of one class to another is called inheritance.


- Inheritance is required to implement code reusability, IS-A relationship, and method overriding
(polymorphism).
- 'extends' keyword is used to implement inheritance.

Syntax:
class Child extends Parent
{
// body
}

- Super class = base class


- Sub class = derived class

- To access the properties of parent class, create an object only for child class.

Types of Inheritance:
1) Single level → Single parent, single child
Parent → Child

2) Multi level → One parent, multiple levels of children


Parent → Child1 → Child2 → Child3

3) Multiple Inheritance → Single child with multiple parents


(Not supported in Java directly)

4) Hybrid Inheritance → Combination of multiple and hierarchical

5) Hierarchical Inheritance → One parent, multiple children


Parent → Child1, Child2, Child3

Note:
- Object class of [Link] package is the parent class of all classes in Java.
- Java doesn’t support class-level multiple inheritance.

Example Programs:

1) Single level inheritance:

class Animal {
Animal() {
[Link]("Parent class constructor");
}
void display() {
[Link]("I am from Animal class");
}
}
class Dog extends Animal {
Dog() {
[Link]("Child class constructor");
}
void bark() {
[Link]("Dog is barking");
}
}

public class SLI_Demo {


public static void main(String[] args) {
Dog ob = new Dog();
[Link]();
[Link]();
}
}

--------------------------------------------------

2) Multi level inheritance:

class Animal {
Animal() {
[Link]("Parent class constructor");
}
void display() {
[Link]("I am from Animal class");
}
}

class Dog extends Animal {


Dog() {
[Link]("Child class constructor");
}
void bark() {
[Link]("Dog is barking");
}
}

class BabyDog extends Dog {


BabyDog() {
[Link]("Baby Dog constructor");
}
void weep() {
[Link]("I am Baby Dog");
}
}

public class MLI_Demo {


public static void main(String[] args) {
BabyDog ob = new BabyDog();
[Link]();
[Link]();
[Link]();
}
}

--------------------------------------------------

3) Hierarchical inheritance:

class Animal {
void display() {
[Link]("This is Animal class");
}
}

class Dog extends Animal {


void bark() {
[Link]("Dog is barking");
}
}

class Cat extends Animal {


void meow() {
[Link]("Cat is meowing");
}
}

public class HI_Demo {


public static void main(String[] args) {
Dog ob1 = new Dog();
Cat ob2 = new Cat();
[Link]();
[Link]();
[Link]();
}
}

--------------------------------------------------
Super Keyword:
- Super keyword is used to refer immediate parent class members (constructors, methods,
variables).

class Parent {
int m;
Parent(int z) {
m = z;
}
}

class Child extends Parent {


int m;
Child(int x, int y) {
super(x);
m = y;
}
void display() {
[Link]("m of Child: " + m);
[Link]("m of Parent: " + super.m);
}
}

public class Demo {


public static void main(String[] args) {
Child ob = new Child(100, 200);
[Link]();
}
}

--------------------------------------------------

Method Overriding:
- Two methods with same name, same type of parameters and same number of parameters, one in
parent class and other in child class, is called method overriding.
- Method overriding refers to polymorphism (runtime).
- Overriding is preferred when we are not satisfied with parent class implementation.

Example:

class Father {
void property() {
[Link]("I will give you money");
}
void marriage() {
[Link]("You marry XYZ girl");
}
}

class Son extends Father {


void marriage() {
[Link]("I will marry ABC girl");
}
}

Common questions

Powered by AI

Inheritance is significant in object-oriented programming as it allows a class to acquire properties and methods from another class, promoting code reusability and establishing an IS-A relationship. The 'extends' keyword in Java is used to facilitate inheritance by allowing one class (subclass) to inherit the fields and methods of another class (superclass). For example, in the syntax 'class Child extends Parent', the Child class inherits properties from the Parent, enabling method overriding, which supports polymorphism .

Multi-level inheritance operates by allowing a class to inherit from another class, which in turn inherits from a third class. The document provides an example with three classes: Animal, Dog, and BabyDog. Here, Dog extends Animal, and BabyDog extends Dog. This means BabyDog inherits both the properties and behaviors of Dog as well as those of Animal. For instance, when creating an object of BabyDog, it can access methods such as display() from Animal, bark() from Dog, and weep() specific to BabyDog, showcasing how multi-level inheritance facilitates structured code reuse and extensibility .

In the context of inheritance, a base class, also known as the superclass, is a class whose properties and methods are inherited by another class. Conversely, a derived class, or subclass, is a class that inherits from the superclass. The base class provides a general structure and functionality, while the derived class can either use or override this functionality to meet specific requirements. For instance, in Java, when a class Animal is extended by a class Dog, Animal is the superclass and Dog is the subclass .

The hierarchical inheritance example in the document demonstrates code reusability by allowing different subclasses (Dog and Cat) to inherit common properties and methods from a single superclass (Animal). This eliminates the need to write the same code in each subclass, as shared methods, such as display(), are inherited directly from the Animal class. As each subclass adds specific behavior, like bark() in Dog or meow() in Cat, this efficiently extends functionality while reusing established code, reducing redundancy and potential for errors .

Java supports several types of inheritance: single-level inheritance (where one class inherits from another single class), multi-level inheritance (a class inherits from a class that is already a derived class), and hierarchical inheritance (one class is inherited by multiple classes). However, Java does not directly support multiple inheritance at the class level due to potential conflict issues, such as the diamond problem. The language allows for multiple inheritance of interfaces instead, which do not carry such conflicts .

The super keyword in Java is important as it allows a subclass to access methods and constructors of its superclass, helping to avoid variable shadowing and constructor chaining issues. Within subclass constructors, super is used to call superclass constructors, ensuring the superclass is properly initialized before the subclass constructor executes its own initialization logic. For example, in 'Child extends Parent', using 'super(x)' in the Child constructor calls the Parent constructor with x as its argument, initializing Parent's fields before Child's fields .

Java does not support class-level multiple inheritance because of the diamond problem, where a class inherits the same method from multiple virtual paths, leading to ambiguity in method invocation. Instead, Java handles this by allowing multiple inheritance through interfaces. Interfaces do not have method implementations, thus avoiding conflicts as they only define method signatures without carrying state. This approach provides a workaround for implementing multiple interface inheritances without the issues caused by class-level multiple inheritance .

Method overriding demonstrates polymorphism in Java by allowing a subclass to provide a specific implementation of a method that is already defined in its superclass. This is a form of runtime polymorphism, where the method to be executed is determined at runtime. Method overriding is preferred when the behavior defined in the parent class method is not suitable for the subclass, allowing the subclass to define a more appropriate behavior, as exemplified by a Son class overriding the marriage method of its Father class .

Multiple inheritance refers to a scenario where a single class can inherit from two or more classes, leading to the possibility of inheriting multiple methods or properties with the same name, causing ambiguity or conflict. Java does not support multiple inheritance directly at the class level to avoid these issues, such as the diamond problem where a class inherits the same method or field from multiple paths. Instead, Java allows multiple inheritance of interfaces, which bypasses these conflicts since interfaces cannot contain state .

Hierarchical inheritance involves a single parent class that is extended by multiple child classes. Each child class inherits the properties and methods from the parent class. In the document, an example is given where the class Animal is the parent class, and it has two child classes: Dog and Cat. Both Dog and Cat inherit the methods of Animal, where Dog extends Animal with a bark method and Cat extends Animal with a meow method .

You might also like