ABSTRACTION IN JAVA
Abstract class in Java
A class which is declared with the abstract keyword is known as an abstract class in java. It can have abstract and non-abstract methods (method with the body).
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Points
- An abstract class must be declared with an abstract keyword.
- It can have abstract and non-abstract methods.
- It cannot be instantiated.
- It can have constructe and static methods also.
- It can have final methods which will force the subclass not to change the body of the method.
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an abstract method.
abstract void print(); //no method body and abstract
eg -
// Abstract class
abstract class Animal {
// Abstract method
public abstract void animalSound();
// Regular method
public void eat() {
System.out.println("the dog eat");
}
}
// Subclass (inherit from Animal)
class Dog extends Animal {
public void animalSound() {
System.out.println("The Dog sound");
}
}
class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog .animalSound();
myDog .eat();
}
}

Comments
Post a Comment