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

Attachment (3)

Inheritance in Java allows one class to inherit properties from another, with the superclass being the parent class and the subclass being the child class. Java supports single, multilevel, hierarchical, and hybrid inheritance, but not multiple inheritance of classes, although it does allow multiple inheritance of interfaces. This mechanism promotes code reuse and organization, enabling subclasses to access and override methods from their superclasses.

Uploaded by

adityapandji1
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)
5 views9 pages

Attachment (3)

Inheritance in Java allows one class to inherit properties from another, with the superclass being the parent class and the subclass being the child class. Java supports single, multilevel, hierarchical, and hybrid inheritance, but not multiple inheritance of classes, although it does allow multiple inheritance of interfaces. This mechanism promotes code reuse and organization, enabling subclasses to access and override methods from their superclasses.

Uploaded by

adityapandji1
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
You are on page 1/ 9

JAVA Chapter 3

Notes

Inheritance in Java :

Inheritance in Java is a mechanism by which one class can inherit properties


(fields and methods) of another class. The class that is being inherited is called
the superclass or parent class, and the class that is inheriting the properties is
called the subclass or child class.

To create a subclass in Java, the "extends" keyword is used followed by the


name of the superclass. For example, the syntax to create a subclass named
"ChildClass" that inherits from a superclass named "ParentClass" would be:

java

class ChildClass extends ParentClass {

//subclass code goes here

Once the subclass is created, it can access all the public and protected methods
and fields of the superclass. The subclass can also override the methods of the
superclass if necessary. This allows the subclass to provide its own
implementation of the methods inherited from the superclass.

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


In Java, multiple inheritance is not allowed, meaning that a subclass can inherit
from only one superclass. However, a class can implement multiple interfaces,
which is a similar concept to inheritance.

In summary, inheritance is a powerful feature in Java that allows for code reuse
and promotes the concept of code organization and hierarchy.

In Java, there are five types of inheritance:

 Single Inheritance: In this type of inheritance, a subclass extends a single


superclass.
 Multilevel Inheritance: In this type of inheritance, a subclass extends
another subclass, which in turn extends a superclass.
 Hierarchical Inheritance: In this type of inheritance, multiple subclasses
inherit from a single superclass.
 Multiple Inheritance: Java does not support multiple inheritance of
classes, but it supports multiple inheritance of interfaces.
 Hybrid Inheritance: It is a combination of two or more types of
inheritance. For example, a subclass can inherit from multiple interfaces,
and the interfaces, in turn, can extend other interfaces.

It's important to note that Java only supports single inheritance of classes, but it
supports multiple inheritance of interfaces.

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


Single Level Inheritance -:

Single level inheritance in Java is a mechanism by which a subclass (also called


a derived class) is derived from a single superclass (also called a base class or
parent class). In other words, the subclass inherits the properties and behaviors
of its superclass.

To create a subclass in Java, the "extends" keyword is used, followed by the


name of the superclass. For example, if we have a superclass called "Vehicle"
and we want to create a subclass called "Car", we would use the following
syntax:

java

class Car extends Vehicle {

// subclass implementation

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


}

In this example, the subclass "Car" inherits all the properties and methods of the
superclass "Vehicle", such as its attributes (e.g. color, model, year) and its
methods (e.g. accelerate, brake, turn). The subclass can also define its own
attributes and methods, which can be used in addition to those inherited from
the superclass.

Single level inheritance provides a way to create a hierarchy of classes with


increasing levels of specialization. It allows us to reuse code by inheriting
commonly used properties and behaviors from a superclass, while still allowing
us to add new functionality in a subclass.

Example -:

// Parent class

class Animal {

public void eat() {

System.out.println("The animal is eating.");

// Child class inheriting from parent class

class Dog extends Animal {

public void bark() {

System.out.println("The dog is barking.");

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


// Main class

public class InheritanceDemo {

public static void main(String[] args) {

// Create a new Dog object

Dog myDog = new Dog();

// Call the eat() method inherited from Animal class

myDog.eat();

// Call the bark() method from Dog class

myDog.bark();

Multilevel Inheritance -:

Multi-level inheritance in Java is a type of inheritance where a class extends


another class which in turn extends another class. In other words, it involves
creating a hierarchy of classes where each class inherits properties and
behaviors from its parent class.

To implement multi-level inheritance in Java, you need to create a chain of


classes where each subsequent class inherits from the previous one. For
example, suppose you have a class called Animal that contains properties and
methods common to all animals. You can then create a subclass called Mammal

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


that extends Animal and adds additional properties and methods specific to
mammals, such as the ability to nurse their young. Finally, you can create
another subclass called Dog that extends Mammal and adds properties and
methods specific to dogs, such as the ability to bark and wag their tails.

Here is an example code implementation of multi-level inheritance in Java:

class Animal {

public void eat() {

System.out.println("Animal is eating");

class Mammal extends Animal {

public void nurseYoung() {

System.out.println("Mammal is nursing its young");

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


class Dog extends Mammal {

public void bark() {

System.out.println("Dog is barking");

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.eat(); // Output: Animal is eating

myDog.nurseYoung(); // Output: Mammal is nursing its young

myDog.bark(); // Output: Dog is barking

In this example, the Dog class inherits from the Mammal class, which in turn
inherits from the Animal class. As a result, the Dog class has access to all the
properties and methods defined in both the Mammal and Animal classes.

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


Multiple Inheritance -:

Multiple inheritance in Java refers to a feature that allows a class to inherit


properties and behaviors from more than one parent class. However, Java does
not support multiple inheritance of classes, but it supports multiple inheritance
of interfaces.

In Java, a class can implement multiple interfaces, which enables it to inherit


the abstract methods declared in each interface. To implement multiple
interfaces, you can use the implements keyword followed by a comma-
separated list of interface names.

For example:

public interface InterfaceA {

public void methodA();

public interface InterfaceB {

public void methodB();

public class MyClass implements InterfaceA, InterfaceB {

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND


public void methodA() {

// implementation

public void methodB() {

// implementation

In this example, the MyClass implements both InterfaceA and InterfaceB.


Therefore, it has to implement all the abstract methods declared in each
interface.

It is important to note that if two interfaces declare a method with the same
name and signature, the implementing class only needs to provide one
implementation for both of them.

MSBTE NEXT ICON COURSE YOUTUBE CHANNEL – UR ENGINEERING FRIEND

You might also like