Abstraction
What is Abstraction?
- Abstraction means hiding the background detail and showing the essential element only.
- Let's take a daily life example of a switchboard, we know that by tapping the switches we can turn them on and off the fan without knowing the functionality performed inside of the switch.
- Abstraction is not possible without inheritance as method overriding is not possible without inheritance.
Need of Abstraction
- By the use of abstraction, we can provide security to our application so that breaches in the database can be prevented by hiding the functionality of the methods.
Key Points:
- The abstract keyword is used to create an abstract method and class.
Example:
abstract class AbstractClass { // abstract class
abstract void getAbstractClass(); // abstract method
}
- An abstract class is different from a normal class as we can't create an object of an abstract class. We can instantiate the Anonymous Subclass of the abstract class
Example:
package quipohouse;
public abstract class AbstractClass {
public static void main(String[] args) {
AbstractClass a = new AbstractClass();
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type AbstractClass
- If we want to access the non-static member of the abstract class then we have to create the object of a class that extends that abstract class.
Example:
package quipohouse;
abstract class AbstractClass { // Abstract Class
int a = 10;
abstract void getAbstractMethod(); // Abstract Method
void getAbstractClass() { // Concrete Method
System.out.println("Inside Abstract Class");
}
}
class Child extends AbstractClass { // Inheriting abstract class
// two public class cannot reside in one .java file
@Override // Overriding abstract method
void getAbstractMethod() {
}
public static void main(String[] args) {
Child c = new Child(); // Creating object
System.out.println(c.a); // Calling abtract method through child
c.getAbstractClass();
}
}
Output:
10
Inside Abstract Class
- If we don't want the child class to be abstract because of inheriting the abstract class containing abstract methods, then we must have to override all the abstract methods of parent classes.
Example:
package quipohouse;
abstract class AbstractClass {
int a = 10;
abstract void getAbstractClass();
}
class Child extends AbstractClass {
@Override
void getAbstractClass() {
System.out.println("Inside Overriden Abstract Method");
}
public static void main(String[] args) {
Child c = new Child();
c.getAbstractClass();
System.out.println();
}
}
Output:
Inside Overriden Abstract Method
- For calling the constructor of an abstract class we have to instantiate the child class.
Example:
package quipohouse;
abstract class AbstractClass {
public AbstractClass() { // Constructor
System.out.println("Inside AbstractClass Default Constructor");
}
abstract void getAbstractMethod();
}
class Child extends AbstractClass {
@Override
void getAbstractMethod() {
}
public static void main(String[] args) {
Child c = new Child();
// as default constructor automatically called when we instantiate the class
}
}
Output:
Inside AbstractClass Default Constructor
Abstraction can be done at two levels:
- Class Level
- Method Level
Abstract class or Class Level Attraction:
- At the class level, we achieve abstraction by declaring class abstract.
- An abstract class have one or more abstract methods.
- It may have non-abstract methods or concrete methods.
- If a class has an abstract method then it must be declared as abstract.
Syntax:
public abstract Class_Name{
abstract return_type method_Name();
return_type method_Name(){
}
}
Abstract method or method level abstraction:-
- We do abstraction by creating abstract methods too.
- An abstract method does not have a body or block and is declared abstract.
- If an abstract class is extended by a child class then all abstract methods should be defined ( or have the body )which is also known as method overriding.
Syntax:
abstract return_type method_Name();
Examples of both:
package quipohouse;
abstract class AbstractClass { //abstract class
abstract void getAbstractClass(); //abstract method
void getAbstractMethod() {
}
}