You are on page 1of 6

Java Programming Lab (PCCET501P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

Practical No. 03

Aim:
Develop an interface to calculate and print the area of different 2D
Shapes. Give name of the interface as “Area”. Create a menu driven
program to provide the user menu to choose and calculate Area of
different 2D shapes such as circle, triangle and rectangle etc.

Name of Student: Poonam Tiwari

Roll No.: B52

Semester/Year: 5th/ 3rd

Academic Session: 2022 - 23

Date of Performance: __________

Date of Submission: __________

Department of Electronics & Telecommunication Engineering, S.B.J.I.T.M.R,


Nagpur. 1
Java Programming Lab (PCCET501P)
AIM: Develop an interface to calculate and print the area of different 2D Shapes. Give name of
the interface as “Area”. Create a menu driven program to provide the user menu to choose and
calculate Area of different 2D shapes such as circle, triangle and rectangle etc.

OBJECTIVE/EXPECTED LEARNING OUTCOME:


The objectives and expected learning outcome of this practical are:
 To understand the creation of an Interface.
 To get acquainted with the use of Interfaces into different classes.
 To understand difference between a Method and an Interface.

THEORY:

Interface:
An interface in Java is a blueprint of a class. The interface in Java is a mechanism to
achieve abstraction. There can be only abstract methods in the Java interface, not method body.
It is used to achieve abstraction and multiple inheritance in Java. It cannot have a method body.
Java Interface also represents the IS-A relationship.

Why use Java interface?

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

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

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.

Department of Electronics & Telecommunication Engineering, S.B.J.I.T.M.R,


Nagpur. 2
Java Programming Lab (PCCET501P)
FLOWCHART:

Start

Declare an Interface “Area”

Create a class “Circle” implementing “Area”


Interface

Create a class “Rectangle” implementing


“Area” Interface

Create a Driver class “Main” and define main()

Create Object in main()

Read the user input

Calculate and Display the


Appropriate Area

Stop

Department of Electronics & Telecommunication Engineering, S.B.J.I.T.M.R,


Nagpur. 3
Java Programming Lab (PCCET501P)
PROGRAM:

OUTPUT:

Department of Electronics & Telecommunication Engineering, S.B.J.I.T.M.R,


Nagpur. 4
Java Programming Lab (PCCET501P)

CONCLUSION: We have learnt and understand the concept and implementation of an


Interface. We have also understood the difference between a Method and an Interface.

DISCUSSION QUESTIONS:
1) Give the difference between a Method and an Interface.

2) What are the different ways to implement the interfaces?


Ans)
1. Abstract Classes: An abstract class is a class that contains at least one abstract method, which is
a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and
must be extended by another class that provides implementations for the abstract methods.
2. Interfaces: An interface is a reference type in Java that is similar to a class. It contains constants,
method signatures, and nested types. Unlike a class, an interface cannot contain method
implementations.
3. Implementing Multiple Interfaces: A class can implement multiple interfaces, as long as the
methods defined in each interface are implemented.
4. Default Methods: Java 8 introduced the concept of default methods, which allow interfaces to
have implementations.

Department of Electronics & Telecommunication Engineering, S.B.J.I.T.M.R,


Nagpur. 5
Java Programming Lab (PCCET501P)
5. Static Methods: An interface can also contain static methods, which do not need to be
implemented by a class that implements the interface.
6. Private Methods: Java 9 introduced the concept of private methods, which allow interfaces to
have private methods that can only be used within the interface.

3) Can we use an Interface to implement polymorphism.


Ans) Yes, an interface can be used to implement polymorphism. Polymorphism can be
implemented by creating multiple classes that implement the same interface. This allows the
same interface to be used to invoke different methods in different classes, depending on the
context.

REFERENCE:

1. “Programming with Java”, E Balagurusamy, 6thEdition, 2019, McGraw Hill Education.


2. “Java -The Complete reference”, Herbert Schildt, 11thEdition, 2018, McGraw Hill
Education.
3. “Thinking in Java”, Bruce Eckel, 4th Edition, 2008, Prentice Hall.
4. “JavaTM How To Program”, Deitel Paul, Deitel Harvey, 10th Edition, 2014, PHI Learning.

Department of Electronics & Telecommunication Engineering, S.B.J.I.T.M.R,


Nagpur. 6

You might also like