Inhertance in Java
Inhertance in Java
JAVA
Nipun Lingadally-22211A05L5
Agenda
INTRODUCTION
TYPES OF INHERITANCE
SINGLE INHERITANCE
MULTI-LEVEL INHERITANCE
HIERARCHICAL INHERITANCE
Introduction
Inheritance in Java is a mechanism in which
one object acquires all the properties and
behaviors of a parent object. It is an important
part of OOPs (Object Oriented programming
system).
Hierarchical
Single Multi-level
Inheritance
Inheritance Inheritance
Multiple subclasses
A subclass inherits from A subclass inherits from
inherit from a single
only one superclass. another subclass.
superclass.
Definition
When a class inherits another class, it is known as a
single inheritance. In the example given below, Dog class
inherits the Animal class, so there is the single
inheritance.
Single Syntax
class Super {
Inheritance }
// properties and methods
Multi-level Syntax
class Super1 {
Inheritance }
// properties and methods
Hierarchical Syntax
class Super {
// properties and methods
Inheritance }
class Sub1 extends Super {
// properties and methods
}
class Sub2 extends Super {
// properties and methods
}
Example OUTPUT:
eating....
barking....
class Animal{
meowing....
void eat(){
System.out.println("eating...");
} }
class Dog extends Animal{
void bark(){
System.out.println("barking...");
} }
class Cat extends Animal{
void meow(){
System.out.println("meowing...");
} }
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
}}
Thank You