Java Interview Questions 7 of 9 - Abstraction Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is Abstraction in Object Oriented programming?

A

Abstraction is the process of hiding certain implementation details of an object and showing only essential features of the object to outside world.

It is different from Abstract class in Java.

Abstraction process identifies commonalities and hides the complexity of implementation. It helps us in focusing on the interface that we share with the outside world.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is Abstraction different from Encapsulation?

A

Abstraction happens at class level design. It results in hiding the implementation details. Encapsulation is also known as “Information Hiding”. An example of encapsulation is marking the member variables private and providing getter and setter for these member variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an abstract class in Java?

A

An abstract class in Java has one or more abstract methods. An abstract method is just declared in the abstract class, but it is not implemented.

An abstract class has to be extended in Java and its abstract methods have to be implemented by a child class. Also Java does not allow new instance of Abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is it allowed to mark a method abstract method without marking the class abstract?

A

No. Java specification says that if there is at least one abstract method in a class, the class has to be marked abstract.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is it allowed to mark a method abstract as well as final?

A

No. It will be contradictory statement to mark a method abstract as well as final.

An abstract method has to be overridden by a child class. And a final method cannot be overridden. Therefore a method can be either abstract or final in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can we instantiate an abstract class in Java?

A

No. We cannot create an instance of an abstract class in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an interface in Java?

A

An Interface in Java is an abstract type blueprint of a class. It contains the methods that a class must implement. It is like a protocol.

It has method signatures and constant declarations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is it allowed to mark an interface method as static?

A

Yes, from Java 8 onwards, we can define static and default methods in an interface. Prior to Java 8, it was not allowed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why an Interface cannot be marked as final in Java?

A

A final method cannot be overridden. But an interface method has to be implemented by another class. So the interface method cannot be marked as final.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a marker interface?

A

There are interfaces that do not have any data member or methods. These interfaces are called Marker interface.

E.g. Serializable, Cloneable, Remote etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What can we use instead of Marker interface?

A

We can use annotations instead of Marker interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How Annotations are better than Marker Interfaces?

A

Annotations serve the purpose of conveying metadata about the class to its consumers without creating a separate type for it.

Annotations are more powerful than a Marker interface. They allow programmers to pass more sophisticated information to classes that “consume” it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between abstract class and interface in Java?

A

Differences between Abstract class and Interface are as follows:

  1. An abstract class can have implemented methods with body (non-abstract methods). Interface has only abstract methods. From Java 8 onwards, interface can have static/default methods in implemented form.
  2. An abstract class can have instance member variables. An interface cannot have instance variables. It can only have constants.
  3. An abstract class can have a constructor. Interface cannot have constructor. It has to be implemented by another class.
  4. A class can extend only one abstract class. A class can implement more than one interface.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Does Java allow us to use private and protected modifiers for variables in interfaces?

A

No. All the variables in an interface are implicitly public.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can we cast to an object reference to an interface reference?

A

An Object that implements an Interface can be cast to the same Interface. Since An Object implementing an Interface already provides implementation for the methods of that Interface, it is allowed to do so as per the rules of Inheritance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly