0% found this document useful (0 votes)
15 views

Inheritance,Interface, Package

The document explains the concept of inheritance in Java, highlighting its importance in object-oriented programming for code reusability and extensibility. It details different types of inheritance, including single-level, multilevel, and hierarchical inheritance, while noting that Java does not support multiple or hybrid inheritance through classes but allows it via interfaces. Additionally, it covers method overriding and the use of the super keyword to access parent class methods and constructors.

Uploaded by

poojalandge1295
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Inheritance,Interface, Package

The document explains the concept of inheritance in Java, highlighting its importance in object-oriented programming for code reusability and extensibility. It details different types of inheritance, including single-level, multilevel, and hierarchical inheritance, while noting that Java does not support multiple or hybrid inheritance through classes but allows it via interfaces. Additionally, it covers method overriding and the use of the super keyword to access parent class methods and constructors.

Uploaded by

poojalandge1295
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Inheritance ,Interface

, Package
Inheritance
 Inheritance is one of the key features of OOP that allows us to create a
new class from an existing class.
 Inheritance is an object-oriented programming concept in which one class
acquires the properties and behavior of another class.
 It represents a parent-child relationship between two classes. This
parent-child relationship is also known as an IS-A relationship.
 The new class that is created is known as subclass (child or derived class)
and the existing class from where the child class is derived is known
as superclass (parent or base class).
 Need of Java Inheritance:
1. Code Reusability
2. Extensibility
3. Implantation of Method Overriding
4. Achieving Abstraction( Data abstraction is the process of hiding
certain details and showing only essential information to the user.)
How to achieve/implement inheritance in Java?

Terms and keywords generally used in inheritance are:-


 Subclass: The class that inherits the attributes and methods of
another class.
 Superclass: The class whose attributes and methods the subclass
inherits.
 Extends: The subclass uses the keyword to inherit the superclass.
 Reusability: The methods and attributes of the superclass can be
reused in the subclass because of inheritance, this is called
reusability.
Implementation of Java Inheritance

 To implement (use) inheritance in Java, the extends keyword is used. It


inherits the properties (attributes or/and methods) of the base class to
the derived class. The word "extends" means to extend functionalities
i.e., the extensibility of the features.
Super
 Syntax:- class
class Super extend extend
extend
s s
{ s
Sub class Sub class Sub class
// methods and fields
}
class Sub extends Super
{
// methods and fields of Super
class & Subclass
}
Types Of Inheritance

 In Java, there are mainly three types of inheritances1.


 1. Single level Inheritance
 2.Multilevel Inheritance
 3.Hierarchical. Inheritance
 Java does not support Multiple and Hybrid inheritance.
Single level Inheritance

 It is a child and parent class relationship where a child class extends


or inherits only from one class or superclass.
 Here only one base class and one derived class. inherits data from
only one base class to only one derived class.
Example of Single Level Inheritance
class Student class StudentDetails extends Student
{ {
String name="abc"; int marks=66;
int rollno=12; public static void main(String[] args) {
void display() Main m1=new Main();
{ m1.display();
System.out.println("Name="+name);
System.out.println("Roll no="+rollno); System.out.println("Marks="+m1.marks)
;
} }
} }

Output:-
Name=abc
Roll no=12
Marks=66
Multilevel Inheritance

 It is a child and parent class relationship where one can inherit from a
derived class, thereby making this derived class the base class for the
new class.
 The inheritance in which a base class is inherited to a derived class
and that derived class is further inherited to another derived class is
known as multi-level inheritance. Multilevel inheritance involves
multiple base classes.
class Student class Marks extends Student
{ {
String name="abc"; int m1=67;
int rollno=12; int m2=88;
void display() int m3=90;
{ void total()
{
System.out.println("Name="+name) display();
; System.out.println("Marks1=
System.out.println("Roll "+m1+"\t marks2="+m2+"\t
no="+rollno); Marks3="+m3);
} System.out.println("Total="+
} (m1+m2+m3));
}
}
class StudentDetails extends Marks
{

public static void main(String[] args) {


StudentDetails s1=new Main();
s1.total();

}
}

Output:-
Name=abc
Roll no=12
Marks1= 67 marks2=88 Marks3=90
Total=245
Hierarchical Inheritance

 The inheritance in which only one base class and multiple derived
classes is known as hierarchical inheritance.
 Hierarchical inheritance occurs when multiple subclasses inherit from
a single superclass. It allows multiple classes to share common
properties and behaviours defined in the superclass.
class shape { class Hierarchical {
float length, breadth, radius; public static void main(String[]
} args) {
class rect extends shape {
public rect(float l, float b) { rect o1 =new rect(2,5);
length = l; System.out.println("Area of
breadth = b; Rectangle :
} "+o1.rectangle_area());
float rectangle_area() {
return length * breadth; circle o2 =new circle(5);
} System.out.println("Area of
} Circle : "+ o2.circle_area());
class circle extends shape { }
public circle(float r) { }
radius = r; Output:-
} Area of Rectangle : 10.0
float circle_area() { Area of Circle : 78.5
return 3.14f * (radius *
radius);
}
}
Multiple Inheritance

 Java doesn't support multiple inheritance to avoid the 'diamond


problem', where ambiguity arises when a class inherits from two classes
with the same method names.
 n Multiple Inheritance, one child or subclass class can have more than
one base class or superclass and inherit features from every parent
class which it inherits.

 In Java, we can achieve multiple inheritances only through Interfaces.


Hybrid Inheritance
 It is a combination of two or more types of inheritance.
 Hybrid inheritance is a combination of Single and Multiple
inheritance.
 The hybrid inheritance is also not possible with classes because Java
doesn’t support multiple inheritance with classes.
 We can achieve hybrid inheritance only through Interfaces.
Method Overriding:-
 In object-oriented terms, overriding means to override the
functionality of an existing method.
 Overriding is when a child class has its method implementation for
the method already present in the parent class.
 Method overriding allows us to achieve run-time polymorphism and
is used for writing specific definitions of a subclass method that is
already defined in the superclass.
 The method is superclass and overridden method in the subclass
should have the same declaration signature such as parameters list,
type, and return type.
 Java manages Run Time Polymorphism is by method overriding.
class Animal {
public void displayInfo()
{
System.out.println("I am an animal.");
} }
class Dog extends Animal
{ @Override
public void displayInfo()
{ System.out.println("I am a dog.");
}}
class Main
Output:-I am an animal.
I am a dog..
{
public static void main(String[] args)
{Animal d1 = new Animal();
d1.displayInfo();
Dog d1 = new Dog();
d1.displayInfo();
}
}
Super keyword
 The super keyword in Java is used to refer to the immediate parent
class object. It is commonly used to access parent class methods and
constructors, enabling a subclass to inherit and reuse the
functionality of its superclass.
 The super keyword can be used in three primary contexts:
1. To call the superclass constructor.
2. To access a method from the superclass that has been overridden in
the subclass.
3. To access a field from the superclass when it is hidden by a field of
the same name in the subclass.
 Syntax
super();
super.methodName();
super.VariableName;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy