Lab 2 Abstract Classes and Interfaces
Lab 2 Abstract Classes and Interfaces
Lab-02
Abstract classes and Interfaces
Lab 2: Abstract Classes and Interfaces
Table of Contents
1. Introduction 15
3. Concept Map 16
3.1. Super class 16
3.2. Encapsulation 16
3.3. Abstract class 16
3.4. Abstract method 17
3.5. Interface 17
3.6. Extending Interfaces 18
4. Procedure& Tools 18
4.1. Tools 18
4.2. Setting-up JDK 1.7 18
4.2.1. Compile a Program 18
4.2.2. Run a Program 19
4.3. Walkthrough Task 19
4.3.1. Implementing Abstract Class 19
4.3.2. Implementing Interfaces 20
5. Practice Tasks 21
5.1. Practice Task 1 21
5.2. Practice Task 2 22
5.3. Practice Task 3 22
5.4. Practice Task 4 22
5.5. Practice Task 5 22
5.6. Out comes 22
5.7. Testing 22
6. Further Reading 23
6.1. Books 23
6.2. Slides 23
Page 14
Lab 2: Abstract Classes and Interfaces
1. Introduction
You have learnt Java constructs, abstract classes and interfaces. In this lab, you will test the
theory by implementing the abstract classes and interfaces. A class in which one or more
method is declared but not defined is known as abstract class. Any method which is
declared but not defined is known as abstract method. Following example shows how to
declare an abstract class.
For the above class we cannot create an object as it is an abstract class and the class
inheriting this abstract class must define the methods which were declared but not defined
in the class. On the other hand, in an interface all the methods are abstract. Therefore,
instead of defining all the methods abstract a simple way is to replace the “class” keyword
with “interface”.
For example, remote controlfor multimedia devices has some common functionalities to be
performed. As you know that there are many appliances commonly used in home that have
some common features and anyone who will use any remote will expect some common
features to be in a remote. This means that remote is some standardized thing. If you try to
define a common interface for remote then it might look as follows.
interface Remote {
void volumeDown();
void volumeUp();
void forward();
void rewind();
}
Any class which uses this interface must implement all its methods. Based on need and
requirement any class can use this interface by using the keyword “implements” instead of
“extends”. It is important to note here that after “implements” keyword we can use as many
Page 15
Lab 2: Abstract Classes and Interfaces
interfaces as we like but any class can extend only one class. Multiple inheritance that was
supported by C++ has been removed by Java.
To get basic understanding of Object Oriented concepts and how they are
implemented using Java.
To write programs for polymorphic scenarios and learn how to compile and run it
To get an understanding of identifying basic errors.
To understand the command line arguments and their purpose.
3. Concept Map
This section provides you the overview of the concepts that will be discussed and
implemented in this lab.
Any class which is extended or in other words inherited is known as base class. In Java, the
base class is called the super class. You can initialize the fields of super class by calling
super() method, which calls the constructor of the super class.
3.2. Encapsulation
Abstract classes are used to define only part of an implementation. Because, information is
not complete therefore an abstract class cannot be instantiated.
Page 16
Lab 2: Abstract Classes and Interfaces
Like simple classes, abstract class can also contain instance variables and methods that are
completely defined. The class that inherits an abstract class must provide details i.e.
implementation of undefined methods.
A method that has no implementation similar to pure virtual function in C++. Any class
with an abstract method must be declared abstract. If a subclass provides implementation
of all abstract methods of the super class, only then we can create an object of subclass.
Such class is also known as concrete class.
It is important to understand that the reference of abstract class can point to its sub class
that is concrete class. This concept gives rise to the concept of dynamic binding studied in
OOP course.
3.5. Interface
Similar to Abstract class, interfaces imposes a design structure on any class that uses the
interface. Unlike inheritance, a class can implement more than one interface. To do this;
separate the interface names with commas. This is Java’s way of multiple inheritance.
We cannot instantiate object of interface like the abstract classes. Figure 1 shows
characteristics of an interface
However, a reference of interface can point to any of its implementation class. Static
methods cannot be declared in the interfaces.
You can choose to omit implementing the few methods declared in interface but in this
scenario a class implementing the interface will become an abstract class and you will have
to explicitly mention abstract keyword in the definition of such class.
Page 17
Lab 2: Abstract Classes and Interfaces
Java allows one interface to extend or inherit another interface. Following example shows
how to do that.
4. Procedure& Tools
4.1. Tools
javac Interface.java
After the execution of the above statement byte code of your class will be generated with
same name as the .java file but its extension will change to .class.
Similarly, compile all classes implementing the interfaces.
Now you will need JVM to run the program.
Page 18
Lab 2: Abstract Classes and Interfaces
After successfully completing the section 6.2.1, the only thing left is to execute the byte
code on the JVM. Run the file containing main method in it. Use the following command to
run the program.
java DriverClass
If the above command executed successfully then you will see output of the program.
This task is designed to guide you towards creating your own abstract class and interface
and running the program.
This example shows you how to create an abstract class and use it. For this example, we
have used a well-known polymorphism example of shape class. The Shape class contains
an abstract method calculateArea() which is abstract. Class Circle extends from abstract
Shape class, therefore to become concrete class it must provide the definition of
calculateArea() method.
Page 19
Lab 2: Abstract Classes and Interfaces
4. Save the file by the name of Circle.java. This is the class which has implemented the
abstract class.
The Test class contains main method. Inside main, a reference s Shape class is created.
This reference can point to Circle asdiscussed earlier. By using reference, method
calculateArea() of circle class can be invoked.
5. Open the notepad again and type the following code and save the file by the name of
Test.java.
6. Compile all the classes and run the test class to test the program.The compilation and
execution of the above program is shown in figure 2.
Page 20
Lab 2: Abstract Classes and Interfaces
To implement the interface, you will have to follow the following steps.
1. Open Notepad and type the following code, finally save the file by the name of
PrintableInterface.java
2. Class AcpStudent implement that interface. AcpStudent class has to provide the
definition of print method or we are unable to compile the code successfully.Open
another Notepad to type the following code.
3. Finally write a test class as written for abstract class and run the program.
5. Practice Tasks
This section will provide more practice exercises related to development. You need to finish the
tasks in the required time. When you finish them, put these tasks into the
https://moellim.riphah.edu.pk/.
Create a class Glass with attributes opacity and thickness. The class contains an abstract
method show. Extend a class WindowGlass from Glass and add two new attributes width
and height. Override the show method that displays all the attributes. Create a class
CarWindowGlass that extends WindowGlass and also define its own show method that
displays all the attributes.
Page 21
Lab 2: Abstract Classes and Interfaces
Create an interface of car rentals offering multiple companies that rents cars. It can be
implemented by multiple companies and offer services to rent cars of customer’s choice.
Create an abstract class Vehicle with attributes name, model, brand and number_of_wheels.
Create an abstract method display() .
Create two concrete classes Car & Bike that extends Vehicle and implements
VehicleFunctions.
Search or create three interfaces in the java and make a test program to check these
interfaces.
Part - 1: Search at least three interfaces (other than example given below) in JAVA docs at
http://docs.oracle.com/javase/7/docs/api/ and list its all methods like this: EventListener
and its method is handleEvent,
Part - 2: Then make a class that implements any of the interfaces you found in Q 5 Part -1,
try to execute program without implementing any of the interface’s method and list down
the error you receive.
After completing this lab, student will be able to understand the use of abstract class and
interface.
5.7. Testing
This section provides you the test cases to test the working of your program. If you get the
desired mentioned outputs for the given set of inputs then your program is right.
Page 22
Lab 2: Abstract Classes and Interfaces
The test cases for other tasks will be provided by your lab instructor when you will finish
your tasks.
6. Further Reading
6.1. Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide: http://www.oracle.com/events/global/en/java-
outreach/resources/java-a-beginners-guide-1720064.pdf
6.2. Slides
The slides and reading material can be accessed from the folder of the class instructor
available at https://moellim.riphah.edu.pk/
Page 23