Unit 5 Interface2
Unit 5 Interface2
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is:
"+b.getRateOfInterest()+" %");
b=new PNB();
Abstract class having constructor,
data member and methods
• An abstract class can have a data member,
abstract method, method body (non-abstract
method), constructor, and even main()
method.
• If there is an abstract method in a class, that
class must be abstract.
• If you are extending an abstract class that has
an abstract method, you must either provide
the implementation of the method or make
this class abstract.
//Example of an abstract class that has class TestAbstraction2{
abstract and non- public static void main(String args[]){
abstract methods Bike obj = new Honda();
abstract class Bike{ obj.run();
Bike(){System.out.println("bike is cre obj.changeGear();
ated");}
}
abstract void run();
}
void changeGear(){System.out.printl
n("gear changed");} Output:-
}
//Creating a Child class which inherits bike is created
Abstract class running safely..
class Honda extends Bike{ gear changed
void run(){System.out.println("runnin
g safely..");}
}
//Creating a Test class which calls abstr
act and non-abstract methods
• Java Interface also represents the IS-A
relationship.
Why use Java interface?
Outout:--
Hello world
Multiple inheritance in Java by
interface
interface Printable{
void print();
}
interface Showable{
void show();
}
) A Java abstract class can have Members of a Java interface are public
class members like private, protected, by default.
etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Polymorphism
• Polymorphism means perform single action in
different ways.
• The word "poly" means many and "morphs"
means forms.
In Compile time Polymorphism, the call is resolved by the In Run time Polymorphism, the call is not resolved by the
compiler. compiler.
It is also known as Static binding, Early binding and It is also known as Dynamic binding, Late binding and
overloading as well. overriding as well.
Method overloading is the compile-time polymorphism Method overriding is the runtime polymorphism having the
where more than one methods share the same name with same method with same parameters or signature but
different parameters or signature and different return type. associated withcompared, different classes.
Compile time polymorphism is less flexible as all things Run time polymorphism is more flexible as all things execute
execute at compile time. at run time.