Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

Abstract class

Posted By: Christopher James     Category: Java     Views: 20040

This article explains about an abstract class with example in java.

Abstract class specifies what functionality is provided but not how that functionality is provided. Abstract classes cannot be instantiated.

Syntax of class with abstract modifier

abstract class clsName
{
      // body of class
}

Examples of an Abstract class

Example 1 : Program that illustrates how to use abstract class in java using class shape

abstract class Shape
{
       void display()
       {
       }   
}

class Circle extends Shape
{
       void display()
       {
              System.out.println("You are using circle class");
       }
}

class Rectangle extends Shape
{
       void display()
       {
              System.out.println("You are using rectangle class");
       }
}

class Triangle extends Shape
{
       void display()
       {
              System.out.println("You are using triangle class");
       }
}

class AbstractClassDemo
{
       public static void main(String args[])
       {
               Shape sObj = new Circle();
               sobj.display();
               
               sObj = new Rectangle();
               sobj.display();

               sObj = new Triangle();
               sobj.display();
       }
}


Output 
You are using circle class
You are using rectangle class
You are using triangle class

 
  
Share: 

 
 

Didn't find what you were looking for? Find more on Abstract class Or get search suggestion and latest updates.

Christopher James
Christopher James author of Abstract class is from Los Angeles, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!