0% found this document useful (0 votes)
65 views16 pages

OOP Final

The document explains inheritance in Java, a core concept of Object-Oriented Programming that allows a new class (derived or child class) to inherit properties and behaviors from an existing class (base or parent class). It discusses various types of inheritance including single, multi-level, hierarchical, multiple (via interfaces), and hybrid inheritance, along with their definitions and example code. Additionally, it highlights the benefits of inheritance such as code reusability, polymorphism, and improved code organization.
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)
65 views16 pages

OOP Final

The document explains inheritance in Java, a core concept of Object-Oriented Programming that allows a new class (derived or child class) to inherit properties and behaviors from an existing class (base or parent class). It discusses various types of inheritance including single, multi-level, hierarchical, multiple (via interfaces), and hybrid inheritance, along with their definitions and example code. Additionally, it highlights the benefits of inheritance such as code reusability, polymorphism, and improved code organization.
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

IN JAVA
Team Members…
ABHINAV PRAKASH ( 24 / IT / 008 )
ABHI ADITYA ( 24 / IT / 005 )
ADITYA KUMAR ( 24 / IT / 015 )
ABHISHEK KUMAR ( 24 / IT / 009 )
AYUSH ( 24 / IT / 064 )
WHAT IS INHERITANCE?
● Inheritance is one of the core concept of Object-Oriented Programming .
● It allows to create a new class which inherit properties and behaviors from
existing class .
● The new class also have their own unique properties.
● The new class is called "derived class" or "child class".
● The existing class is known as "base class" or "parent class".
● In Java “ extends ” keyword is used for inheritance .
USE OF INHERITANCE IN JAVA?
● Code Reusability
● Polymorphism
● Improved Code Organization
● Extensibility
● Readability & Maintainability
TYPES OF INHERITANCE
Single Inheritance
- In single inheritance, a sub-class is
derived from only one super class.
- It inherits the properties and behavior
of a single-parent class.
- It is also known as simple inheritance.
Code : Simple Inheritance

// Parent Class
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}

// Child Class
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Multi-level Inheritance
- In multi-level inheritance, a class is
derived from a class which is also derived
from another class is called multi-level
inheritance.
- In simple words, we can say that a class
that has more than one parent class is
called multi-level inheritance.
Code : Multi-level Inheritance
class Animal { // Grandparent Class
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal { // Parent Class
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog { // Child Class
void weep() {
System.out.println("Puppy is weeping");
}
}
Hierarchical Inheritance

- In hierarchical inheritance, more


than one subclass is inherited
from a single base class. i.e. more
than one derived class is created
from a single base class.
Code : Hierarchical Inheritance
class Animal { // Parent Class
void eat() {
System.out.println("Animal eats food");
}
}

class Dog extends Animal { // Child Class 1


void bark() {
System.out.println("Dog barks");
}
}

class Cat extends Animal { // Child Class 2


void meow() {
System.out.println("Cat meows");
}
}
Multiple Inheritance
- In Multiple Inheritance, one class can
have more than one superclass and
inherit features from all parent classes.
- Java does not support multiple
inheritances due to ambiguity.
- Java does not support multiple
inheritances at the class level but can be
achieved through an interface..
Code : Multiple Inheritance
interface CanRun {
void run();
}

interface CanBark {
void bark();
}

// Class implementing multiple interfaces


class Dog implements CanRun, CanBark {
public void run() {
System.out.println("Dog runs fast");
}
public void bark() {
System.out.println("Dog barks loudly");
}
}
Hybrid Inheritance
- Hybrid inheritance is a combination of
two or more types of inheritance
(single, multiple, multilevel, or
hierarchical).

- It creates a complex class hierarchy.

- In Java Hybrid inheritance through


classes is not supported But it can be
achieved using Interfaces.
Code : Hybrid Inheritance // Interface 2
interface CanBark {
void bark();
// Parent Class
}
class Animal {
void eat() {
// Child Class combining features
System.out.println("Animal eats food");
class Dog extends Animal implements CanRun, CanBark {
}
public void run() {
}
System.out.println("Dog runs fast");
}
// Interface 1
public void bark() {
interface CanRun {
System.out.println("Dog barks loudly");
void run();
}
}
}

You might also like