Abstraction
What is a meaningless method ?
when we inherit sub classes and super classes,we have to create a meaningless method/s to override.
meaningless/blank methods haven't a body.
It only used to override methods as our wish and we cannot include specific methods inside meaningless methods as it is only used to overriding.
We write our codes inside the methods of sub classes according to our necessity after overriding.
When we use "Abstraction" ?
When we are unable to give specific codes inside a method that is being created in a certain instance(Meaningless methods) ,we use "Abstraction" to keep that type of methods.
For that, "Abstract" keyword is used.
Keypoints
- We use abstraction to keep meaningless methods.
- Only abstract class can keep abstract methods.
- Abstract class can contain both abstract methods and non-abstract methods.
- Objects cannot be created by using abstract classes.
- Therefore constructors cannot be created.
- But we can run methods by using the "super" keyword inside sub classes.
How to create objects with Abstract classes ?
According to the above example, we can use casting to create objects by using the super class object reference.
And here we can run only sub classes.
Vehicle v1 = new Car ();
Vehicle v2 = new Van ();
We can call methods inside the sub classes by using the above procedure.
And what if we want to call the method inside the super class ?
For that, we can use the "super" keyword as below.
super.colour();
v1.super();
v2.super();
Comments
Post a Comment