Abstraction
Showing only the functionalities in more generalized manner is called as abstraction. In java we can achieved this either by implementing an Interface or an abstract class. Interface provides 100% abstraction while Abstract class can provide a partial abstraction in between 0% to 100%.
Abstract Class
If a class is declared using the key word "abstract" then we called it as an abstract class. We can't instantiated an abstract class. (Can't make objects) but it can have sub classes.
Code:
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
public class TestAbstractClass{
public static void main(String args[]){
Student s1= new Student("John", 18);
s1.eat();
s1.sleep();
s1.eat();
s1.sleep();
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
abstract class Person{
String name;
int age;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
this.age=age;
}
void eat(){
System.out.println(this.name+":"+"Eating...");
}
void sleep(){
System.out.println(this.name+":"+"Sleeping...");
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
class Student extends Person{
Student(String name,int age){
super(name,age);
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
Output
John:Eating...
John:Sleeping...
John:Sleeping...
Note: Though we can't instantiate abstract classes we can use constructors within abstract classes.
Abstract classes may have abstract methods.(methods without a body or without an implementation). If a abstract method is declared within a class, that class must be declared as an abstract class as well.
Code:
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
public class TestAbstractClass{
public static void main(String args[]){
Student s1= new Student("John", 18);
s1.eat();
s1.sleep();
s1.eat();
s1.sleep();
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
abstract class Person{
String name;
int age;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
this.age=age;
}
void eat(){
System.out.println(this.name+":"+"Eating...");
}
abstract void sleep();
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
class Student extends Person{
Student(String name,int age){
super(name,age);
}
void sleep(){
System.out.println(this.name+":"+"Sleeping 6 hours...");
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
Output
John:Eating...
John:Sleeping 6 hours...
John:Sleeping 6 hours...
Abstract classes can extend an Abstract class and need not to implement the abstract methods of the parent class. but a concrete class must implement all the abstract methods declared in parent abstract classes.
Code:
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
public class TestAbstractClass{
public static void main(String args[]){
PartTimeStudents1= new PartTimeStudent("John", 18);
s1.eat();
s1.study();
s1.sleep();
s1.eat();
s1.study();
s1.sleep();
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
abstract class Person{
String name;
int age;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
this.age=age;
}
void eat(){
System.out.println(this.name+":"+"Eating...");
}
abstract void sleep();
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
abstract class Student extends Person{
Student(String name,int age){
super(name,age);
}
void study(){
System.out.println(this.name+":"+"Studying...");
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
class PartTimeStudent extends Student{
PartTimeStudent(String name,int age){
super(name,age);
}
void sleep(){
System.out.println(this.name+":"+"Sleeping 5 hours...");
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
Output
John:Eating...
John:Studying...
John:Sleeping 5 hours...
Key Points to remember in Abstraction
- A class defined as abstract cannot be instantiated.
- When a method is marked as abstract, there should not be any implementation for the method
- Abstract methods are meant to be overridden in subclasses.
- Abstract class need not have abstract methods.
- If there is an abstract method, the whole class should be declared as abstract.
- A concrete class extending an abstract class should override all the abstract methods.
- If a subclass does not override an abstract method, it should mark itself as abstract to delegate the responsibility to its subclasses.
- Methods marked private cannot be abstract.
- Methods marked as static cannot be abstract
- Methods marked as final cannot be abstract
- Abstract Classes can have constructors.
- When no constructor is defined, a default constructor will be generated by compiler.
- Constructors of abstract classes are invoked when subclass object is created.
Abstract classes and Methods in Java -Java Beginners tutorial by Java9s
Interface
Like in abstract classes interfaces can have methods and variables but all the methods declared within an interface are abstract and public by default and variables are public, static and final.
Since interfaces are not implementing any code within non static methods, there is no point of having non static fields in an interface. That's why that all the variables are static.
Code:
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
public class TestInterface{
public static void main(String args[]){
Circle c1= new Circle(7);
System.out.println("Area: "+c1.area());
System.out.println("Area: "+c1.area());
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
interface CircleShape{
double pi=22/7;
double area();
double area();
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
class Circle implements CircleShape{
double radius;
Circle(double radius){
Circle(double radius){
this.radius=radius;
}
public double area(){
return radius*radius*pi;
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
Output:
Area: 147.0
An interface is needed to form multiple Is-A Relationship. In java you are not allowed to have multiple inheritance but by using interfaces you can achieve this.
//Wrong Implementation - Cannot extend two classes
PartTimeStudent extends Student, Person, Citizen{
}
//Correct Implementation - Person is a abstract or a concrete class while Student and Citizen are interfaces
PartTimeStudent extends Person implements Student, Citizen{
}
When we use extends with a concrete or a abstract class it means that its extending another concrete or an abstract class. And it can extend only one class. But when an interface use extends it means that its extending another interface. It cannot extend concrete or abstract classes But it can extends multiple interfaces.
Comments
Post a Comment