Abstract -Abstract in English means existing in thought or as an idea without concrete existance.
Abstract method-A method that is declared without an implementation
abstract void moveTo(double x ,double y)
Abstract class-If a class includes abstract methods, then the class itself must be declared abstract, as in:
public abstract class PhoneModel{
abstract void switchoff();
//code}
When an abstract class is subclass the subclass usually provides implementation for all of the methods in parent class If it doesn't , It must be declared abstract be declared abstract.
Note-It is possible to create reference of or abstract class.
It is not possible to create an object of an abstract class.
We can also assign reference of an abstract class to the object of a concrete subclass.
Interfaces in Java-Interface in English is a point where two systems meet and intrace.
In Java interface is a group of related methods with empty bodies.
class AvonCycle implements Bycycle{
Example-
interface Bicycle{
void applyBrake(int decrement);
void speedUp(int increment);
}
int speed=7;
void applyBrake(int decrement){
speed=speed-decrement;
}
}
Abstract class vs Interfaces
We cant extend multiple abstract classes but we can implement multiple interfaces at a time. Interfaces are meant for dynamiic method dispatch and run time polymorphism.
Is multiple inheritance allowed in java?
Multiple inheritance face problems when there exist methods with some signature in both the super classes.
Due to such problems, Java does not support multiple inheritance directly but the similar concept can be achieved using interfaces.
A class can implement multiple interfaces and extend a class at the same time.
Comments
Post a Comment