What is an "abstract class" in Java?
An abstract class is a class which cannot be instantiated. An abstract class is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass:
Here's an example:
Notice that "abstractMethod()" doesn't have any method body. Because of this, you can't do the following:
There's no method that implements
Here's a correct
Notice that you don't have to define
Here's another correct
In this case, you have overridden
However, because of the
You can't do this because the implementation of
Now you can also implement an abstract class twice:
Now somewhere you could write another method.
Notice that even though we declared
If we wish to access a member specific to a particular subclass, we must cast down to that subclass first:
Lastly, you cannot do the following:
Only one class can be extended at a time. If you need to extend multiple classes, they have to be interfaces. You can do this:
Here's an example interface:
This is basically the same as:
The only difference is that the second way doesn't let the compiler know that it's actually an interface. This can be useful if you want people to only implement your interface and no others. However, as a general beginner rule of thumb, if your abstract class only has abstract methods, you should probably make it an interface.
The following is illegal:
You cannot implement methods in an interface. This means that if you implement two different interfaces, the different methods in those interfaces can't collide. Since all the methods in an interface are abstract, you have to implement the method, and since your method is the only implementation in the inheritance tree, the compiler knows that it has to use your method.
A Java class becomes abstract under the following conditions:
1. At least one of the methods is marked as abstract:
In that case the compiler forces you to mark the whole class as abstract.
2. The class is marked as abstract:
As already said: If you have an abstract method the compiler forces you to mark the whole class as abstract. But even if you don't have any abstract method you can still mark the class as abstract.
A common use of abstract classes is to provide an outline of a class similar like an interface does. But unlike an interface it can already provide functionality, i.e. some parts of the class are implemented and some parts are just outlined with a method declaration. ("abstract")
An abstract class cannot be instantiated, but you can create a concrete class based on an abstract class, which then can be instantiated. To do so you have to inherit from the abstract class and override the abstract methods, i.e. implement them.
It's a class that cannot be instantiated, and forces implementing classes to, possibly, implement abstract methods that it outlines.
Take a look at this:
Simply speaking, you can think of an abstract class as like an Interface with a bit more capabilities.
You cannot instantiate an Interface, which also holds for an abstract class.
On your interface you can just define the method headers and ALL of the implementers are forced to implement all of them. On an abstract class you can also define your method headers but here - to the difference of the interface - you can also define the body (usually a default implementation) of the method. Moreover when other classes extend (note, not implement and therefore you can also have just one abstract class per child class) your abstract class, they are not forced to implement all of your methods of your abstract class, unless you specified an abstract method (in such case it works like for interfaces, you cannot define the method body).
Otherwise for normal methods of an abstract class, the "inheriters" can either just use the default behavior or override it, as usual.
Get your answers here:
BTW - those are question you asked recently. Think about a new question to build up reputation...
Just realized, that the posters of this and the referenced questions have the same or at least similiar name but the user-id is always different. So either, there's a technical problem, that keyur has problems logging in again and finding the answers to his questions or this is a sort of game to entertain the SO community ;)
An abstract class can not be directly instantiated, but must be derived from to be usable. A class MUST be abstract if it contains abstract methods: either directly
However, a class can be abstract without containing abstract methods. Its a way to prevent direct instantation, e.g.
The latter style of abstract classes may be used to create "interface-like" classes. Unlike interfaces an abstract class is allowed to contain non-abstract methods and instance variables. You can use this to provide some base functionality to extending classes.
Another frequent pattern is to implement the main functionality in the abstract class and define part of the algorithm in an abstract method to be implemented by an extending class. Stupid example:
Solution - base class (abstract)
From oracle documentation
Abstract Methods and Classes:
An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
If a class includes abstract methods, then the class itself must be declared abstract, as in:
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.
Little addition to all these posts.
Class which can have both concrete and non concrete methods i.e. with body and without body. 1-methods without implementation must contain 'abstract' key-word 2-abstract class can't be instantiated
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.
In other words, a class that is declared with abstract keyword, is known as abstract class in java. It can have abstract(method without body) and non-abstract methods (method with body).
Important Note:- Abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java's approach to run-time Polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature in the below example