What is Inheritance?
Inheritance in Java enables a class to inherit properties and actions from another class, called a superclass or
parent class. A class derived from a superclass is called a subclass or child class. Through inheritance, a subclass
can access members of its superclass (fields and methods), enforce reuse rules, and encourage hierarchy.
Inheritance is used for:
• For Runtime Polymorphism.
• For Code Reusability.
Syntax:
class Subclass-name extends Superclass-name
{
//methods and fields
}
3.
Types of Inheritance
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.
4.
Types of Inheritance
In java, Multiple and Hybrid inheritance can be implemented by interface only.
5.
// Parent class
classAnimal {
void eat( ) {
System.out.println("Animals can eat");
}
}
// Child class
class Dog extends Animal {
void bark( ) {
System.out.println("Dog can bark");
}
}
// Main class
public class SingleInheritanceExample {
public static void main(String[] args) {
Dog d = new Dog( );
d.eat( ); // inherited method
d.bark( ); // own method
}
}
Single Inheritance
class Animal {
void eat() {
System.out.println("Animals can eat");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog can bark");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy can weep");
}
}
public class MultilevelInheritanceExample {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat(); // from Animal
p.bark(); // from Dog
p.weep(); // from Puppy
}
}
Multi-level Inheritance
6.
Hierarchical Inheritance
class Animal{
void eat() {
System.out.println("Animals can eat");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog can bark");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat can meow");
}
}
public class HierarchicalInheritanceExample {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
d.bark();
c.eat();
c.meow();
}
}
7.
interface Animal {
voideat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
System.out.println("Dog eats food");
}
public void play() {
System.out.println("Dog loves to play");
}
}
public class MultipleInheritanceExample {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.play();
}
}
An interface in Java is a blueprint of a class. It has
static constants and abstract methods.
The interface in Java is a mechanism to achieve
abstraction. There can be only abstract methods in
the Java interface, not a method body. It is used to
achieve abstraction and multiple inheritance in Java.
Multiple Inheritance
8.
interface A {
voidshowA();
}
interface B extends A {
void showB();
}
class C {
void showC() {
System.out.println("Class C method");
}
}
class D extends C implements B {
public void showA() {
System.out.println("Interface A method");
}
public void showB() {
System.out.println("Interface B method");
}
}
public class HybridInheritanceExample {
public static void main(String[] args) {
D obj = new D();
obj.showA();
obj.showB();
obj.showC();
}
}
Hybrid Inheritance
9.
Polymorphism-Method Overloading inJava
Method overloading in Java is the feature that enables defining more than one method in a class
having the same name but with different types and number of parameters. When a method is called,
Java decides which version of it to execute depending on the arguments given. If we have to
perform only one operation, having the same name of the methods increases the readability of
the program.
Approaches to Method Overloading
There are the following two ways to overload a method in Java:
1. By Changing the Number of Arguments
2. By Changing the Data Type of Arguments
In Java, method overloading is not possible by changing the return type of the method only.
10.
Key Points:
•Method name:same
•Parameter list: different
•Return type: can be same or different (but not used for
overloading)
•Decided at compile time
class Calculator {
// Method 1: adds two integers
int add(int a, int b) {
return a + b;
}
// Method 2: adds three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Method 3: adds two double numbers
double add(double a, double b) {
return a + b;
}
}
public class MethodOverloadingExample {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println("Sum of two integers: " + calc.add(10, 20));
System.out.println("Sum of three integers: " + calc.add(10, 20, 30));
System.out.println("Sum of two doubles: " + calc.add(5.5, 2.3));
}
}
11.
Polymorphism-Method Overriding inJava
If subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
Method overriding is used to provide the specific implementation of a method that is already
provided by its superclass.
Method overriding is used for runtime polymorphism.
Method overriding allows subclasses to reuse and build upon the functionality provided by their
superclass, reducing redundancy and promoting modular code design.
Subclasses can override methods to tailor them to their specific needs or to implement specialized
behavior that is unique to the subclass.
Method overriding enables dynamic method dispatch, where the actual method implementation
to be executed is determined at runtime based on the type of object, supporting flexibility and
polymorphic behavior.
12.
Rules for JavaMethod Overriding
Same Method Name: The overriding method in the subclass must have the same name as the method in the superclass that it is
overriding.
Same Parameters: The overriding method must have the same number and types of parameters as the method in the superclass.
This ensures compatibility and consistency with the method signature defined in the superclass.
IS-A Relationship (Inheritance): Method overriding requires an IS-A relationship between the subclass and the superclass. This
means that the subclass must inherit from the superclass, either directly or indirectly, to override its methods.
Same Return Type or Covariant Return Type: The return type of the overriding method can be the same as the return type of the
overridden method in the superclass, or it can be a subtype of the return type in the superclass. This is known as the covariant
return type, introduced in Java 5.
Access Modifier Restrictions: The access modifier of the overriding method must be the same as or less restrictive than the access
modifier of the overridden method in the superclass. Specifically, a method declared as public in the superclass can be overridden
as public or protected but not as private. Similarly, a method declared as protected in the superclass can be overridden as protected
or public but not as private. A method declared as default (package-private) in the superclass can be overridden with default,
protected, or public, but not as private.
No Final Methods: Methods declared as final in the superclass cannot be overridden in the subclass. Because final methods cannot
be modified or extended.
No Static Methods: Static methods in Java are resolved at compile time and cannot be overridden. Instead, they are hidden in the
subclass if a method with the same signature is defined in the subclass.
13.
//Java Program toillustrate the use of Java Method
Overriding
//Creating a parent class.
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}
}
//Creating a Main class to create object and call method
public class Main{
public static void main(String args[]){
Bike obj = new Bike();//creating object
obj.run();//calling method
}
}
Method Overriding