Infolinks In Text Ads

Object Oriented Programming, Abstract Classes In Java

Abstract Classes In Java:

 A  class is Abstract when it is declared with Abstract keyword or one or more methods in the class are abstract , class must also be abstract.
Example:



package com.aurangzeb.utk;


public abstract class Document {

    // declare fields
    // delcare non-abstract methods.
     public  abstract void xmlDocument();
  
}



we cannot create objects of an bbstract class, However we can declare reference variables that point to its child class objects.

For any subclass of an abstract class it MUST implement all of the abstract methods in the superclass
OR
Be itself declared abstract if doesn't implement inherited abstract methods.
When a class is not abstract, it is called 'Concrete' Class.




Code Example:



package com.aurangzeb.utk;

public abstract class Graphics {

    // declare fields
    // delcare non-abstract methods.
     public  abstract void draw();
  

package com.aurangzeb.utk;

public class Circle extends Graphics {

      @Override
      public void draw() {
            System.out.println("Draw circle");
           
      }

}


package com.aurangzeb.utk;

public class Shape extends Graphics{

      @Override
      public void draw() {
           
            System.out.println("Draw Shape");
           
      }

}

 

 


Newer Post Older Post

Leave a Reply

Powered by Blogger.