JavaChap3 Inheritance
JavaChap3 Inheritance
Chapter 3
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
• The idea behind inheritance in Java is that we can
create new classes that are built upon existing classes
• When we inherit methods from an existing class, we can
reuse methods and fields of the parent class.
• However, we can add new methods and fields in your
current class also
What is Inheritance?
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
//suppose if it were
class C extends A,B{
public static void main(String args[]){
C obj=new C();
obj.msg(); //Now which msg() method would be invoked?
}
}
How to achieve Multiple Inheritance in Java?
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...");}
}
public class Main{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Hierarchical Inheritance
• When two or more classes inherits a single class, it is known as hierarchical inheritance.
class Animal{
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...");}
}
public class Main{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Super Keyword
OUTPUT :
animal is created
dog is created
Superclass References and Subclass Objects
// If a Parent type reference refers to a Child object, Child's show() is called. This is called RUN TIME
POLYMORPHISM.
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
Abstract Class having constructor, data member,
and methods
An abstract class can have
• data member
• abstract method
• method body (non-abstract method)
• constructor
• main() method.
abstract class Subject {
//constructor
Subject() {
System.out.println("Learning Subject");
}
//Abstract method
abstract void syllabus();
//non abstract method
void Learn(){
System.out.println("Preparing Right Now!");
}
}
class IT extends Subject {
void syllabus(){
System.out.println("C , Java , C++");
}
}
class Test {
public static void main(String[] args) {
Subject x=new IT();
x.syllabus();
x.Learn();
}
}
using final
• The final Keyword in Java is used as a non-access modifier applicable only to
a variable, a method, or a class.
• It is used to restrict a user in Java.
The following are different contexts where the final is
used:
1.Variable 🡪 Used to create Constant variable
2.Method 🡪 Used to prevent method overriding
3.Class 🡪 Used to prevent inheritance or subclassing
using final 🡪 Prevent Overriding
abstract class Shape // getHeight method is declared as final so any class extending
{ Shape can not override it
private double width; public final double getHeight()
{
private double height; return height;
// Shape class parameterized constructor }
public Shape(double width, double height) // method getArea() declared abstract because it upon its
{ subclasses to provide complete implementation
abstract double getArea();
this.width = width; }
this.height = height;
}
// getWidth method is declared as final so
any class extending Shape can't override it
public final double getWidth()
{
return width;
class Rectangle extends Shape //derived class two
{ class Square extends Shape
// Rectangle class parameterized constructor {
public Rectangle(double width, double height) // Square class parameterized constructor
{ public Square(double side)
// calling Shape class constructor {
super(width, height); // calling Shape class constructor
} super(side, side);
}
// getArea method is overridden and declared
// as final so any class extending Rectangle can't // getArea method is overridden and declared as
override it // final so any class extending
@Override // Square can't override it
final double getArea() @Override
{ final double getArea()
return this.getHeight() * this.getWidth(); {
} return this.getHeight() * this.getWidth();
}
}
}
// Driver class
public class Test
{
public static void main(String[] args)
{
// creating Rectangle object
Shape s1 = new Rectangle(10, 20);
//getting area of s1
System.out.println("area of s1 : "+ s1.getArea());
//getting area of s2
System.out.println("area of s2 : "+ s2.getArea());
}
}
using final 🡪 Prevent Inheritance
final class FinalClass {
void display(){
System.out.println(“Inside FinalClass");
}
}
public final Class getClass() returns the Class class object of this object.
The Class class can further be used to get
the metadata of this class.
protected Object clone() throws creates and returns the exact copy (clone)
CloneNotSupportedException of this object.
public final void notifyAll() wakes up all the threads, waiting on this
object's monitor.
Methods of Object class
Method Description
public final void wait(long timeout)throws causes the current thread to wait for the
InterruptedException specified milliseconds, until another thread
notifies (invokes notify() or notifyAll()
method).
public final void wait(long timeout,int causes the current thread to wait for the
nanos)throws InterruptedException specified milliseconds and nanoseconds,
until another thread notifies (invokes
notify() or notifyAll() method).
public final void wait()throws causes the current thread to wait, until
InterruptedException another thread notifies (invokes notify() or
notifyAll() method).
protected void finalize()throws Throwable is invoked by the garbage collector before
object is being garbage collected.