why cant an abstract class be instantiated??
Printable View
why cant an abstract class be instantiated??
If u wan't a class whose object can't be created , make it abstract class.
Example ... shape is an abstract class
where as Rectangle, Circle and Triangle r the concrete sub classes
When I declare Shape as an Abstract Class ...(becoz Shape is unique so I make it abstract so that the sub classes can use the abstract methods in the super class)
Simple...
You cant create an object for abstract class directly...instead you can create object for the class that extends the abstract class...Your question is "Why?" ..
When you inherit a class with an abstract class, the compiler implicitly creates an object for both the classes...Any class that contain an abstract method must be declared “abstract” and abstract methods can have definitions only in child classes(Inheriting Classes...)...
Whenever you need your class to prevent instantiated, create it abstract...
similarly when you need your class to prevent inherited, create it Final...
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed..
Only Abstract classes can have abstract methods...
They can contain implemented Interfaces...
abstract interface get
{
public void fun();
abstract void sun();
}
abstract class hello implements get
{
public void fun()
{
System.out.println("fun...");
}
abstract void sun() //reports an error that abstract methods cant have a body
{
System.out.println("sun...");
}
}
public class one
{
public static void main(String args[])throws Exception
{
one o=new one();
//hello h=new hello(); //Shows Error....
}
}
In your snippet please use code tags
[code]
YOUR CODE HERE
[/code]
Also; it's unnecessary to say "abstract interface", an interface is implicitly abstract and the explicit qualifier is unnecessary.
You should also be using naming conventions in classes and methods. For classes the first letter of each word is capitalized
And methods have the first letter of the first word lowercase and the remainig words have the first letter capitalized.Code:Get
Instead of
get