Programming in Java: Abstract Class and Interface
Programming in Java: Abstract Class and Interface
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.
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
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Object Class
There is one special class, called Object, defined by Java.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Methods in Object class
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)