INHERITANCE IN JAVA

undefined
 
 INHERITANCE IN JAVA
 
Inheritance  in Java
 
Mechanism of deriving 
new class 
from 
old class
.
The old class is known as-
 
Base Class
 / 
Super class 
/ 
Parent Class
The new class is known as-
           
Derived class
/ 
Sub Class 
/ 
Child class
Types:
1.
 
Single Inheritance
2.
 
Multilevel Inheritance
3.
 
Multiple inheritance
4.
 
Hierarchical Inheritance
 
 
 
Inheritance In Java
A
A
B
B
 
Single inheritance
 -
When a class extends another 
one class 
only
then we call it a single inheritance.
The below flow diagram shows that class B
extends only one class which is A.
Here A is a 
parent class
 of B and B would
be  a 
child class
 of A.
 
Single Inheritance example program in Java
 
 
Class
 A
 
{
  
public
 
void
 methodA()
  
{
   
System
.
out
.println(
"Base class method"
);
  
}
 
}
 
Class
 B 
extends
 A
 
{
  
public
 
void
 methodB()
  
{
   
System
.
out
.println(
"Child class method"
);
  
}
  
public
 
static
 
void
 main(
String
 args[])
  
{
   
B obj = 
new
 B();
   
obj.methodA(); 
//calling super class method
   
obj.methodB(); 
//calling local method
  
}
 
}
 
Sub Class Constructor
 
A subclass constructor is used to construct the
instance variables of both the subclass and the
superclass.
The subclass constructor uses the keyword 
super
 
to
invoke the constructor method of superclass.
The 
super
 Keyword is used with following
Conditions
super
 may only be used within a subclass constructor.
The call to super must appear as first statement.
Parameters in the super call must match with
declaration in superclass
 
 
class
 Vehicle
 
{
  
  
Vehicle()
  
{
  
System.out.println(
"Vehicle is created"
);
  
}
 
}
 
class
 Bike 
extends
 Vehicle{
  
  
Bike()
  
{
   
   
super
();
//will invoke parent class constructor
   
   
System.out.println(
"Bike is created"
);
  
  
}
 
public
 
static
 
void
 main(String args[]){
   
 
Bike b=
new
 Bike();
 
}
 
}
 
Multiple Inheritance
 
Multiple Inheritance
” refers to the concept of one
class extending (Or inherits) 
more than one base
class
.
The problem with “multiple inheritance” is that the
derived class will have to manage the dependency
on two base classes.
A
A
B
B
C
C
 
Multilevel Inheritance
 
Multilevel inheritance
 refers to a mechanism in
OO technology where one can inherit from a derived
class, thereby making this derived class the base
class for the new class.
As you can see in below flow diagram C is subclass
or child class of B and B is a child class of A.
A
A
B
B
C
C
 
 
Class
 X
 
{
  
public
 
void
 methodX()
  
{
   
System
.
out
.println(
"Class X method"
);
  
}
 
}
 
Class
 Y 
extends
 X
 
{
  
public
 
void
 methodY()
 
  
{
   
System
.
out
.println(
"class Y method"
);
  
}
 
}
 
Class
 Z 
extends
 Y
 
{
  
public
 
void
 methodZ()
  
{
   
System
.
out
.println(
"class Z method"
);
  
}
  
public
 
static
 
void
 main(
String
 args[])
  
{
   
Z obj = 
new
 Z();
   
obj.methodX(); 
//calling grand parent class method
   
obj.methodY(); 
//calling parent class method
   
obj.methodZ(); 
//calling local method
  
}
 
}
 
Hierarchical Inheritance
 
In such kind of inheritance one class is inherited
by many
 sub classes
.
In below example class B,C and D 
inherits
 the
same class A.
A is 
parent class (or base class)
 of B,C & D.
A
A
B
B
C
C
D
D
 
 
 
Class
 A
 
{
  
public
 
void
 methodA()
  
{
  
System
.
out
.println(
"method of Class A"
);
  
}
 
}
 
Class
 B 
extends
 A
 
{
  
public
 
void
 methodB()
  
{
   
System
.
out
.println(
"method of Class B"
);
  
}
 
}
 
Class
 C 
extends
 A
 
{
  
public
 
void
 methodC()
  
{
   
System
.
out
.println(
"method of Class C"
);
  
}
 
}
 
Class
 D 
extends
 A
 
{
  
public
 
void
 methodD()
  
{
   
System
.
out
.println(
"method of Class D"
);
  
}
 
}
Class
 
MyClass
{
 
public
 
static
 
void
 main(
String
 args[])
 
{
  
B obj1 = 
new
 B();
  
C obj2 = 
new
 C();
  
D obj3 = 
new
 D();
  
obj1.methodA();
  
obj2.methodA();
  
obj3.methodA();
}}
 `
 
Overriding Methods
 
If subclass (child class) has the same method as declared
in the parent class, it is known as 
method overriding
.
In other words, If subclass provides the specific
implementation of the method that has been provided by
one of its parent class, it is known as Method Overriding.
 
Advantage of Java Method Overriding
provide specific implementation of a method that is already provided by
its super class.
 
Rules for Method Overriding
method must have same name as in the parent class
method must have same parameter as in the parent class.
must be IS-A relationship (inheritance).
 
Example
 
 
class
 Vehicle
 
{
  
void
 run()
  
{
   
System.out.println(
"Vehicle is running"
);
  
}
 
}
 
class
 Bike 
extends
 Vehicle
 
{
  
void
 run()
  
{
   
System.out.println(
"Bike is running safely"
);
  
}
  
public
 
static
 
void
 main(String args[])
  
{
   
Bike obj = 
new
 Bike();
   
obj.run();
  
}
 
}
 
 
class
 Bank{
  
int
 getRateOfInterest()
   
{
return
 
0
;}
  
}
 
 
class
 SBI 
extends
 Bank{
  
int
 getRateOfInterest()
   
{
return
 
8
;}
  
}
 
class
 ICICI 
extends
 Bank{
  
int
 getRateOfInterest()
  
{
return
 
7
;}
  
}
 
class
 Test{
   
public
 
static
 
void
 main(String args[]){
   
SBI s=
new
 SBI();
   
ICICI i=
new
 ICICI();
System.out.println(
"SBI Rate of Interest: "
+s.getRateOfInterest());
System.out.println(
"ICICI Rate of Interest: "
+i.getRateOfInterest());
}
 
 
Final variables and Methods
 
All methods and variables can be overridden by
default in subclasses.
To prevent the subclasses from overriding the
members of the superclass, declare them as final using
final
 keyword.
 
final int SIZE=10;
final void showstatus(..) {…}
 
Defining method final ensures that functionality of
defined method will not be altered.
Similarly the value of final variable never be changed.
 
Final Classes
 
A class that can not be sub-classed is called 
final
 class.
 
final class Aclass
 
{
  
 
}
 
 
final class Bclass extends Someclass
 
{
  
 
}
 
Finalizer Methods
 
Constructors are used to initialize an object when it is
declared. This process is known as “
Initialization
”.
 
Similarly, java supports a concept called 
finalization
”.
 
Java run time is automatic garbage collecting system. It
automatically frees up the memory resources used by the
objects.
 
But objects may hold other non-object resources.
 
To free these resources we must use a 
finalizer method
finalize( )
 
Abstract Methods and Classes
 
A class declared as abstract is known as 
abstract class
.
It needs to be 
extended
 and its method implemented.
It cannot be instantiated.
 
Syntax to declare the abstract class
 
abstract
 
class
 <class_name>{ }
 
Syntax to define the abstract method
 
abstract
 return_type <method_name>();
//no braces{}
 
Example
abstract
 
class
 Bike{
  
   
abstract
 
void
 run( );
}
class
 Honda 
extends
 Bike
{
 
void
 run()
 
{
 
System.out.println("running safely..");
 
}
 
public
 
static
 
void
 main(String args[])
 
{
 
  
Bike obj = 
new
 Honda();
 
  
obj.run();
 
}
}
 
//example of abstract class having constructor, field and method
 
abstract
 
class
 Bike
 
{  
 
int
 limit=30;
  
 Bike( )
  
{ System.out.println("constructor is invoked");
 
}
 
  
void
 getDetails() {
  
System.out.println("it has two wheels");
 
}
 
  
abstract
 
void
 run();
 
}
 
class
 Honda 
extends
 Bike{
 
  
void
 run(){  System.out.println("running safely..");  }
 
 
public
 
static
 
void
 main(String args[]){
 
  
 Bike obj = 
new
 Honda();
  
  
obj.run();
  
  
obj.getDetails();
 
  
System.out.println(obj.limit);  }  }
 
Methods with Varargs
 
Varargs represents variable length arguments in
methods.
It makes the code simpler and flexible.
 
<access specifier> void method-name(object…arguments)
 
In this syntax, the method contains an argument
called 
varargs ,
in which
Object
 is type of an argument.
Ellipsis (…) is the key to varargs and
arguments is the name of variable.
 
 
Thus varargs allows us to declare a method with the
unspecified number of parameters.
The varargs must be the final argument in the
argument list of a method.
Example :
 
Public void sample(String username, String password, String mailId);
 
This method can be replaced by varargs:
public void sample(String … var_name)
 
Example program for varargs
   class Exampleprg
     {
 
Exampleprg (String… person)
 
{
  
for(String name: person)
  
{
  
System.out.println(“Hello ”+ name);
  
}
 
}
 
public static void main(String args[])
 
{
  
Exampleprg(“John”, “Janny”, “Janardan”);
 
}
    }
Slide Note
Embed
Share

Mechanism of deriving new classes from old classes is explained in Java through concepts like Single Inheritance, Multilevel Inheritance, Multiple Inheritance, and Hierarchical Inheritance. The process involves creating new classes known as Derived, Sub, or Child classes from existing Base, Super, or Parent classes. Various examples and diagrams illustrate how classes extend and inherit properties in Java programming. Additionally, topics like subclass constructors, the super keyword, and the challenges of multiple inheritance are covered.

  • Java Programming
  • Inheritance Mechanism
  • Class Hierarchy
  • Subclass Constructor
  • Single Inheritance

Uploaded on Feb 25, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. INHERITANCE IN JAVA

  2. Inheritance in Java Mechanism of deriving new class from old class. The old class is known as- Base Class / Super class / Parent Class The new class is known as- Derived class/ Sub Class / Child class Types: Single Inheritance Multilevel Inheritance Multiple inheritance Hierarchical Inheritance 1. 2. 3. 4.

  3. Inheritance In Java Single inheritance - When a class extends another one class only then we call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A. A B

  4. Single Inheritance example program in Java Class A { public void methodA() { System.out.println("Base class method"); } } Class B extends A { public void methodB() { System.out.println("Child class method"); } public static void main(String args[]) { B obj = new B(); obj.methodA(); //calling super class method obj.methodB(); //calling local method } }

  5. Sub Class Constructor A subclass constructor is used to construct the instance variables of both the subclass and the superclass. The subclass constructor uses the keyword superto invoke the constructor method of superclass. The super Keyword is used with following Conditions super may only be used within a subclass constructor. The call to super must appear as first statement. Parameters in the super call must match with declaration in superclass

  6. class Vehicle { } class Bike extends Vehicle{ Bike() { super();//will invoke parent class constructor System.out.println("Bike is created"); } public static void main(String args[]){ Bike b=new Bike(); } } Vehicle() { System.out.println("Vehicle is created"); }

  7. Multiple Inheritance Multiple Inheritance refers to the concept of one class extending (Or inherits) more than one base class. The problem with multiple inheritance is that the derived class will have to manage the dependency on two base classes. A B C

  8. Multilevel Inheritance Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A. A B C

  9. Class X { } Class Y extends X { public void methodY() { } } Class Z extends Y { public void methodZ() { } public static void main(String args[]) { Z obj = new Z(); obj.methodX(); //calling grand parent class method obj.methodY(); //calling parent class method obj.methodZ(); //calling local method } } public void methodX() { System.out.println("Class X method"); } System.out.println("class Y method"); System.out.println("class Z method");

  10. Hierarchical Inheritance In such kind of inheritance one class is inherited by many sub classes. In below example class B,C and D inherits the same class A. A is parent class (or base class) of B,C & D. A B C D

  11. Class A { } Class B extends A { public void methodB() { System.out.println("method of Class B"); } } Class C extends A { public void methodC() { System.out.println("method of Class C"); } } public void methodA() { System.out.println("method of Class A"); }

  12. Class D extends A { public void methodD() { System.out.println("method of Class D"); } } Class MyClass { public static void main(String args[]) { B obj1 = new B(); C obj2 = new C(); D obj3 = new D(); obj1.methodA(); obj2.methodA(); obj3.methodA(); }} `

  13. Overriding Methods If subclass (child class) has the same method as declared in the parent class, it is known as method overriding. In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as Method Overriding. Advantage of Java Method Overriding provide specific implementation of a method that is already provided by its super class. Rules for Method Overriding method must have same name as in the parent class method must have same parameter as in the parent class. must be IS-A relationship (inheritance).

  14. Example class Vehicle { } class Bike extends Vehicle { void run() { } public static void main(String args[]) { Bike obj = new Bike(); obj.run(); } } void run() { } System.out.println("Vehicle is running"); System.out.println("Bike is running safely");

  15. class Bank{ int getRateOfInterest() {return 0;} } class SBI extends Bank{ int getRateOfInterest() {return 8;} } class ICICI extends Bank{ int getRateOfInterest() {return 7;} } System.out.println("SBI Rate of Interest: "+s.getRateOfInterest()); System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest()); } class Test{ public static void main(String args[]){ SBI s=new SBI(); ICICI i=new ICICI();

  16. Method Overloading Method Overriding 1) Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class. 2) method overloading is performed within a class. Method overriding occurs in two classes that have IS-A relationship. 3) In case of method overloading parameter must be different. In case of method overriding parameter must be same.

  17. Final variables and Methods All methods and variables can be overridden by default in subclasses. To prevent the subclasses from overriding the members of the superclass, declare them as final using final keyword. final int SIZE=10; final void showstatus(..) { } Defining method final ensures that functionality of defined method will not be altered. Similarly the value of final variable never be changed.

  18. Final Classes A class that can not be sub-classed is called final class. final class Aclass { } final class Bclass extends Someclass { }

  19. Finalizer Methods Constructors are used to initialize an object when it is declared. This process is known as Initialization . Similarly, java supports a concept called finalization . Java run time is automatic garbage collecting system. It automatically frees up the memory resources used by the objects. But objects may hold other non-object resources. To free these resources we must use a finalizer method finalize( )

  20. Abstract Methods and Classes A class declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated. Syntax to declare the abstract class abstract class <class_name>{ } Abstract Method A method that is declared as abstract and does not have implementation is known as abstract method. Syntax to define the abstract method abstract return_type <method_name>(); //no braces{}

  21. Example abstract class Bike{ } abstract void run( ); class Honda extends Bike { void run() { } System.out.println("running safely.."); public static void main(String args[]) { Bike obj = new Honda(); obj.run(); } }

  22. //example of abstract class having constructor, field and method abstract class Bike { int limit=30; Bike( ) { System.out.println("constructor is invoked"); } void getDetails() { System.out.println("it has two wheels"); } abstract void run(); } class Honda extends Bike{ void run(){ System.out.println("running safely.."); } public static void main(String args[]){ Bike obj = new Honda(); obj.run(); obj.getDetails(); System.out.println(obj.limit); } }

  23. Methods with Varargs Varargs represents variable length arguments in methods. It makes the code simpler and flexible. <access specifier> void method-name(object arguments) In this syntax, the method contains an argument called varargs ,in which Object is type of an argument. Ellipsis ( ) is the key to varargs and arguments is the name of variable.

  24. Thus varargs allows us to declare a method with the unspecified number of parameters. The varargs must be the final argument in the argument list of a method. Example : Public void sample(String username, String password, String mailId); This method can be replaced by varargs: public void sample(String var_name)

  25. Example program for varargs class Exampleprg { Exampleprg (String person) { for(String name: person) { System.out.println( Hello + name); } } public static void main(String args[]) { Exampleprg( John , Janny , Janardan ); } }

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#