Object Orienting Programming
(Java)
 Lab Manual No 05
 Dated:
 Semester:
 2024
Lab Instructor: SHEHARYAR KHAN Page 1
 57 | P a g e
Lab Objective:
In the lecture, you were described interface features of Object Oriented Programming. This lab
will help you understand the rules of defining programmer defined interfaces and how they
are implemented. We will explain how interfaces allow subclasses to exhibit multiple
inheritances which are not allowed normally 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. There can be only abstract methods
in the java interface not method body. It is used to achieve abstraction and multiple inheritance
in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
Understanding Interfaces
As you have already learned, objects define their interaction with the outside world through the
methods that they expose. Methods form the object's interface with the outside world; the
buttons on the front of your television set, for example, are the interface between you and the
electrical wiring on the other side of its plastic casing. You press the "power" button to turn the
television on and off.
In its most common form, an interface is a group of related methods with empty bodies. A
Window’s behavior, if specified as interface, might appear as follows:
Another associated interface in this scenario can be about position of window on screen. Its
interface can be defined as:
Lab Instructor: SHEHARYAR KHAN Page 2
 58 | P a g e
To implement these interfaces the name of your class would change (E.g. RectWindow) and you
would use the keyword “implements” in the class declaration.
Implementing an interface allows a class to become more formal about the behavior it promises to
provide. Interfaces form a contract between the class and the outside world, and this contract is
enforced at build time by the compiler. If your class claims to implement an interface, all methods
defined by that interface must appear in its source code before the class will successfully compile. A
class can implement any number of interfaces it desires unlike inheritance in which only one class can
be inherited. So in this way multiple inheritance is achieved through Interfaces.
Test class for understanding of above implementation is given below:
Lab Instructor: SHEHARYAR KHAN Page 3
 59 | P a g e
Why use 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.
Understanding 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.
Java Interface Example: Drawable
In this example, Drawable interface has only one method. Its implementation is provided by
Rectangle and Circle classes. In real scenario, interface is defined by someone but
implementation is provided by different implementation providers. And, it is used by someone
else. The implementation part is hidden by the user which uses the interface.
 //Interface declaration: by first user
 interface Drawable{
 void draw();
 }
 //Implementation: by second user
 class Rectangle implements Drawable{
 public void draw(){System.out.println("drawing
 rectangle");} }
 class Circle implements Drawable{
 public void draw(){System.out.println("drawing circle");}
}
Lab Instructor: SHEHARYAR KHAN Page 4
 59 | P a g e
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
Java Interface Example: Bank
Let's see another example of java interface which provides the implementation of Bank interface.
File: TestInterface2.java
 interface Bank{
 float rateOfInterest();
 }
 class SBI implements Bank{
 public float rateOfInterest(){return 9.15f;}
 }
 class PNB implements Bank{
 public float rateOfInterest(){return 9.7f;}
 }
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known
as multiple inheritance.
Lab Instructor: SHEHARYAR KHAN Page 5
 59 | P a g e
 interface Printable{
 void print();
 }
 interface Showable{
 void show();
 }
 class A7 implements Printable,Showable{
 public void print(){System.out.println("Hello");}
 public void show(){System.out.println("Welcome");}
 public static void main(String args[]){
 A7 obj = new A7();
 obj.print();
 obj.show();
}
}
• Multiple inheritance is not supported through class in java but it is possible by interface,
why?As we have explained in the inheritance chapter, multiple inheritance is not supported
in case of class because of ambiguity. But it is supported in case of interface because there is
no ambiguity as implementation is provided by the implementation class. For example:
 interface Printable{
 void print();
 }
 interface Showable{
 void print();
 }
 class TestTnterface3 implements Printable, Showable{
Lab Instructor: SHEHARYAR KHAN Page 6
 62 | P a g e
 public void print(){System.out.println("Hello");}
 public static void main(String args[]){
 TestTnterface1 obj = new TestTnterface1();
 obj.print();
}
}
Java 8 Default Method in Interface
Since Java 8, we can have method body in interface. But we need to make it default method.
Let's see an example:
 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();
}}
Lab Instructor: SHEHARYAR KHAN Page 7
 63 | P a g e
Lab Tasks
 Modify the vehicle inheritance program developed in last lab to include following
 interfaces with vehicle class implementing all of them. A Vehicle's behavior, if specified
 as an interface, might appear as follows:
 To implement this interface, the name of your class would change (to Automobile,
 for example), and you'd use the implements keyword in the class declaration:
 Modify the shape inheritance program developed in last lab to include following
 interfaces with shape class implementing all of them.
Lab Instructor: SHEHARYAR KHAN Page 8
 64 | P a g e
Lab Instructor: SHEHARYAR KHAN Page 9
 65 | P a g e
 Develop a runner program with the following specification
 Create any shape object of any coordinates and set its colour to java.awt.Color.PINK.
 Rotate it about some angle and redraw it. After that change the colour to
 java.awt.Color.RED and scale it by (2,1) and redraw it again.
 You are asked to develop the password validation system for UET network. The
 specifications given are as follows:
 a) Develop an interface named Validator. It has only one function setPassword(String
 s).
 b) Develop a class named User which implements Validator. Its specifications are as
 follow:
 It has following members
 String UserID: It stores user ID.
 String Password: It stores a valid user password.
 It has following constructors
 User( String userid) : It initializes UserID with argument string and set Password
 to Uet1234.
 User(String userid,String password) : It initializes UserID and Password. If
 password is not valid them Password is set
 to Uet1234.
Lab Instructor: SHEHARYAR KHAN Page 10
 66 | P a g e
It has following member functions
 Public void setPassword( String PWord):Same function implemented due to interface
 sets a valid Password.
 Public boolean isValid ( String PWord): Returns a value which indicates whether
 Password is valid or not
Validation Criteria
 Following validation criteria should be fulfilled.
 • A password must have a minimum length of 8 characters
 • A password must have a minimum of three, of these types of characters.
 An upper case character
 A lower case character
 A digit
 A special character ! , @, #, $ or %.
 • A password must not match 5 consecutive characters compared without case
 with previous password
 • A password must not match 5 consecutive characters compared without case
 with UserID.
Develop a program which instantiate an object of User class and set different valid and invalid
passwords and show class response.
Consider an asset-tracking program that will track four types of assets: electronic appliances,
automobiles, furniture, and compact disks. What classes would you design for the program?
Would you define four unrelated classes or one superclass and four subclasses? If you design a
superclass, will it be an abstract superclass?
Write an application that reads daily temperatures for 12 months and allows the user to get
statistics. Support at least three options: monthly average of a given month, yearly average,
and lowest and highest temperatures of a given month. Use a text file to store temperatures.
A line in the text file contains daily temperatures for one month. The first line in the text file
contains Temperatures for January; the second line, those for February; and so forth. Use
StringTokenizer to parse a line into temperatures of type float. For a data structure, consider
using either an array of Month or a two-dimensional array of float. Month is a class you define
yourself.
Lab Instructor: SHEHARYAR KHAN Page 11
 67 | P a g e