3. Inheritance is a fundamental concept in object-oriented programming and is supported in Java.
Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new
methods and fields also.
Syntax:
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you are making a new class that derives from an existing class.
In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.
3
4. Inheritance
Eg:
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle(subclass) extends Shape (superclass)
{
void draw() {
System.out.println("Drawing a circle");
}
}
In this example, the Shape class is the base class, and the Circle
class is the derived class. The Circle class extends the Shape
class, which means that it inherits the draw method from the
Shape class. However, the Circle class also provides its own
implementation of the draw method, which overrides the
implementation provided by the Shape class.
4
5. // inheritance
class Employee
{
float salary=40000;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
} }
Programmer salary is:40000.0
Bonus of programmer is:10000
5
The programmer class inherits the salary variable from the employee class and has its
own bonus variable. When you create a programmer object it has access to both the
salary inherited from employee and its own bonus.
6. On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only.
Types of inheritance in java
6
7. When a class extends multiple classes i.e. known as multiple inheritance which is accomplished using ‘interface’.
7
8. 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 Inheritance
class Animal{
void eat()
{System.out.println("eating...");}
}
class Dog extends Animal{
void bark()
{System.out.println("barking...");}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}
Output:
barking...
eating...
8
9. Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class,
so there is a multilevel inheritance.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating...
9
10. 10
Hierarchical inheritance
Hierarchical inheritance in Java is similar to the concept of hierarchical inheritance in object-oriented programming. In
Java, a class can extend only one parent class, but multiple sub-classes can extend the same parent class, resulting in a
hierarchical inheritance structure.
Here is an example of hierarchical inheritance in Java:
// Base class
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
// Sub-class 1
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
11. 11
// Sub-class 2
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
// Main class
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.bark(); // Specific to Dog
Cat cat = new Cat();
cat.eat(); // Inherited from Animal
cat.meow(); // Specific to Cat
}
}
In this example, the Animal class is the base class and contains
a method called eat(). The Dog and Cat classes are sub-classes
of Animal and inherit the eat() method. Additionally, Dog has a
specific method called bark() and Cat has a specific method
called meow(). When we create instances of Dog and Cat
classes, we can call both the inherited eat() method and their
respective specific methods (bark() and meow()).
Output:
Animal is eating // dog.eat() - Inherited method from Animal
Dog is barking // dog.bark() - Specific to Dog
Animal is eating // cat.eat() - Inherited method from Animal
Cat is meowing // cat.meow() - Specific to Cat
13. An interface in java is a blueprint of a class. It has static constants and abstract
methods only.
There are mainly three reasons to use interface. They are given below.
• It is used to achieve fully abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
13
15. 15
loose coupling refers to a design principle where classes and objects are designed in a way that they are
independent and don't rely too much on each other.
This means that changes made to one class or object won't have a significant impact on other classes or
objects in the system.
16. interface Callback
{
void callback(int param);
}
A class can extend another class and implement the interfaces also ,to achieve
multiple interface.
Ex: class A extends B implements interface1,interface2
One interface can extend another interface.
Ex: interface inf1 extends inf2
16
17. Implementing Interfaces
• Once an interface has been defined, one or more classes can implement
that interface.
• To implement an interface, include the implements clause in a class
definition, and then create the methods defined by the interface.
• The general form of a class that includes the implements clause looks like
this:
• class classname [extends superclass] [implements interface [,interface...]] {
// class-body }
• This supports multiple inheritance . since it is acquiring the properties from
the parent class and interface .
17
To implement an interface, you must create a class that implements that interface. To do this, use the
"implements" keyword followed by the interface name after the class declaration.
18. public class MyClass implements MyInterface {
// implementation of the abstract methods from the interface
public void method1() {
// implementation code goes here
}
public void method2() {
// implementation code goes here
}
}