Lab-2 - Interfaces
Lab-2 - Interfaces
Lab-2 - Interfaces
Objective:
Develop the Java programs to demonstrate the use of interfaces.
Theory:
Interfaces in Java
An Interface in Java programming language is defined as an abstract type used to specify the
behavior of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains
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 the method body. It is used to achieve abstraction and multiple inheritance
in Java. In other words, you can say that interfaces can have abstract methods and variables. It
cannot have a method body. Java Interface also represents the IS-A relationship. When we decide a
type of entity by its behaviour and not via attribute we should define it as an interface.
Like a class, an interface can have methods and variables, but the methods declared in an interface
are by default abstract (only method signature, no body).
• Interfaces specify what a class must do and not how. It is the blueprint of the behaviour.
• Interface do not have constructor.
• Represent behaviour as interface unless every sub-type of the class is guaranteed to have
that behaviour.
• An Interface is about capabilities like a Player may be an interface and any class
implementing Player must be able to (or must implement) move(). So it specifies a set of
methods that the class has to implement.
• If a class implements an interface and does not provide method bodies for all functions
specified in the interface, then the class must be declared abstract.
• A Java library example is Comparator Interface. If a class implements this interface, then it
can be used to sort a collection.
Syntax:
Interface name {
// declare constant fields
// declare methods that abstract
// by default.
}
29
PCO692C EES, University Polytechnic, AMU
To declare an interface, use the interface keyword. It is used to provide total abstraction. That
means all the methods in an interface are declared with an empty body and are public and all fields
are public, static, and final by default. A class that implements an interface must implement all the
methods declared in the interface. To implement interface use implements keyword.
// A simple interface
interface Player {
final int id = 10;
int move();
}
1. In class, you can instantiate variables and create an object. In an interface, you can’t
instantiate variables and create an object.
2. Class can contain concrete(with implementation) methods. The interface cannot contain
concrete(with implementation) methods
3. The access specifiers used with classes are private, protected, and public. In Interface only
one specifier is used- Public.
// A simple interface
interface In1 {
30
PCO692C EES, University Polytechnic, AMU
// Driver Code
public static void main(String[] args) {
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
Output
Hello
10
Real-World Example: Let’s consider the example of vehicles like bicycle, car, bike etc., they
have common functionalities. So we make an interface and put all these common functionalities.
And let Bicycle, Bike, Car etc. implement all these functionalities in their own class in their own
way.
interface Vehicle {
// all are the abstract methods.
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}
// to change gear
@Override
public void changeGear(int newGear) {
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
31
PCO692C EES, University Polytechnic, AMU
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
}
class VehicleDemo {
public static void main (String[] args) {
32
PCO692C EES, University Polytechnic, AMU
• Without bothering about the implementation part, we can achieve the security of the
implementation.
• In Java, multiple inheritances is not allowed, however, you can use an interface to make use
of it as you can implement more than one interface.
33
PCO692C EES, University Polytechnic, AMU
Exercise 1:
int cashSaving;
int retirementFund;
String firstName;
String lastName;
34
PCO692C EES, University Polytechnic, AMU
System.out.println("personinterface1.getName() = " +
personinterface1.getName() + "," +
" personinterface1.computeTotalWealth() = " +
personinterface1.computeTotalWealth());
System.out.println("personinterface2.getName() = " +
personinterface2.getName() + "," +
" personinterface2.computeTotalWealth() = " +
personinterface2.computeTotalWealth());
boolean b2 = (person1 == personinterface2);
System.out.println("Do person1 and personinterface2 point to the same
object instance? " + b2);
}
}
35
PCO692C EES, University Polytechnic, AMU
Exercise 2:
36
PCO692C EES, University Polytechnic, AMU
// The following line of code will generate a compile error since line3
// is declared as RelationInterface interface type not Line type
// and the getLength() method is not one of the methods defined
// in the RelationInterface interface. It is commented out for now.
// System.out.println("Length of line3 is " + line3.getLength());
}
}
4. Compile and run the program
line1 is greater than line2: false
line1 is equal with line2: true
line1 is equal with line3: false
Length of line1 is 1.4142135623730951
Length of line2 is 1.4142135623730951
5. Uncomment the last commented line of Main.java containing line3.getLength() and note
the compiler error. Understand why the compiler error occurs.
37
PCO692C EES, University Polytechnic, AMU
2. Modify Person.java as shown below. The change is that the Person class implements the
AnotherInterfaceExample interface in addition to the PersonInterface interface. The code
snippet that is required to be added is shown in blue.
package ENo_PersonInterfacePkg;
38
PCO692C EES, University Polytechnic, AMU
3. Modify Main.java as shown below. The change is so that the instance of the object of class
Person calls the method defined in the new interface. The code snippet that needs to be changed is
shown in blue.
package ENo_PersonInterfacePkg;
System.out.println("personinterface1.getName() = " +
personinterface1.getName() + "," +
" personinterface1.computeTotalWealth() = " +
personinterface1.computeTotalWealth());
}
}
39
PCO692C EES, University Polytechnic, AMU
40