Abstraction
Abstract Classes &
Interface
Abstraction
• Four pillars in Oops i.e., Abstraction, polymorphism, encapsulation and inheritance.
Abstraction is a process of hiding the implementation details and showing only
functionality to the user
Abstraction lets you focus on what the object does instead of how it does it
• There are tons of real-world examples where abstraction is in play whether smartphone
you are using or smart television you are watching all have implemented abstraction
• There are two ways to achieve abstraction in java: –
1.By using interfaces
2.By using abstract classes
Abstract Classes and Methods
In the inheritance hierarchy, classes become more specific and
concrete with each new subclass
from a subclass back up to a superclass, the classes become more
general and less specific
Sometimes a superclass is so abstract that it cannot have any
specific instances
Referred to as an
Such a class determines the nature of the methods that the
subclasses must implement
Abstract Classes and Methods
Abstract class is declared with abstract keyword
It can have abstract and non-abstract methods
Abstract Method contains no implementation, i.e. no body.
Abstract Method is created to force same name and signature
pattern in all the subclasses
Subclasses have the flexibility to code these methods with their
own specific requirements
An abstract method cannot be contained in a nonabstract class
Abstract Classes and Methods
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
When an abstract class is subclassed, the subclass usually provides implementations for
all of the abstract methods in its parent class
class Circle extends GraphicObject { class Rectangle extends GraphicObject {
void draw() { void draw() {
... ...
} }
void resize() { void resize() {
... ...
} }
} }
Abstract Classes and Methods
If a subclass of an abstract superclass does not implement all the abstract
methods, the subclass must be defined abstract
An abstract class cannot be instantiated using the new operator, but it may
contain constructors, which are invoked in the constructors of its subclasses
A class that contains abstract methods must be abstract. However, it is
possible to define an abstract class that contains no abstract methods
A subclass can be abstract even if its superclass is concrete. For example,
the Object class is concrete, but its subclasses, such as GeometricObject,
may be abstract
A subclass can override a method from its superclass to define it abstract
You cannot create an instance from an abstract class using the new
operator, but an abstract class can be used as a data type
Example
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
abstract class Bank{
Example abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInter
est()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInter
est()+" %");
}}
Interfaces
• Consider a television remote which only contains functionality to operate a
television and it doesn’t serve any other purpose besides operating the
television. Here remote acts as an interface between user and the television.
• In java Interfaces are similar to classes except they contain empty methods and
can contain variables also.
• Empty methods don’t provide any implementation details and its for the classes
or clients to provide the necessary implementation details for that method (or
methods) when they implement the interface.
Interfaces
Interfaces serve several features: –
• They provide total abstraction.
• They help to achieve what we call multiple inheritance as java doesn’t
support multiple inheritance, but you can implement several
interfaces in one class and thus it helps to achieve multiple
inheritance.
• They help to achieve loose coupling in design patterns
implementation.
Interfaces
Syntax
public interface xyz{
public void method();
}
Example :
public interface TelevisionRemote {
public void turnOnTelevision();
public void turnOffTelevision();
}
Interfaces
An interface is treated like a special class in Java. Each interface is compiled
into a separate bytecode file, just like a regular class
Can not create instance from an interface using the new operator
Can be used as a data type for a reference variable
Relationship between the class and the interface is known as interface
inheritance
A constant defined in an interface can be accessed using the syntax
InterfaceName. CONSTANT_NAME
All data fields are public final static and all methods are public abstract in an
interface, Java allows these modifiers to be omitted.
Interfaces
• A java class can just use the implements keyword to implement the interface and provide the implementation
of the methods of the interface.
Example:
public class Television implements TelevisionRemote{
public void turnOnTelevision(){
//method implementation details
}
public void turnOffTelevision(){
//method implementation details
}
}
• Interfaces provide contract specification or sets of rules for the classes implementing them.
Example
import java.util.*;
interface printable{
void print();
}
class AInterface implements printable{
public void print(){System.out.println("Hello");}
public static void main(String args[]){
AInterface obj = new AInterface();
obj.print();
}
}
The relationship between classes and interfaces
A class extends another class, an interface extends another interface, but a class
implements an interface.
Interface Example: Bank
interface Bank{
float rateOfInterest();
} Add a method loanType() in
class SBI implements Bank{ subclasses to print as
public float rateOfInterest(){return 9.15f;} education loan (ROI=9.15%)
}
class PNB implements Bank{
or housing loan (ROI=9.7%)
public float rateOfInterest(){return 9.7f;}
}
class TestInterface{
public static void main(String[] args){
Bank b=new SBI(); // Bank acts as data type
//SBI c = new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Multiple inheritance in Java by interface
• If a class implements multiple interfaces, or an interface
extends multiple interfaces, it is known as multiple inheritance.
Multiple inheritance
interface Printable{
void print();
}
interface Showable{
void show();
}
class A implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A obj = new A();
obj.print();
obj.show();
}
}
Multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there
is no ambiguity. It is because its implementation is provided by the implementation class.
Interface inheritance
• A class implements an interface, but one interface extends another interface.
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
Default Method in Interface
• we can have method body in interface. But we need to make it default method.
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
Static Method in Interface
• can have static method in interface
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
• An interface which has no member is known as a marker or tagged interface, for example, Serializable, Cloneable,
Remote, etc. They are used to provide some essential information to the JVM so that JVM may perform some useful
operation.
Nested Interface in Java
• An interface can have another interface which is known as a
nested interface. For example:
interface printable{
void print();
interface MessagePrintable{
void msg();
}
}
Interfaces vs Abstract Classes
final keyword
Use with Effect
data field It becomes constant
method Prevents overloading
class Prevents further inheritance