Abstract class and abstract methods in java - When to use abstract class or interface practically, 10 features



Contents of page :
  • Features of Abstract class in java >
  • Program 1 -  Writing your first abstract class
  • Program 2 -  Writing abstract class with no abstract methods


  • When to use abstract class or interface practically - Choosing between interface and abstract class>
    • When to use abstract class practically - Program 3.1
    • When to use interface practically - Program 3.2


  • Program 4 -  If any new instance method is added in Abstract class then all concrete classes which extends that abstract class need not to provide implementation of newly added instance method.
  • Program 5 -  If any new abstract method is added in Abstract class then all concrete classes which extends that abstract class must provide implementation of newly added abstract method.


Features of Abstract class in java >
  1. Abstract class aren’t purely abstraction in java
  2. It’s mandatory to write abstract keyword to make class abstract.
  3. Abstract class can be abstract even without any abstract method.
  4. Abstract class can have private, final, abstract, static and instance methods.
Note : Method cannot be private and abstract.
 Method cannot be final and abstract.
  1. Abstract class have constructors.
  2. Abstract class  doesn’t allows multiple inheritance in java.
  3. Abstract class  implements Interface, can implement more than one interface.
  4. If any new instance method is added in Abstract class then all concrete classes which extends that abstract class need not to provide implementation of newly added instance method. (see Program 4 below)


If any new abstract method is added in Abstract class then all concrete classes which extends that abstract class must provide implementation of newly added abstract method. (see Program 5 below)
  1. Abstract class can have synchronized instance methods, but abstract methods cannot be synchronized.
concrete class which implements abstract class can make abstract methods synchronized.
  1. Even if one abstract method is defined in class than class must be declared as abstract.



Program 1 -  Writing your first abstract class


/*
* abstract class
*/
abstract class MyAbstractClass{
  
   abstract void m();
  
}
/*
* concrete class
*/
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
class MyClass extends MyAbstractClass {
   @Override
   public void m(){
          System.out.println("MyClass - m()");
   }
  
   public static void main(String[] args) {
          MyAbstractClass obj=new MyClass();
          obj.m();
   }
}
/*OUTPUT
MyClass - m()
*/
In the above program MyAbstractClass defines abstract method m()
and its subClass MyAbstractClass provides implementation of abstract method.

Program 2 -  Writing abstract class with no abstract methods


/*
* abstract class
*/
abstract class MyAbstractClass{
  
   void m(){
          System.out.println("MyAbstractClass -  m()");
   }
  
}
/*
* concrete class
*/
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
class MyClass extends MyAbstractClass {
  
   public static void main(String[] args) {
          MyAbstractClass obj=new MyClass();
          obj.m();
   }
}
/*OUTPUT
MyAbstractClass -  m()
*/


In the above program MyAbstractClass defines instance method m().


When to use abstract class or interface practically - Choosing between interface and abstract class>


When to use abstract class practically -
Let’s say we have to choose between class or interface for TerrestrialAnimals, than one thing will be for sure that habitat of all Terrestrial animals must be land. That means we can have same implementation of habitat method for all Terrestrial animals. And food of all Terrestrial animals might be different.


So, we will create abstract class with -
 instance method = habitat()   [because all Terrestrial animals live on land]
 abstract method = food()   [because food of all Terrestrial animals might be different]


instance method habitat() will be inherited in all subclasses.


Program 3.1 - use abstract class practically
/*
* abstract class
*/
abstract class TerrestrialAnimals {
   void habitat(){  
          System.out.println("Habitat of Terrestrial animal is land");
   }
  
   abstract void food();
  
}
/*
* concrete class - Lion
*/
class Lion extends TerrestrialAnimals {
   @Override
   void food(){
          System.out.println("Lion eat - flesh");
   }
}
/*
* concrete class - Lion
*/
class Goat extends TerrestrialAnimals {
   @Override
   void food(){
          System.out.println("Goat eat - grass");
   }
}
public class MyClass{
   public static void main(String[] args) {
          TerrestrialAnimals lion=new Lion();
          lion.habitat();
          lion.food();
          System.out.println();
         
          TerrestrialAnimals goat=new Goat();
          goat.habitat();
          goat.food();
   }
}
/*OUTPUT
Habitat of Terrestrial animal is land
Lion eat - flesh
Habitat of Terrestrial animal is land
Goat eat - grass
*/



When to use interface practically -
Let’s say we have to choose between class or interface for Animals, than habitat of  animals might be land or water.
And food of all animals might be different.


So, we will create interface with -
 abstract method = habitat()   [because animals might be living on land or water]
 abstract method = food()   [because food of all animals might be different]
Program 3.2 - use interface practically


/*
* interface
*/
interface Animals {
   abstract void habitat();
  
   abstract void food();
  
}
/*
* concrete class - Lion
*/
class Lion implements Animals {
   @Override
   public void habitat(){
          System.out.println("Habitat of Lion is land");
   }
  
   @Override
   public void food(){
          System.out.println("Lion eat - flesh");
   }
}
/*
* concrete class - Lion
*/
class Whale implements Animals {
   @Override
   public void habitat(){
          System.out.println("Habitat of Whale is water");
   }
  
   @Override
   public void food(){
          System.out.println("Whale eat - aquatic animals");
   }
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass{
   public static void main(String[] args) {
          Animals lion=new Lion();
          lion.habitat();
          lion.food();
         
          System.out.println();
         
          Animals whale=new Whale();
          whale.habitat();
          whale.food();
   }
}
/*OUTPUT
Habitat of Lion is land
Lion eat - flesh
Habitat of Whale is water
Whale eat - aquatic animals
*/





Now, I will like to elaborate 8th point >
Program 4 -  If any new instance method is added in Abstract class then all concrete classes which extends that abstract class need not to provide implementation of newly added instance method.
/*
* abstract class
*/
abstract class MyAbstractClass{
  
   void m(){
          System.out.println("MyAbstractClass -  m()");
   }
  
}
/*
* concrete class
*/
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
class MyClass extends MyAbstractClass {
  
   public static void main(String[] args) {
          MyAbstractClass obj=new MyClass();
          obj.m();
   }
}
/*OUTPUT
MyAbstractClass -  m()
*/
In the above program, MyAbstractClass defines instance method m() and concrete classes MyClass which extends that MyAbstractClass didn’t provide implementation of instance method m().


Program 5 -  If any new abstract method is added in Abstract class then all concrete classes which extends that abstract class must provide implementation of newly added abstract method.


/*
* abstract class
*/
abstract class MyAbstractClass{
  
   abstract void m();
  
}
/*
* concrete class
*/
class MyClass extends MyAbstractClass {
   @Override
   public void m(){
          System.out.println("MyClass - m()");
   }
  
   public static void main(String[] args) {
          MyAbstractClass obj=new MyClass();
          obj.m();
   }
}
/*OUTPUT
MyClass - m()
*/


In the above program, MyAbstractClass defines abstract method m() and concrete classes MyClass which extends that MyAbstractClass provided implementation of abstract method m().



RELATED LINKS>

Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features

10 Differences between Interface and abstract class in java - in detail with programs


Labels: Core Java
eEdit
Must read for you :