Are you prepared in attending an interview? Then do not worry, we’ve a right answer for your job interview preparation. If you are preparing for Java Abstraction job interview and don’t know how to crack interview and what level or difficulty of questions to be asked in job interviews then go through Wisdomjobs Java Abstraction interview questions and answers page to crack your job interview. Abstraction is the process of abstraction in Java which is used to hide certain details and display only required features of the object. It hides the implementation details from the user and provides the functionality to the user. Below is the list of frequently asked Java Abstraction interview questions and answers which gets you ready to face the interviews:
Question 1. What Is Abstraction In Java?
Answer :
In java, Abstraction means show functionality and hide complexity or internal details or hide implementation details to the user is known as abstraction in java.
For example:
The best example in the world of abstraction is ATM machine where we can withdraw or transfer money easily but how it happens. We don't know. We don't know internal details or implementation details.
Question 2. How To Achieve Abstraction In Java?
Answer :
There are two ways in java we can achieve abstraction:
Question 3. What Is Abstract Class In Java?
Answer :
When we declared any class with "abstract" keyword is known as abstract class. In abstract class we can keep abstract method (without body) and non-abstract method (with body).
For example:
abstract class Demo
{
abstract void show();//abstract method
void show(){}//non-abstract method
}
Question 4. Can We Create Instance Of Abstract Class?
Answer :
No, we cannot create an instance of an abstract class.
Question 5. Can We Define Abstract Class Without Abstract Method?
Answer :
Yes, we can define abstract class without abstract method.
Question 6. Can We Declare Abstract Method In Non-abstract Class?
Answer :
No, we can't declare abstract method in non-abstract class.
For example:
class Demo
{
abstract void show();
}
Above example will throw compile time error.
Question 7. What Is Interface In Java?
Answer :
An interface in java is a blueprint of a class that has static constants and abstract method by default.
Question 8. What Is Abstraction?
Answer :
Question 9. What Is An Abstract Class?
Answer :
These classes cannot be instantiated and are either partially implemented or not at all implemented.
This class contains one or more abstract methods which are simply method declarations without a body.
Question 10. When Is An Abstract Method Used?
Answer :
An abstract method is declared in the parent class when we want a class which contains a particular method but on the other hand we want its implementation to be determined by child class.
Question 11. Can An Interface Be Extended By Another Interface In Java?
Answer :
An interface can be extended by another interface in Java.
The code for the same would be like as shown below:
// this interface extends from the Body interface:
public interface FourLegs extends Body
{
public void walkWithFourLegs( );
}
Question 12. In Which Kind Of Situation Would An Interface Be Extended By Another Interface?
Answer :
Remember that any class that implements an interface must implement the method headings that are declared in that interface.
If that particular interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from.
As shown in the example above, if we have a class those implements the FourLegs interface, then that class must define the method headings in both the 'FourLegs' interface and the 'Body' interface.
Question 13. Why We Use Interface In Java?
Answer :
In java we use interface so that we can achieve fully abstraction because through abstract class we can't achieve full abstraction.
Question 14. Can We Create Instance Of Interface?
Answer :
No, we cannot create object of both an interface and abstract class.
Question 15. Can We Declare Abstract Method As Static?
Answer :
No, we can't use static keyword with abstract method.
Question 16. Can We Declare Abstract Method As Final?
Answer :
No, we cannot use final keyword with abstract class.
Question 17. Can We Declare Abstract Method As Private?
Answer :
No, we cannot declare abstract method as private.
Question 18. Can We Use Public, Protected And Default Modifiers With Abstract Method?
Answer :
Yes, we can use public, protected and default modifiers with abstract method.
Question 19. Differentiate An Interface And An Abstract Class?
Answer :
Question 20. What Is A Marker Interface?
Answer :
Marker interface is an interface with no fields or methods in Java.
Uses of marker interface are as follows:
For instance when we want to save the state of an object then we can implement serializable interface.
Question 21. What Will Happen If We Do Not Override All The Abstract Methods In Sub-class?
Answer :
It will throw compile-time error. We have to override all the abstract method in sub-class. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.
Question 22. Can An Inner Class Be Built In An Interface?
Answer :
Yes, an inner class can be built in an interface.
Example:
public interface xyz
{
static int p=0;
void m();
class c
{
c ()
{
int q;
System.out.println("inside");
};
public static void main(String c[])
{
System.out.println("inside ");
}
}
}
Question 23. How To Define An Abstract Class?
Answer :
A class containing abstract method is called an abstract class. An Abstract class cannot be instantiated.
Example of Abstract class:
abstract class test Abstract Class
{
protected String myString;
public String get String()
{
return myString;
}
public abstract string any Abstract Function();
}
Question 24. How Can We Define An Interface?
Answer :
In Java an interface just defines the methods and not implement them. Interface can include constants.
A class that implements the interfaces is bound to implement all the methods defined in an interface.
Example of Interface :
public interface sample Interface
{
public void function One();
public long CONSTANT_ONE = 1000;
}
Question 25. How Do We Use Comparator And Comparable Interfaces?
Answer :
java.util. Comparator
java.util.Comparator: compares some other classes instances.
java.lang. Comparable
java.lang.Comparable: compares another object with itself.
Java Abstraction Related Tutorials |
|
---|---|
Shell Scripting Tutorial | Adv Java Tutorial |
J2EE Tutorial | Core Java Tutorial |
JSP Tutorial | Data Structures Tutorial |
Adaptive software development Tutorial |
Java Abstraction Related Practice Tests |
|
---|---|
Shell Scripting Practice Tests | Adv Java Practice Tests |
J2EE Practice Tests | Core Java Practice Tests |
JSP Practice Tests | Data Structures Practice Tests |
Java Abstraction Practice Test
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.