ABSTRACTION Flashcards

1
Q

Abstract class in Java

A

A class which is declared with the abstract keyword is known as an abstract class inJava. It can have abstract and non-abstract methods (method with the body).
It’s needs to be extended and its method implemented. It cannot be instantiated.

Points to Remember

An abstract class must be declared with an abstract keyword.

It can have abstract and non-abstract methods.

It cannot be instantiated.

It can haveconstructorsand static methods also.

It can have final methods 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
2
Q

WHAT Is Abstraction ?

A

Abstractionis a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don’t know the internal processing about the message delivery.

Abstraction lets you focus on what theobjectdoes instead of how it does it.

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

Ways to achieve Abstraction

A

There are two ways to achieve abstraction in java

Abstract class (0 to 100%)

Interface (100%)

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

Abstract Method in Java

A

Abstract Method in Java

A method which is declared as abstract and does not have implementation is known as an abstract method.

Example of abstract method

abstractvoidprintStatus();//nomethodbodyandabstract

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

Example of Abstract class that has an abstract method

A

In this example, Bike is an abstract class that contains only one abstract method run. Its implementation is provided by the Honda class.

abstractclassBike{

abstractvoidrun();

}

classHonda4extendsBike{

voidrun(){System.out.println(“runningsafely”);}

publicstaticvoidmain(Stringargs[]){

Bikeobj=newHonda4();

obj.run();

}

}

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

Abstract class having constructor, data member and methods

A

An abstract class can have a data member, abstract method, method body (non-abstract method), constructor, and even main() method.

File: TestAbstraction2.java

//Exampleofanabstractclassthathasabstractandnon-abstractmethods

abstractclassBike{

Bike(){System.out.println(“bikeiscreated”);}

abstractvoidrun();

voidchangeGear(){System.out.println(“gearchanged”);}

}

//CreatingaChildclasswhichinheritsAbstractclass

classHondaextendsBike{

voidrun(){System.out.println(“runningsafely..”);}

}

//CreatingaTestclasswhichcallsabstractandnon-abstractmethods

classTestAbstraction2{

publicstaticvoidmain(Stringargs[]){

Bikeobj=newHonda();

obj.run();

obj.changeGear();

}

}

Test it Now

bike is created running safely.. gear changed

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

What is an INTERFACE?

A

Aninterface in Javais a blueprint of a class. It has static constants and abstract methods.

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

Interface in Java

A

Aninterface in Javais a blueprint of a class. It has static constants and abstract methods.

The interface in Java isa mechanism to achieveabstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multipleinheritance in Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.
It cannot be instantiated just like the abstract class.

Since Java 8, we can havedefault and static methodsin an interface.

Since Java 9, we can haveprivate methodsin an interface.

Java Interface alsorepresents the IS-A relationship.

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

Why use Java interface?

A

There are mainly three reasons to use interface. They are given below.

It is used to achieve abstraction.

By interface, we can support the functionality of multiple inheritance.

It can be used to achieve loose coupling

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

How to declare an interface?

A

An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the 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.
Syntax:
interface<interface_name>{</interface_name>

//declareconstantfields

//declaremethodsthatabstract

//bydefault.

}

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

The relationship between classes and interfaces

A

As shown in the figure given below, a class extends another class, an interface extends another interface, but aclass implements an interface.

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

Java Interface Example

A

this example, the Printable interface has only one method, and its implementation is provided in the A6 class.

interfaceprintable{

voidprint();

}

classA6implementsprintable{

publicvoidprint(){System.out.println(“Hello”);}

publicstaticvoidmain(Stringargs[]){

A6obj=newA6();

obj.print();

}

}

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

Multiple inheritance in Java by interface

A

Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance

interfacePrintable{

voidprint();

}

interfaceShowable{

voidshow();

}

classA7implementsPrintable,Showable{

publicvoidprint(){System.out.println(“Hello”);}

publicvoidshow(){System.out.println(“Welcome”);}

publicstaticvoidmain(Stringargs[]){

A7obj=newA7();

obj.print();

obj.show();

}

}

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

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

A

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case ofclassbecause of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

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

Interface inheritance

A

A class implements an interface, but one interface extends another interface.

interfacePrintable{

voidprint();

}

interfaceShowableextendsPrintable{

voidshow();

}

classTestInterface4implementsShowable{

publicvoidprint(){System.out.println(“Hello”);}

publicvoidshow(){System.out.println(“Welcome”);}

publicstaticvoidmain(Stringargs[]){

TestInterface4obj=newTestInterface4();

obj.print();

obj.show();

}

}

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

Java 8 Default Method in Interface

A

Since Java 8, we can have method body in interface. But we need to make it default method. Let’s see an example:

File: TestInterfaceDefault.java

interfaceDrawable{

voiddraw();

defaultvoidmsg(){System.out.println(“defaultmethod”);}

}

classRectangleimplementsDrawable{

publicvoiddraw(){System.out.println(“drawingrectangle”);}

}

classTestInterfaceDefault{

publicstaticvoidmain(Stringargs[]){

Drawabled=newRectangle();

d.draw();

d.msg();

}}