Object Oriented Programming - JAVA - Abstract / Abstract classes
Object Oriented Programming - JAVA👽
Abstract / Abstract classes .....
Abstraction in OOPS is used to hide unnecessary information and show only necessary information to interacting users.
We can modify a class or method into abstract class or abstract method using Abstract keyword.
Let's continue this discussion using example code in the below.
class Vehicle {
void park(){
}
}
class Bicycle extends Vehicle {
void park(){
// break
// putting a foot on the ground
// getting off the bike
}
}
A bodyless method is in the Vehicle class because it needs to be overridden.
Open and closing curly brackets are not needed for the method because it is bodyless.
But we can't use like void park();, That's why we look like a command using method run.
When we use Abstract keyword, we can show bodyless methods without open and closing curly brackets.
So we can show that " abstract void park();" .
What are the conditions in abstract method or classes ?
- Abstract method only can have abstract classes.
- Abstract classes also can have normal methods.
- Abstract classes are also work like normal classes.
- We can't create objects for abstract classes.
- But we can access normal methods of abstract class using upcasting or using Super keyword.
abstract class Vehicle {
int a = 5;
int b = 6;
void plus (){
sout (a + b);
}
abstract void park();
}
class Bicycle extends Vehicle {
void park(){
sout("Parking");
super.plus(); // call methods using super keyword
}
}
class Test {
public static void main(String args[])
Vehicle v = new Bicycle();
v.plus(); // call methods using upcasting
}
}
* Before Java8 version, we can't keep a constructor in abstract class because of we can't create objects , but after Java8 version, we can keep a constructor within abstract class*
Comments
Post a Comment