Abstract class
A Java abstract class is a class which cannot be instantiated, meaning you cannot create new instances of an abstract class.
Declaring an Abstract Class in Java
public abstract class MyAbstractClass
{
}
Abstract Methods:
An abstract class can have abstract methods. You declare a method abstract by adding the abstract keyword in front of the method declaration.
Here is a Java abstract method example:
public abstract class MyAbstractClass
{
public abstract void abstractMethod();
}
If a class has an abstract method, the whole class must be declared abstract. Not all methods in an abstract class have to be abstract methods.
Example:
abstract class MyClass
{
public void show()
{
System.out.println("Hello World");
}
abstract public void show2();
}
class Demo extends MyClass
{
public void show2()
{
System.out.println("Abstract method");
}
public static void main(String args[])
{
Demo obj = new Demo();
obj.show2();
}
}
Output:
Abstract method
3 Comments
What is difference between abstract class and Interface then?
ReplyDeleteI will Soon , make a post on this...👍👍
DeleteSee the Latest Post in Abstract class Vs Interface . parth your problem solved,.
ReplyDelete