Slidesgo Mastering Inheritance in Java A Beginners Guide With Sample Code 20241029065527TZr6
Slidesgo Mastering Inheritance in Java A Beginners Guide With Sample Code 20241029065527TZr6
Presented By :-
Monish Rai (23/CS/266)
Nikhil Kumar (23/CS/280)
Nikshay Yadav (23/CS/284)
Introduction to Inheritance
• In Java, inheritance is a fundamental concept
that allows a class to inherit properties and
methods from another class.
• This promotes code reusability and
establishes a hierarchical relationship
between classes.
• In this presentation, we will explore the
basics of inheritance and provide sample
code for better understanding.
What is
Inheritance ?
• Inheritance is a mechanism where a new class,
known as a subclass, derives from an existing
class, known as a superclass.
• The subclass inherits attributes and methods
from the superclass, allowing for extension and
specialization of functionality.
• This leads to cleaner and more organized code.
Types of
Inheritance
• Java supports several types of
inheritance: single, multilevel,
hierarchical, hybrid and multiple
inheritance (through interfaces).
• Each type serves different design
needs.
• Understanding these types is crucial
for effective class design.
• In Java, the extends keyword is used to
Using the 'extends' Keyword indicate that a class is inheriting from a
superclass.
• For example, class Dog extends Animal
signifies that Dog inherits all methods and
attributes from Animal.
• This keyword is essential for establishing
inheritance relationships.
Single Inheritance
• Single inheritance occurs when a class
inherits from one superclass. This is the
simplest form of inheritance and helps
maintain a clear class structure.
class Animal {
void eat() {
System.out.println("This animal eats food.");
} Output:
}