0% found this document useful (0 votes)
20 views24 pages

Inheritance

Inheritance in Java allows a new class (child class) to acquire properties and behaviors from an existing class (parent class), promoting code reusability and method overriding. Java supports single, multilevel, and hierarchical inheritance but does not support multiple inheritance to avoid ambiguity. Key concepts include visibility modifiers, the super reference for accessing parent class members, and the distinction between classes and interfaces.

Uploaded by

volcanoman7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views24 pages

Inheritance

Inheritance in Java allows a new class (child class) to acquire properties and behaviors from an existing class (parent class), promoting code reusability and method overriding. Java supports single, multilevel, and hierarchical inheritance but does not support multiple inheritance to avoid ambiguity. Key concepts include visibility modifiers, the super reference for accessing parent class members, and the distinction between classes and interfaces.

Uploaded by

volcanoman7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Inheritance

Inheritance in Java is a mechanism in which one object acquires


all the properties and behavior of a 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 the parent
class.
Inheritance

• The existing class is called the parent class, or superclass, or


base class

• The new class which is derived from the existing class is called
the child class, derived class or subclass.

• The child class inherits the methods and data defined for the
parent class.
Why Inheritance?

• For Method Overriding (so runtime


polymorphism can be achieved).

• For Code Reusability.


Inheritance
To tailor a derived class, the programmer can add new variables or
methods, or can modify the inherited ones

Vehicle

Car

Inheritance should create an is-a relationship, meaning the child is a


more specific version of the parent
Deriving subclasses

The extends keyword indicates that you are making a new class that derives from
an existing class. The meaning of "extends" is to increase the functionality.

class Car extends Vehicle


{
// class contents
}
Java Inheritance Example

The relationship between the two classes is Programmer IS-


A Employee. It means that Programmer is a type of
Employee.
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
} }
Types of inheritance in java
There can be three types of inheritance in java:

• Single

• Multilevel and

• Hierarchical.

• Java does not support multiple inheritance

• Multiple inheritance allows a class to be derived from two or


more classes, inheriting the members of all parents.
Types of Inheritance
Why Multiple Inheritance is not supported in Java?

class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were

public static void main(String args[]){


C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Visibility Modifiers

Visibility modifiers determine which class members are inherited


and which are not.

Variables and methods declared with public visibility are


inherited; those with private visibility are not.

There is a third visibility modifier that helps in inheritance


situations: protected.

The protected modifier allows a member of a base class to be


inherited into a child.
The super reference
• Constructors are not inherited, even though they have public visibility.
• Yet we often want to use the parent's constructor to set up the "parent's
part" of the object
• The super reference can be used to refer to the parent class, and often is
used to invoke the parent's constructor
• A child’s constructor is responsible for calling the parent’s constructor
• The first line of a child’s constructor should use the super reference to call
the parent’s constructor
• The super reference can also be used to reference other variables and
methods defined in the parent’s class
Interface
To tie elements of different classes together Java uses an interface. The aim of interface in Java is
to dictate common behaviour among objects from diverse classes. A class that implements an
interface has to implement all the methods declared in the interface. It is used to achieve
abstraction and multiple inheritance in Java.
Interface fields are public, static and final by default, and the methods are public and abstract

• The interface keyword is used to declare an interface

interface shape
{
//body
}
Similarity b/w Class and Interface

An interface is similar to a class in the following ways −

• An interface can contain any number of methods.

• An interface is written in a file with a .java extension, with the


name of the interface matching the name of the file.

• The byte code of an interface appears in a .class file.


Class v/s Interface

However, an interface is different from a class in several ways, including −

• You cannot instantiate an interface.

• An interface does not contain any constructors.

• All of the methods in an interface are abstract.

• An interface can only contain constants.

• An interface is not extended by a class; it is implemented by a class.

• An interface can extend multiple interfaces.


Method Overriding

Declaring a method in sub class which is already present in parent


class is known as method overriding. Overriding is done so that a
child class can give its own implementation to a method which is
already provided by the parent class. In this case the method in
parent class is called overridden method and the method in child
class is called overriding method.
Rules of Method Overriding

• Argument list: The argument list of overriding method (method of child class) must
match the Overridden method(the method of parent class). The data types of the
arguments and their sequence should exactly match.

• Access Modifier of the overriding method (method of subclass) cannot be more


restrictive than the overridden method of parent class. For e.g. if the Access Modifier
of parent class method is public then the overriding method (child class method )
cannot have private, protected and default Access modifier, because all of these three
access modifiers are more restrictive than public.

• private, static and final methods cannot be overridden as they are local to the class
Method overloading v/s Method Overriding
No. Method Overloading Method Overriding
1) Method overloading is used to increase the readability of the Method overriding is used to provide the specific
program. implementation of the method that is already provided by its
super class.

2) Method overloading is performed within class. Method overriding occurs in two classes that have IS-A
(inheritance) relationship.

3) In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.

4) Method overloading is the example of compile time Method overriding is the example of run time polymorphism.
polymorphism.

5) In java, method overloading can't be performed by changing Return type must be same or covariant in method overriding.
return type of the method only. Return type can be same or
different in method overloading. But you must have to change
the parameter.
Abstract Class

Abstract Class– is the class that simply represents a concept and whose
objects cannot be created.

• An abstract class must be declared with an abstract keyword.

• It can have abstract and non-abstract methods.

• It can have constructors and static methods also.


Abstract Methods

A method which is declared as abstract and does not have implementation


is known as an abstract method.

Example of abstract method

abstract void printStatus();//no method body and abstract


Binding
Connecting a method call to the
method body is known as binding.

There are two types of binding

Static Binding (also known as Early


Binding).

Dynamic Binding (also known as


Late Binding).
Static Binding

• When type of the object is determined at compiled time(by the compiler),


it is known as static binding.

• Static binding is better performance wise (no extra overhead is required).


Compiler knows that all such methods cannot be overridden and will
always be accessed by object of local class. Hence compiler doesn’t have
any difficulty to determine object of class (local class for sure). That’s the
reason binding for such methods is static.
Dynamic binding

• When type of the object is determined at run-time, it is known as dynamic


binding.

• In Dynamic binding compiler doesn’t decide the method to be called.


Overriding is a perfect example of dynamic binding.

public static void main(String args[])


{
Vehicle vehicle = (args[0].equals("car")) ? new Car() : new Vehicle();
vehicle.start();
}
Static binding v/s Dynamic binding
Static Binding Dynamic Binding

It is a binding that happens at compile time. It is a binding that happens at run time.

It is also called early binding because binding It is also called late binding because binding happens at run
happens during compilation. time.

Method overloading is the best example of static


binding. Method overriding is the best example of dynamic binding.

Private, static and final methods show static binding. Other than private, static and final methods show dynamic
Because, they can not be overridden. binding. Because, they can be overridden.
Final Keyword

The final keyword in java is used to restrict the user. The java final keyword
can be used in many context. Final can be:
• variable
If you make any variable as final, you cannot change the value of final
variable(It will be constant).
• method
If you make any method as final, you cannot override it.
• class
If you make any class as final, you cannot extend it.

You might also like