crux points:
- An abstract class is a class which is declared with the keyword "abstract".
- Abstract classes - No instantiation (Objects cannot be created).
- Abstract classes can be extended by other classes (Inheritance is possible).
- Abstract classes may or may not contain abstract methods.
An abstract method is a method which has the only declaration but no implementation.
eg: abstract void thedevelopingminds();
The implementation of this abstract method is provided by the class which gets inherited from the abstract class.
Note:
The major difference between abstract classes and interfaces is-
Abstract class - may or may not have abstract methods
interface - All the methods are abstract strictly.
From Java 8, we can include default methods too.
From Java 8, we can include default methods too.
An example demonstrating abstract class:
abstract class Geometricobject
{
void display() //non-abstract method
{
System.out.println("I am Geometric object");
}
abstract void area(); //abstract method
}
class Circle extends Geometricobject
{
double radius=2.8;
//abstract method must be implemented by the inherited class
void area()
{
System.out.println("I am circle with area:"+""+3.14*radius*radius);
}
}
class Rectangle extends Geometricobject
{
int length=3;
int breadth=5;
//abstract method must be implemented by the inherited class
void area()
{
System.out.println("I am rectangle with area:"+""+length*breadth);
}
}
class TestArea
{
public static void main(String... args)
{
Circle c=new Circle();
c.area();
Rectangle r=new Rectangle();
r.area();
}
}
Output:
I am circle with area:24.6176
I am rectangle with area:15
Comments
Post a Comment