Abstract class in java
Abstract class used to declare and define the method.
You can't define the abstract method in abstract class. But you can define that abstract method in another class . That class should be extends the abstract class.
Why Abstract Class use ?
- we don't want anyone to create object of abstract class.
- we can use one method which except all the subclass objects.
The abstract class "Food" has a one abstract method. In that abstract method defined as child class.
Example-1
abstract class Food { public abstract void Non_veg(); } class A extends Food { public void Non_veg() { System.out.println("Chicken"); } } public class Main { public static void main(String [] args) { A obj = new A(); obj.Non_veg(); } }
Output
Chicken
Example-2
abstract class Food { public void Veg() { System.out.println("Tomoto rice"); } public abstract void Non_veg(); } class A extends Food { public void Non_veg() { System.out.println("Chicken"); } } public class Main { public static void main(String [] args) { A obj = new A(); obj.Veg(); obj.Non_veg(); } }
Output
Tomoto rice Chicken