Inheritance
Inheritance
Output:
weeping...
barking...
eating...
Hierarchical Inheritance
Example
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...");}
}
Hierarchical Inheritance
Example
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing...
eating...
Method Overriding in
Java
• If subclass (child class) has the same
method as declared in the parent class, it is
known as method overriding in java.
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
Example of method
overriding
class Bike2 extends Vehicle{
void run(){System.out.println("Bike is running
safely");}
}
}
super : used to refer immediate
parent class method.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work()
class TestSuper2{
{ public static void main(String args[]){
super.eat(); Dog d=new Dog();
bark(); d.work();
}}
}
} Output: eating...
barking...
super : used to invoke immediate
parent class conctructor.
class Animal{
Animal(){System.out.println("animal is created");}
} class TestSuper3{
public static void main(String args[]){
• variable
• method
• class
Final Keyword In Java
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
• method
• Class
The final keyword can be applied with the variables, a final variable that
have no value it is called blank final variable or uninitialized final variable.
It can be initialized in the constructor only. The blank final variable can be
static also which will be initialized in the static block only.
Java final variable
• If you make any variable as final, you cannot
change the value of final variable(It will be
constant).
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class Output:Compile Time Error
Java final method
• If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}
class A{}
class B extends A{}
A a=new B();//upcasting
Dynamic Method
Dispatch Example
class Bank{
float getRateOfInterest(){return 0;}
}
class SBI extends Bank{
float getRateOfInterest(){return 8.4f;}
}
class ICICI extends Bank{
float getRateOfInterest(){return 7.3f;}
}
class AXIS extends Bank{
float getRateOfInterest(){return 9.7f;}
}
Dynamic Method
Dispatch Example
class TestPolymorphism{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("SBI Rate of Interest: "+b.getRateOfInterest());
b=new ICICI();
System.out.println("ICICI Rate of Interest: "+b.getRateOfInterest());
b=new AXIS();
System.out.println("AXIS Rate of Interest: "+b.getRateOfInterest());
}
}
Abstractionin Java
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
Ways to achieve Abstaction
There are two ways to achieve abstraction in
java
• Abstract class (0 to 100%)
• Interface (100%)
Abstract class in Java
• A class that is declared with abstract
keyword, is known as abstract class in java.
• It cannot be instantiated.
Abstract class in Java
abstract class Shape{
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unk
//nown by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
Abstract class in Java
//In real scenario, method is called by programmer or u
//ser
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();//In real scenario, object is provi
//ded through method e.g. getShape() method
s.draw();
}
}
Interface in Java
• 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 method body.
• It is used to achieve abstraction and multiple
inheritance in Java.
• Java Interface also represents IS-A relationship.
• It cannot be instantiated just like abstract class.
Why use Java interface?
There are mainly three reasons to use
interface. They are given below.
interface printable{
void print();
}
class A implements printable{
public void print(){System.out.println("Hello");}
Output :
4) Abstract class can provide the Interface can't provide the implementation
implementation of interface. of abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }