Java Abstraction

— Abstraction in JAVA “shows” only the essential attributes and “hides” unnecessary details of the object from the user.
— In Java, abstraction is accomplished using Abstract classes, Abstract methods, and interfaces.— Abstraction helps in reducing programming complexity and effort.

WATCH VIDEO:

Abstract Class:

— A class which is declared “abstract” is called as an abstract class.
— It can have abstract methods as well as concrete methods.
— A normal class cannot have abstract methods.

Abstract Method:

— A method without a body is known as an Abstract Method

— It must be declared in an abstract class.
— The abstract method will never be final because the abstract class must implement all the abstract methods.

Abstraction Java Program:

abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println(“Zzz”);
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println(“The pig says: wee wee”);
}
}
class MyAbstractClass {
public static void main(String[] args) {
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}

Output: The pig says: wee wee , and Zzz

Java Abstraction OOPs

Connect with us: