CodingBison

An abstract class, as the name says it is just declaration and, it does not have a concrete implementation. There would be many scenarios where a super-class is not aware of the content that goes into the methods, and only the sub-class that inherits it, would have that info. Accordingly, we can use the abstract class to mandate that every sub-class must define their own implementation of the abstract class.

Let us look at an example and understand the usage of an "abstract class"



Figure: Abstract Class

Abstract class properties

Let us understand the properties of an Abstract Class.

First, as shown in the figure, "abstract" key word is used to indicate an Abstract class (abstract class movie). Second, An abstract class should at-least have one abstract method defined in it. Third, a sub-class (jamesBondMovies extends movie) that inherits and abstract class "must" implement the abstract methods. As shown in the above figure if a sub-class (harryPotterMovies extends movie) does not implement the abstract methods, then the compiler throws an error. Fourth, an object cannot be instantiated for an abstract class.

Here is an example:

 abstract class movie {
     String name;
     int    length;
     String director;

     /**
      * Default constructor.
      */
     movie () {
         this.name = "";
         this.length = 0;
         this.director = "";
     }
     /**
      * @param name
      * @param length
      * @param director
      */
     movie(String name, int length, String director) {
         this.name = name;
         this.length = length;
         this.director = director;
     }

     // This is an abstract method, and the sub-classes will
     // have to implement it.
     abstract void fillThePlotSummary(String summary);

     String dummyConcreteMethod() {
         return "Dummy Concrete method of Abstract class";
     }	
 }

 class jamesBondMovies extends movie {
     String bondActor;
     String bondGirl;
     String mRoleCharacterName;
     String plotSummary;
     /**
      * Default constructor.
      */
     jamesBondMovies() {
         super();
         this.bondActor = "";
         this.bondGirl = "";
         this.mRoleCharacterName = "";
     }

     jamesBondMovies(String name, int length, String director, 
                                     String bondActor, String bondGirl, 
                                     String mRoleCharacterName) {
         super(name, length, director);
         this.bondActor = bondActor;
         this.bondGirl = bondGirl;
         this.mRoleCharacterName = mRoleCharacterName;
     }

     // Plot summary would be unique for every instance of a jamesBondmovies class.
     void fillThePlotSummary(String summary) {
         System.out.println("(Sub Class: JamesBondMovies) Abstract class summary is being filled");
         plotSummary = summary;
     }

     String getThePlotSummary() {
         return plotSummary;
     }
 }

 public class abstractExample {
     public static void main(String[] args) {
         /* This is not allowed, an object cannot be instantiated for abstract class */
         //movie           movieObj1 = new movie();

         jamesBondMovies jbondObj1 = new jamesBondMovies("Casino Royale",144,
                                                         "Martin Campbell",
                                                         "Daniel Craig",
                                                         "Eva Green",
                                                         "Judi Dench");

         /* This is still OK, as this is just a reference */
         movie             movieObj;

         jbondObj1.fillThePlotSummary("James bond, object 1 plot summary");
         System.out.println( jbondObj1.getThePlotSummary());

         movieObj = jbondObj1;
         System.out.println("Movie Name : " + movieObj.name + " Length: " + movieObj.length);
         System.out.println("Concrete Class : " + movieObj.dummyConcreteMethod());
     }
 }

Like shown in the above example, super-class "movies" does not have any clue about the movie plot and only the sub-classes do, and hence it imposes the sub-class to add the plot summary to every object by defining an abstract method (abstract void fillThePlotSummary(String summary);). Now that a super-class has added an abstract method, sub-class is forced to fill in the plot summary.

Continuation to the properties of Abstract class, fifth, run time polymorphism is still achieved, as a reference to an abstract class can be created (not an object though). Sixth, abstract class like any other class can have other concrete classes defined (String dummyConcreteMethod ()).

Compile/Run this program and here is the output:

 (Sub Class: JamesBondMovies) Abstract class summary is being filled
 James bond, object 1 plot summary
 Movie Name : Casino Royale Length: 144
 Concrete Class : Dummy Concrete method of Abstract class




comments powered by Disqus