Abstract Classes
When we're coding, we will meet methods that we can't apply codes, So we can name them as Meaningless Methods. We can't store meaningless methods in an ordinary way. In that situation, We have to make Abstract methods using the keyword "Abstract". We can't store Abstract methods in an ordinary class. Therefore, we have to make the class also an abstract class using Abstract keyword. We can store abstract methods only in abstract classes. But we can store any method in an abstract class. The important thing is, we cannot create objects in an abstract class.
Refer to the code sample given below,
Abstract class Vehicle{
abstract void park ();
}
Abstract class Vehicle {
Abstract void park ();
void reverse (){
}
}
There is a code inside the meaningless method highlighted with Orange like ,
Ex :- class Vehicle {
void park (){
}
}
The part highlighted in blue is a meaningless method.
Example :- Abstract void park ();
In meaningless methods there is no call.
But we have to call the class, and also we have to create an object from the class but we cannot do these in abstract classes.
You know that a Software Engineer is a problem solver, So we have to solve this too.
Refer to the code sample given below,
Abstract class Nimal {
abstract void coding ();
void playingcricket (){
System.out.println ("Bowling!");
}
}
(But we have to make an object)
class Rathnapala extends Nimal{
Nimal n = new Rathnapala ();
n.playingcricket ();
}
output = Bowling!
The word highlighted with pink is the Super class reference.
The word highlighted with purple is the Sub class object.
Here we can extend abstract classes.
1) Can we generate a constructor in an abstract class?
No, We can't
2) Why?
Because abstract classes can't have objects


Comments
Post a Comment