Abstraction Flashcards

1
Q

What is abstract class?

A
A class which is declared with abstract word is known as an abstract class in Java. 
It can have abstract and non-abstract methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is abstraction ?

A

Abstraction is the process of hiding the implementation details and showing only functionality to the user
Abstract lets you focus on what the object does instead of how it does it

for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message delivery

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

What are the ways to achieve abstraction ?

A
  1. Abstract class

2. Interface

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

What is Abstract class ?

A

Points to remember
• Abstract class must be declared with abstract keyword
• It can have abstract and non-abstract methods
• It cannot be instantiated
• It can have constructors and static methods too
It can have final keyword which will force the subclass not to change the body of the method

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

What is abstract method

A

A method that is declared as abstract and does not have implementation is known as abstract method

Example:-
Abstract void printStatus(); //no method body and abstract

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

What is an interface in Java ?

A

Blueprint of the class.
It has static constants and abstract methods
The interface in Java is mechanism to achieve abstraction.
Java interface also represents the IS-A relationship

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

What can not be in interface?

A

Interfaces can have abstract methods and variables. It cannot have a method body.

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

Why use Java interface ?

A
  • It is used to achieve abstraction
  • By interface we can support functionality of multiple inheritance
  • Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to declare java interface

A

It is declared using interface keyword
It provides total abstraction means all the methods in the interface are declared with empty body and all the fields are public static and final by default

A class that implements an interface must implement all the methods declared in the interface.

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

Internal addition by compiler when using interface

A

The java compiler adds public and abstract keywords before the interface methods and public, static and final before the interface fields

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

Multiple inheritance is not supported through class in Java, but is possible by an interface why?

A

As we know that multiple inheritance u not supported in the case of class because of ambiguity. However it is supported in the case of an interface because there is no ambiguity. It is because implementation is provided by other implementation class

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

Common things between abstract class and interface

A

Abstract class and interface both are used achieve abstraction, where we can declare abstract method. Abstract class and abstract method both can’t be instantiated

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

Differences between abstract class and interface

A
  1. can have abstract and non-abstract methods
  2. Interface can only have abstract methods.
  3. Doesn’t support multiple inheritance
  4. Supports multiple inheritance
  5. can have final, non-final, static, non-static variables
  6. can only have static and final variables
  7. abstract class can extends another java class and implement multiple java interfaces
  8. java interface can only extend another java interface
  9. abstract class can have class members like private, protected, etc
  10. Members of java interface are public by default
How well did you know this?
1
Not at all
2
3
4
5
Perfectly