Abstract Class Example in Java Program

Abstract Class Example in Java Program. You can find Abstract Class, Abstract method, Abstract Concept program (code snippets) in this page.

Abstract Class Example in Java Program. You can find Abstract Class, Abstract method, Abstract Concept program (code snippets) in this page.

abstract class Shape{

abstract void draw();

}

class Rectangle extends Shape{

void draw(){

System.out.println(“Drawing Rectangle”);

}

}

class Traingle extends Shape{

void draw(){

System.out.println(“Drawing Traingle”);

}

}

class AbstractDemo{

public static void main(String args[]){

Shape s1=new Rectangle();

s1.draw();

s1=new Traingle();

s1.draw();

}

}

Result:

Drawing Rectangle

Drawing Traingle


4 Comments

  • 1
    venkatesh
    October 3, 2012 - 2:52 pm | Permalink

    i want to know explanation about what is abstract class and method

  • 2
    selvi
    January 4, 2013 - 11:47 am | Permalink

    you will want to create a super class that only defines a generalized form that will be shared by all of its sub classes, leaving it to each subclass to fill in the details. Such a class determines the nature of the methods that the sub classes must implement. One way this situation can occur is when a super class is unable to create a meaningful implementation for a method.

  • 3
    rajtravelsss
    March 13, 2013 - 2:34 pm | Permalink

    I want to more information about abstract method or class.

  • 4
    Vivek Ghavle
    May 12, 2013 - 12:11 am | Permalink

    But same thing can achieve without Abstract class so why to use it

    class Rectangle {

    void draw(){

    System.out.println(“Drawing Rectangle”);

    }

    }

    class Traingle {

    void draw(){

    System.out.println(“Drawing Traingle”);

    }

    }

    class AbstractDemo{

    public static void main(String args[]){

    Rectangle s1=new Rectangle();

    s1.draw();

    Traingle s2=new Traingle();

    s2.draw();

    }

    }