Thursday 3 November 2016

Java Source World: Abstract Class in Java


Abstract Class in Java



Abstract Class:


A class which is modified by a keyword "Abstract" is known as "Abstract Class".

  • An abstract class can contain collection of all undefined methods (known as abstract methods)

Example:

            public abstract class MyAbstract {
                 abstract void show();//Undefined method 
                 abstract void calculate();//Undefined method
               }

  • An abstract class can contain both defined and undefined methods.


Example:

           public abstract class MyAbstract {
                  abstract void show();//Undefined method
                  public void calculate(){
                       //do something
                     }//Defined method
              }

  • An abstract class can contain collection of only defined methods too.
Example:

              public abstract class MyAbstract {
                    public void show(){
                            //do something
                           }//Defined method
                    public void calculate(){
                           //do something
                       }//Defined method
                 }


Abstract classes cannot be instantiated, they should be used only as a super class(Parent class).

If a class has any undefined method then that must be modified by abstract keyword, otherwise it will result in compile time error.
And then if a class has at-least one abstract method then the entire class has to be modified by "abstract" keyword otherwise it results in compile time error.

Can abstract class has constructor ?



Answer is Yes. 

Abstract class has default constructor, it can also have parameterized constructor.

Abstract class constructor can be used by child class to initialize the inherited data members.


Points to remember about Abstract class:

  • Abstract class cannot be made with final keyword.
  • Abstract methods cannot be made with final keyword, if do so results in compile time error.
       Example:
                               public abstract  class MyAbstract {
     
                       abstract final void doSomething();//compile time error
                 }

Compile time error message shows as below:

     "The abstract method doSomething in type MyAbstract can only set a                visibility modifier, one of public or protected"

  • Abstract methods cannot me made private
  • Abstract methods cannot be made static, abstract and static cannot be used together.

Both abstract and non abstract methods are inherited to child class. Child class should provide the definition for inherited abstract methods, otherwise child class will also become abstract. We need to modify that class by abstract keyword.


Child class which doesn't provide definition for all inherited abstract methods, it will become abstract and we cannot instantiate it.

When abstract method is defined in the child class keyword abstract should not be used.


Example:


            abstract class Parent {
                 abstract void show();//Undefined method
                 abstract void doSomething();//Undefined method
                }

             abstract class Child extends Parent{
                  @Override
                  void doSomething() {
                       //Do something
                     }
                  abstract void show() ;
               }
              class GrandChild extends Child{
                  void show(){
                     //Do something
                    }
               }
Defined methods cannot be made "abstract".

Example:


          abstract class Parent {
                 abstract void show();//Undefined method
                 abstract void doSomething();//Undefined method
            }
       
             abstract class Child extends Parent{
               @Override
                void doSomething() {
                //Do something
                      }
                   void show(){
                  //Do something
                      }
                }

Advantages of Abstract classes in java:

  
    *     Re-usability: As child class inherits the parent class methods(features) it can reuse them to get all the benefits of inheritance concept in java.

    *    Contract: Abstract classes impose contract on child class to override all inherited abstract methods. This is useful for real time applications.


                                                      Figure: Advantages of Abstract class

As shown in the above class diagram, BankServices is an abstract class and it has one defined(Concrete) method void show() and two abstract methods. tax() and interest();

In SavingAccount and FixedAccount classes First method reused to show some details. it will have the common behavior and the second and 3rd methods are have some contract and individually will get  implemented with different behavior in the derived classes.

Abstract class can be used as a reference variable to hold child class object. This allows to implement Dynamic method dispatch or Run time polymorphism through abstract class reference variable.
             
Example:

         abstract class Parent {
                  abstract void show();//Undefined method
             }

        class ChildOne extends Parent{
           @Override
            void show(){
               System.out.println("ChildOne show() method is called");
                 }
             }
 
         class ChildTwo extends Parent{
              @Override
                  void show(){
                   System.out.println("ChildTwo show() method is called");
              }
           }
 
 
     public class MyAbstract{
          public static void main(String[] args) {
             Parent ref; //Reference variable for the Parent class
           
            ref = new ChildOne();
            ref.show();
           
            ref = new ChildTwo();
            ref.show();
           
          }
     }

Now we will run the above code and see the output.

Console output:

ChildOne show() method is called
ChildTwo show() method is called

So, firstly ChildOne class show() method is got called and then the ChildTwo class show method is called, that is based on the parent reference variable is pointed to which class.


You may also like the fallowing posts:


                           

                                                                                   Java Source World                   A Java quick reference blog

                                  







No comments:

Post a Comment