abstraction in java with interview questions and answers with example

In this tutorial, we will see What is an abstraction in java with practical and at the end, we will discuss some important interview questions and answers related to abstraction in java.

What is an abstraction

in Java?

Java is an object-oriented programming language. abstraction is an oops concept. Abstraction is a process of hiding the implementation details and showing only the functionality of the application. We can be achieved abstraction in java by using abstract class and interface.

Abstract class vs abstraction 0 to 100%.

Interface vs abstraction 100%.

Abstract class in Java.

A Java class which is defined with an abstract keyword is called abstract class. An abstract class is something which is incomplete and you cannot create an instance of the abstract class.If you want to use it you need to extend it and complete it.
Syntax:

abstract class class-name{}

Abstract method in Java.

A method without implementation is called an abstract method. the abstract method is just prototype of the method or you can signature.

abstract return_type method_name ();

Abstraction in java with example.

public class Main{
  public static void main(String args[]){
        FeedBack obj= new FeedBack();
        obj.show();
    }
}
abstract  class Message{
    public abstract void show();
}
class FeedBack extends Message{
  public void show(){
    System.out.println("Hello CodeBun");
  }
}

Important points you should remember

  • If you define any method as an abstract than class must be abstract.
  • If you are extending an abstract class so you need to implement all the method of that class.
  • In Java, you cannot create the instance of an abstract class.
  • Use abstraction if you know something needs to be in class but the implementation of that varies.
  • abstract is a keyword in Java.

Java abstraction interview questions and answers with example

Can we define constructors in an abstract class?

An abstract class can have a constructor but it cannot be instantiated. If you do not define your own constructor compiler will give a default constructor to the Abstract class.

why we use a constructor in abstract class in java?

A constructor is used in the abstract method because some time we need to initialize variables in subclass like below code.

class Main extends User {
  String education;

  Main(String education, String name, int age) {
    super(name, age);
    this.education = education;
  }

  public static void main(String[] args) {
    Main obj = new Main("MCA", "rahul", 22);
    System.out.println(obj.education + " " + obj.name + " " + obj.age);
  }

  @Override
  public void msg() {
    // TODO Auto-generated method stub
    // write your code....

  }
}

abstract class User {
  String name;
  int age;

  User(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public abstract void msg();

}

Can we define an abstract class as a final class?

My answer is direct no because as abstraction says you are going implement the methods, and as you know if we define as a final we can’t change anything. They are also opposite of each other, abstract keyword enforces to extend a class, for using it, on the other hand, final keyword prevents a class from being extended.

Is it necessary for an abstract class to have an abstract method?

No, it’s not necessary because we can define concrete and non-concreate method in abstract class.

Difference between abstract class and interface in Java.

An abstract class can contain abstract and non-abstract method but in interface only abstract method its true before java 8 version now we can define. please read in detail.

Can we define the main class as an abstract class?

Yes, the abstract class can contain the main method.

abstract class Main {

  public static void main(String[] args) {
    System.out.println("hello codebun");
  }
}

Can we declare abstract methods as private?

No, If abstract methods are allowed to be private, then they will not be inherited to subclass.

 

You May Also Like These Posts:

Leave a Reply

Your email address will not be published.

twenty − 5 =