0% found this document useful (0 votes)
12 views122 pages

Abstract Classes N Interface

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views122 pages

Abstract Classes N Interface

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 122

Abstraction & Interfaces in Java

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstraction

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstraction in Java
Data Abstraction is the process of identifying only the
required characteristics of an object ignoring the irrelevant
details.

Example, when we ride a bike, we only know about


how to ride bikes but can not know about how it work? And
also we do not know the internal functionality of a bike.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Ways to achieve Abstraction

There are two ways to achieve abstraction in java


1. Abstract class (0 to 100%)
2. Interface (100%)

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract class in Java

A class which is declared with the abstract keyword is


known as an abstract class in Java. It can have abstract and
non-abstract methods (method with the body)

abstract class A{}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
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 have constructors and static methods also.
• It can have final methods which will force the subclass not
to change the body of the method.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract Method in Java

A method which is declared as abstract and does not


have implementation is known as an abstract method.

Syntax:
abstract void printStatus();//no method body and abstract

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract classes and Abstract methods :
• An abstract class is a class that is declared with
abstract keyword.
• An abstract method is a method that is declared without an
implementation.
• An abstract class may or may not have all abstract methods.
Some of them can be concrete methods
• Any class that contains one or more abstract methods must
also be declared with abstract keyword.
• There can be no object of an abstract class. That is, an
abstract class can not be directly instantiated with the
new operator.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
When to use abstract classes and abstract methods with
an example
Define a superclass that declares the structure of a
given abstraction without providing a complete
implementation of every method.

For example, We are doing Mathematical calculation such as


addition, multiplication and so on.., with the use of abstract
class and abstract method.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
abstract class Mathematics
{

// these are abstract methods


abstract double cal();

class add extends Mathematics


{
int a=5;
int b=5;

// @Override
double cal() {
return a+b;
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
class Mul extends Mathematics{

double x=5;
double y=5;

//@Override
double cal() {
return x*y;
}

}
public class Main
{
public static void main(String[] args)
{
Mathematics M1 = new add();
Mathematics M2 = new Mul();

}
} © 2018 SMART Training Resources Pvt. Ltd.
SMART TRAINING RESOURCES INDIA PVT. LTD.
Output:
10
25

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example of Abstract class that has an abstract method
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:
running safely

In this example, Bike is an abstract class that contains


only one abstract method run. Its implementation is provided
by the Honda class.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Another example of Abstract class in java
abstract class Car{
abstract void display();
}
//In real scenario, implementation is provided by others i.e.
unknown by end user
class model extends Car{
void display(){System.out.println("Car Model name: XYZ");}
}
class price extends Car{
void display(){System.out.println("Car price is 800000");}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
//In real scenario, method is called by programmer or user
class Main{
public static void main(String args[]){
Car c1 = new model();
Car c2=new price();
c1.display();
c2.display();
}
}
Output:
Car Model name: XYZ

Car price is 800000

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract class having constructor, data member and
methods

An abstract class can have a data member, abstract


method, method body (non-abstract method), constructor,
and even main() method.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
//Example of an abstract class that has abstract and non-
abstract methods
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
//Creating a Test class which calls abstract and non-abstract
methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Output:
bike is created
running safely..
gear changed
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Rule:
If there is an abstract method in a class, that class
must be abstract.

class Bike12{
abstract void run();
}
Output: Compile time error
Rule:
If you are extending an abstract class that has an
abstract method, you must either provide the implementation
of the method or make this class abstract.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Encapsulation vs Data Abstraction

• Encapsulation is data hiding(information hiding) while


Abstraction is detail hiding(implementation hiding).
• While encapsulation groups together data and methods
that act upon the data, data abstraction deals with
exposing the interface to the user and hiding the details of
implementation.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Advantages of Abstraction

• It reduces the complexity of viewing the things.


• Avoids code duplication and increases reusability.
• Helps to increase security of an application or program as
only important details are provided to the user

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Interfaces

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Interface in Java
An interface in Java is a blueprint of a class. It has
static constants and abstract methods.

The interface in Java is a mechanism to achieve


abstraction and multiple inheritance in Java.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
• Since Java 8, we can have default and static methods in
an interface.
• Since Java 9, we can have private methods in an
interface.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Use of Java interface
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.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Declaration of Interfaces:

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.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Syntax:

interface <interface_name>{

// declare constant fields


// declare methods that abstract
// by default.
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
A real-world example:

Let’s consider the example of vehicles like bicycle, car,


bike………, they have common functionalities. So we make an
interface and put all these common functionalities. And lets
Bicycle, Bike, car ….etc implement all these functionalities in
their own class in their own way.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
import java.io.*;

interface Vehicle {

// all are the abstract methods.


void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
class Bicycle implements Vehicle{

int speed;
int gear;

// to change gear
@Override
public void changeGear(int newGear){

gear = newGear;
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}
}

class Bike implements Vehicle {

int speed;
int gear;

// to change gear
@Override
public void changeGear(int newGear){

gear = newGear;
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
// to increase speed
@Override
public void speedUp(int increment){

speed = speed + increment;


}

// to decrease speed
@Override
public void applyBrakes(int decrement){

speed = speed - decrement;


}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}

}
class GFG {

public static void main (String[] args) {

// creating an inatance of Bicycle


// doing some operations
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
System.out.println("Bicycle present state :");
bicycle.printStates();

// creating instance of the bike.


Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);

System.out.println("Bike present state :");


bike.printStates();
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:

Bicycle present state :


speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Java 8 Interface Improvement
Since Java 8, interface can have default and static
methods which is discussed later.

Internal addition by the compiler


The Java compiler adds public and abstract keywords
before the interface method. Moreover, it adds public, static
and final keywords before data members.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
The relationship between classes and interfaces
As shown in the figure given below, a class extends
another class, an interface extends another interface, but
a class implements an interface.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Java Interface Example
interface printable{
void print();
}
class Java8 implements printable{
public void print(){System.out.println("Hello");}
}
class Main{
public static void main(String args[]){
Java8 obj = new Java8();
obj.print();
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:
Hello

In this example, the Printable interface has only one


method, and its implementation is provided in the Java8
class.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an
interface extends multiple interfaces, it is known as multiple
inheritance.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
interface Print{
void print();
}
interface Show{
void show();
}
class Multiple implements Print,Show{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
}
class Main{
public static void main(String args[]){
Multiple obj = new Multiple();
obj.print();
obj.show();
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Multiple inheritance is not supported through class in
java, but it is possible by an interface, why?

Multiple inheritance is not supported in the case of


class because of ambiguity. However, it is supported in case
of an interface because there is no ambiguity.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
interface Printable{
void print();
}
interface Showable{
void print();
}

class TestInterface3 implements Printable, Showable{


public void print(){System.out.println("Hello");}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
obj.print();
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:
Hello

As you can see in the above example, Printable and


Showable interface have same methods but its
implementation is provided by class TestTnterface1, so there
is no ambiguity.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Interface inheritance
interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}

public static void main(String args[]){


TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:
Hello
Welcome

A class implements an interface, but one interface


extends another interface.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Java 8 Default Method in Interface
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:
drawing rectangle
default method

Since Java 8, we can have method body in interface.


But we need to make it default method

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Marker or Tagged interface:
An interface which has no member is known as a
marker or tagged interface, for example, Serializable,
Cloneable, Remote, etc.

Syntax:
public interface Serializable{
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Nested Interface in Java

An interface i.e. declared within another interface or


class is known as nested interface. The nested interfaces are
used to group related interfaces so that they can be easy to
maintain. The nested interface must be referred by the outer
interface or class. It can't be accessed directly.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Points to remember for nested interfaces
There are given some points that should be remembered by
the java programmer.

• Nested interface must be public if it is declared inside the


interface but it can have any access modifier if declared
within the class.
• Nested interfaces are declared static implicitely

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Syntax of nested interface which is declared within the
interface

interface interface_name{
...
interface nested_interface_name{
...
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Syntax of nested interface which is declared within the
class

class class_name{
...
interface nested_interface_name{
...
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example of nested interface which is declared within the interface
interface Showable{
void show();
interface Message{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message{
public void msg(){System.out.println("Hello nested interface");}

public static void main(String args[]){


Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}
Output:hello nested interface

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example of nested interface which is declared within the class
class A{
interface Message{
void msg();
}
}

class TestNestedInterface2 implements A.Message{


public void msg(){System.out.println("Hello nested interface");}

public static void main(String args[]){


A.Message message=new TestNestedInterface2();//upcasting here
message.msg();
}
}
Output:hello nested interface
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Difference between abstract class and interface
Abstract class and interface both are used to achieve
abstraction where we can declare the abstract methods.
Abstract class and interface both can't be instantiated.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstract class Interface

1) Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods. Since Java 8, it can
have default and static methods also.

2) Abstract class doesn't support Interface supports multiple inheritance.


multiple inheritance.

3) Abstract class can have final, non- Interface has only static and final
final, static and non-static variables. variables.

4) Abstract class can provide the Interface can't provide the


implementation of interface. implementation of abstract class.

5) The abstract keyword is used to The interface keyword is used to declare


declare abstract class. interface.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
6) An abstract class can extend An interface can extend another
another Java class and implement Java interface only.
multiple Java interfaces.
7) An abstract class can be An interface can be
extended using keyword implemented using keyword
"extends". "implements".
8) A Java abstract class can Members of a Java interface are
have class members like private, public by default.
protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Example of abstract class and interface in Java
//Creating interface that has 4 methods
interface A{
void a();//bydefault, public and abstract
void b();
void c();
void d();
}

//Creating abstract class that provides the implementation of one method


of A interface
abstract class B implements A{
public void c(){System.out.println("I am C");}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
//Creating subclass of abstract class, now we need to provide the
implementation of rest of the methods
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}

//Creating a test class that calls the methods of A interface


class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Output:
I am a
I am b
I am c
I am d

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
MCQ’s

1. Which of these keywords are used to define an abstract


class?
a) abst
b) abstract
c) Abstract
d) abstract class

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: b

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
2. Which of these is not abstract?
a) Thread
b) AbstractList
c) List
d) None of the Mentioned

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: Thread is not an abstract class.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
3. If a class inheriting an abstract class does not define all of
its function then it will be known as?
a) Abstract
b) A simple class
c) Static class
d) None of the mentioned

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: Any subclass of an abstract class must either
implement all of the abstract method in the superclass or be
itself declared abstract.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
4. Which of these is not a correct statement?
a) Every class containing abstract method must be declared
abstract
b) Abstract class defines only the structure of the class not its
implementation
c) Abstract class can be initiated by new operator
d) Abstract class can be inherited

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: Abstract class cannot be directly initiated with
new operator, Since abstract class does not contain any
definition of implementation it is not possible to create an
abstract object.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
5. Which of these packages contains abstract keyword?
a) java.lang
b) java.util
c) java.io
d) java.system

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
6. What will be the output of the following Java code?
class A
{
public int i;
private int j;
}
class B extends A
{
void display()
{
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: d
Explanation: Class contains a private member variable j, this
cannot be inherited by subclass B and does not have access
to it.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
7. What will be the output of the following Java code?
class A
{
public int i;
public int j;
A()
{
i = 1;
j = 2;
}
}
class B extends A
{
int a;
B()
{
super();
}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
class super_use
{
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: Keyword super is used to call constructor of
class A by constructor of class B. Constructor of a initializes i
& j to 1 & 2 respectively.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
8. What will be the output of the following Java code?
class A
{
int i;
void display()
{
System.out.println(i);
}
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
class method_overriding
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: class A & class B both contain display() method,
class B inherits class A, when display() method is called by
object of class B, display() method of class B is executed
rather than that of Class A.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Which among the following best defines abstraction?
a) Hiding the implementation
b) Showing the important data
c) Hiding the important data
d) Hiding the implementation and showing only the features

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: d
Explanation: It includes hiding the implementation part and
showing only the required data and features to the user. It is
done to hide the implementation complexity and details from
the user. And to provide a good interface in programming.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Hiding the implementation complexity can ____________
a) Make the programming easy
b) Make the programming complex
c) Provide more number of features
d) Provide better features

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: It can make programming easy. The
programming need not know how the inbuilt functions are
working but can use those complex functions directly in the
program. It doesn’t provide more number of features or
better features.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Class is _________ abstraction.
a) Object
b) Logical
c) Real
d) Hypothetical

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: b
Explanation: Class is logical abstraction because it provides a
logical structure for all of its objects. It gives an overview of
the features of an object.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Object is ________ abstraction.
a) Object
b) Logical
c) Real
d) Hypothetical

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: Object is real abstraction because it actually
contains those features of class. It is the implementation of
overview given by class. Hence the class is logical abstraction
and its object is real.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstraction gives higher degree of ________
a) Class usage
b) Program complexity
c) Idealized interface
d) Unstable interface

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: It is to idealize the interface. In this way the
programmer can use the programming features more
efficiently and can code better. It can’t increase the program
complexity, as the feature itself is made to hide it.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstraction can apply to ____________
a) Control and data
b) Only data
c) Only control
d) Classes

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: Abstraction applies to both. Control abstraction
involves use of subroutines and control flow abstraction. Data
abstraction involves handling pieces of data in meaningful
ways.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Which among the following can be viewed as combination of
abstraction of data and code.
a) Class
b) Object
c) Inheritance
d) Interfaces

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: b
Explanation: Object can be viewed as abstraction of data and
code. It uses data members and their functioning as data
abstraction. Code abstraction as use of object of inbuilt class.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Abstraction principle includes___________
a) Use abstraction at its minimum
b) Use abstraction to avoid longer codes
c) Use abstraction whenever possible to avoid duplication
d) Use abstraction whenever possible to achieve OOP

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: Abstraction principle includes use of abstraction
to avoid duplication (usually of code). It this way the program
doesn’t contain any redundant functions and make the
program efficient.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Higher the level of abstraction, higher are the details.
a) True
b) False

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: b
Explanation: Higher the level of abstraction, lower are the
details. The best way to understand this is to consider a
whole system that is highest level of abstraction as it hides
everything inside. And next lower level would contain few of
the computer components and so on.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Encapsulation and abstraction differ as ____________
a) Binding and Hiding respectively
b) Hiding and Binding respectively
c) Can be used any way
d) Hiding and hiding respectively

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: Abstraction is hiding the complex code. For
example, we directly use cout object in C++ but we don’t
know how is it actually implemented. Encapsulation is data
binding, as in, we try to combine a similar type of data and
functions together.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
In terms of stream and files ____________
a) Abstraction is called a stream and device is called a file
b) Abstraction is called a file and device is called a stream
c) Abstraction can be called both file and stream
d) Abstraction can’t be defined in terms of files and stream

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: a
Explanation: Abstraction is called stream to provide a level of
complexity hiding, for how the files operations are actually
done. Actual devices are called file because in one way or
another, those can be considered as single entity and there is
nothing hidden.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
If two classes combine some private data members and
provides public member functions to access and manipulate
those data members. Where is abstraction used?
a) Using private access specifier for data members
b) Using class concept with both data members and member
functions
c) Using public member functions to access and manipulate
the data members
d) Data is not sufficient to decide what is being used

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: It is the concept of hiding program complexity
and actual working in background. Hence use of public
member functions illustrates abstraction here.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
A phone is made up of many components like motherboard,
camera, sensors and etc. If the processor represents all the
functioning of phone, display shows the display only, and the
phone is represented as a whole. Which among the following
have highest level of abstraction?
a) Motherboard
b) Display
c) Camera
d) Phone

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: d
Explanation: Phone as a whole have the highest level of
abstraction. This is because the phone being a single unit
represents the whole system. Whereas motherboard, display
and camera are its components.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Which among the following is not a level of abstraction?
a) Logical level
b) Physical level
c) View level
d) External level

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: d
Explanation: Abstraction is generally divided into 3 different
levels, namely, logical, physical and view level. External level
is not defined in terms of abstraction.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Using higher degree of abstraction __________
a) May get unsafe
b) May reduce readability
c) Can be safer
d) Can increase vulnerability

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Answer: c
Explanation: It will make the code safer. One may think it
reduces the readability, but the fact is, it actually helps us
understand the code better. We don’t have to read the
complex code which is of no use in understanding the
program.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
What is the syntax of abstract class in java?
A. abstract A{}
B. abstract class A
C. abstract class A{}
D. abstract class A[]

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Ans : C
Explanation:The syntax of abstract class in java is abstract
class A{}.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
What will be output for the folllowing code?
abstract class Bank {

private abstract void withdraw(); // Line 1


abstract void deposit();
public void balance(){} //Line 2
}
class office extends Bank{ // Line 3

void deposit() { // Line 4


// TODO Auto-generated method stub

}
}

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
A. Compilation error in Line 1(abstract method cannot be
private)
B. Compilation error in Line 2(abstract class cannot have
concrete method)
C. Compilation error in Line 3(abstract class cannot be
extended)
D. Compilation error in Line 4(deposit method should have
public access modifier)

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Ans : A
Explanation: Yes, you are right!! As private method can’t be
overridden and abstract method should be overridden in
child classes, so this line will give compilation error.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Which of these is not a correct statement?
A. Every class containing abstract method must be declared
abstract
B. Abstract class defines only the structure of the class not its
implementation
C. Abstract class can be initiated by new operator
D. Abstract class can be inherited

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Ans : C
Explanation: Abstract class cannot be directly initiated with
new operator, Since abstract class does not contain any
definition of implementation it is not possible to create an
abstract object.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
A method which is declared as abstract and does not have
implementation is known as an _____________?
A. Abstract Interface
B. Abstract Thread
C. Abstract List
D. abstract Method

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.
Ans : D
Explanation: A method which is declared as abstract and
does not have implementation is known as an abstract
method.

SMART TRAINING RESOURCES INDIA PVT. LTD. © 2018 SMART Training Resources Pvt. Ltd.

You might also like