Programming in Java
Abstract class and Interface
Contents
Abstract Class
Object Class
Abstraction in java
Abstraction is a process of hiding the implementation details
and showing only functionality to the user.
Another way, it shows only essential things to the user and
hides the internal details, for example, sending SMS where you
type the text and send the message. You don't know the
internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of
how it does it.
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
Abstract class
Interface
Abstract Class
• An abstract class is a class that is declared abstract.
• An abstract method is a method that is declared without an
implementation (without braces, and followed by a semicolon),
like this:
abstract void moveTo(double deltaX, double deltaY);
• Abstract class may or may not include abstract methods.
• Abstract classes cannot be instantiated, but they can be sub-
classed.
• If a class includes abstract methods, the class itself must be
declared abstract, as in:
public abstract class GraphicObject
{ // declare fields
// declare non-abstract methods
abstract void draw();
}
A class which is declared as abstract is known as an abstract
class. It can have abstract and non-abstract methods. It needs
to be extended and its method implemented. It cannot be
instantiated.
An abstract class must be declared with an abstract
keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the
subclass not to change the body of the method.
Example of Abstract class
abstract class A{}
Example of Abstract Method
abstract void printStatus();//no method body and abstract
Abstract class that has an abstract
method
1.abstract class Bike{
2. abstract void run();
3. }
4.class Honda4 extends Bike{
5.void run(){System.out.println("running safely
");}
6.public static void main(String args[]){
7. Bike obj = new Honda4();
8. obj.run();
9. }
10.}
Running safely
/ An abstract class with constructor
abstract class Base {
Base() { System.out.println("Base Constructor Called"); }
abstract void fun();
}
class Derived extends Base {
Derived() { System.out.println("Derived Constructor Called"); }
void fun() { System.out.println("Derived fun() called"); }
}
class Main {
public static void main(String args[]) {
Derived d = new Derived();
d.fun();
}
}
abstract class Shape{
abstract void draw();
}
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();
s.draw();
}
}
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and non-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Interfaces
Interfaces
• An interface is a collection of abstract methods.
• An interface is not a class.
• A class describes the attributes and behaviors of an object
whereas an interface contains behaviors that a class
implements.
• A class implements an interface, thereby inheriting the
abstract methods of the interface.
• Unless the class that implements the interface is abstract,
all the methods of the interface need to be defined in the
class.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Properties of Interfaces
The interface keyword is used to declare an interface.
Interfaces have the following properties:
An interface is implicitly abstract. We do not need to use the
abstract keyword when declaring an interface.
Each method in an interface is also implicitly public and
abstract, so the abstract keyword is not needed.
Each variable in an interface is implicitly public, static and
final.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Interface Vs Class
An interface is similar to a class in the following ways:
• An interface can contain any number of methods.
• An interface is written in a file with a .java extension, with the
name of the interface matching the name of the file.
• The byte-code of an interface appears in a .class file.
• Interfaces appear in packages, and their corresponding byte-
code file must be in a directory structure that matches the
package name.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Interface Vs Class
An interface is different from a class in several ways,
including:
• We cannot instantiate an interface.
• An interface does not contain any constructors.
• All of the methods in an interface are abstract.
• An interface is not extended by a class; it is implemented by a
class.
• An interface can extend multiple interfaces.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Implementing Interfaces
When a class implements an interface, then it has to
perform the specific behaviors of the interface.
If a class does not perform all the behaviors of the
interface, the class must declare itself as abstract.
A class uses the implements keyword to implement an
interface.
The implements keyword appears in the class declaration
following the extends portion of the declaration.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
interface Animal
{
void eat();
public void travel();
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
public class Mammal implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public int noOfLegs()
{ return 0; }
public static void main(String args[])
{
Mammal m = new Mammal();
m.eat();
m.travel();
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Abstract Class Vs Interfaces
Abstract Class Interface
May contain non-abstract methods Contains only method declaration and
and non-static non final data members static final data members
Multiple Inheritance is not supported Multiple Inheritance through
through classes Interfaces is supported
Classes provide static classing Interfaces provide dynamic classing
environment environment
Inheritance using ‘extends’ keyword Inheritance using ‘implements’
keyword
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Object Class
There is one special class, called Object, defined by Java.
All other classes are subclasses of Object.
A reference variable of type Object can refer to an object of any other
class.
Arrays are implemented as classes, so a variable of type Object can also
refer to any array.
In Object class, getClass( ), notify( ), notifyAll( ), and wait( ) are
declared as final.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods in Object class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)