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
i want to know explanation about what is abstract class and method
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.
I want to more information about abstract method or class.
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();
}
}