Professional Documents
Culture Documents
1. Interfaces in Java
2. Comparable and Comparator Interface
3. Inner classes
1 Interfaces in Java
A Java interface is a 100% abstract superclass, which define a set of methods its subclasses must
support. An interface contains only public abstract methods (methods with signature and no
implementation) and possibly constants (public static final variables). You have to use the keyword
"interface" to define an interface (instead of keyword "class" for normal classes). The keyword public
and abstract are not needed for its abstract methods, as they are mandatory.
An interface is a contract for what the classes can do. It, however, does not specify how the classes
should do it.
Interface Naming Convention: Use an adjective (typically ends with "able") consisting of one or
more words. Each word shall be initial capitalized (camel-case). For example, Serializable,
Externalizable, Movable, Clonable, Runnable, etc.
Example: Movable Interface and its Implementation
Suppose that our application involves many objects that can move. We could define an interface called
movable, containing the signatures of the various movement methods.
CS F213 (Lab-6)
Page 1 of 9
Interface Moveable.java
public interface Movable {
// abstract methods to be implemented by the subclasses
public void moveUp();
public void moveDown();
public void moveLeft();
public void moveRight();
}
CS F213 (Lab-6)
Page 2 of 9
x++;
}
}
Other classes in the application can similarly implement the Movable interface and provide their own
implementation to the abstract methods defined in the interface Movable.
TestMovable.java
We can also upcast subclass instances to the Movable interface, via polymorphism, similar to an
abstract class.
public class TestMovable {
public static void main(String[] args) {
Movable m1 = new MovablePoint(5, 5); // upcast
System.out.println(m1);
m1.moveDown();
System.out.println(m1);
m1.moveRight();
System.out.println(m1);
}
}
CS F213 (Lab-6)
Page 3 of 9
All methods in an interface shall be public and abstract (default). You cannot use other access
modifier such as private, protected and default, or modifiers such as static, final.
All fields shall be public, static and final (default).
An interface may "extends" from a super-interface.
UML Notation: The UML notation uses a solid-line arrow linking the subclass to a concrete or
abstract superclass, and dashed-line arrow to an interface as illustrated. Abstract class and abstract
method are shown in italics.
Exercise1:
Interface
Movable
MovablePoint and MovableCircle
and
its
implementations
Suppose that we have a set of objects with some common behaviors: they could move up, down,
left or right. The exact behaviors (such as how to move and how far to move) depend on the objects
themselves. One common way to model these common behaviors is to define an interface called
Movable, with abstract methods moveUp(), moveDown(), moveLeft() and moveRight(). The
classes that implement the Movable interface will provide actual implementation to these abstract
methods.
Let's write two concrete classes - MovablePoint and MovableCircle - that implement the Movable
interface.
CS F213 (Lab-6)
Page 4 of 9
For the MovablePoint class, declare the instance variable x, y, xSpeed and ySpeed with package
access as shown with '~' in the class diagram (i.e., classes in the same package can access these
variables directly). For the MovableCircle class, use a MovablePoint to represent its center (which
contains four variable x, y, xSpeed and ySpeed). In other words, the MovableCircle composes a
MovablePoint, and its radius.
//Complete the implementation
public class MovablePoint implements Movable {
// instance variables
int x, y, xSpeed, ySpeed;
// package access
// Constructor
public MovablePoint(){}
public MovablePoint(int x, int y, int xSpeed, int ySpeed) {
this.x = x;
}
// Implement abstract methods declared in the interface Movable
public void moveUp() {
y -= ySpeed;
// y-axis pointing down for 2D graphics
}
}
//Complete the implementation
CS F213 (Lab-6)
Page 5 of 9
// upcast
Q.2 Modify your code of the MovablePoint class, so that two MovablePoint objects can be
compared on the basis of their x and y coordinates.
Q.3 Modify your code of the MovableCircle class so that two MovableCircle objects can be
compared on the basis of their (a) radius (b) center and (c) radius and center.
3. Nested classes
Classes that are declared inside the body of a class are called "nested classes".
The following are the main reasons behind the concept of nested classes in Java:
a. Grouping of logical classes
When designing a class, we always try to create well-focused classes - with a
unique behavior and purpose. Let us imagine we have designed classes A and
B on the same principle. Later we found, class B can only be used by class A.
In such cases, we can simply put class B inside class A.
b. Encapsulation
By writing nested classes, we have hidden them from the world and made them
available only to their enclosing class.
CS F213 (Lab-6)
Page 6 of 9
1.
2.
3.
4.
5.
6.
4. Inner Class
1. We define the term "inner class" to the nested class that is:
a. declared inside the body of another class.
b. not declared inside a method of another class.
c. not a static nested class.
d. not an anonymous inner class.
2. An example:
class Outer{
class Inner{
}
}
3. When we compile the above code we get 2 class files:
o Outer.class
o Outer$Inner.class
4. Notice that inner class is tied to its outer class though it is still a separate class.
5. An inner class cannot have any kind of static code including the public static void main(String[]
args).
6. Only classes with "public static void main(String[] args)" can be called using "java" command.
7. In our earlier example, Inner class didn't have a static main method. So, we can't call java
Outer$Inner!
8. The inner class is just like any other member of its enclosing class.
9. It has access to all of its enclosing class' members including private.
CS F213 (Lab-6)
Page 7 of 9
/* It convert Decimal Number into binary. It stores binary representation of decimal number into
ArrayList. In ArrayList each index stores either 0 or 1. Suppose if N is +8. It stores [0, 0, 0, 0, 1,
0, 0, 0] in an ArrayList and N range is from -127 to +127.
Note: The number N is represented in 8-bit format. */
b. Define the class name OneComplement which encapsulates Ones complement binary
representation of decimal number. This class also implements interface BinaryInterface.The
class OneComplement is described as follows:
CS F213 (Lab-6)
Page 8 of 9
Attribute:
+ Arr : ArrayList<Integer> // It stores the ones complement binary representation of decimal number.
Operation:
+ toBinary (N: int ): void
/* This method converts the decimal number N into ones compliment binary
representation in an ArrayList Arr. */
c. Define the class name SignedMagnitude which encapsulates Ones complement binary
representation of decimal number. This class also implements interface BinaryInterface. The
class SignedMagnitude is described as follows:
Attribute:
+ Arr : ArrayList<Integer> // It stores the signed magnitude binary representation of decimal number.
Operation:
+ toBinary (N: int ): void
/* This method converts the decimal number N into signed magnitude binary
representation in an ArrayList Arr. */
d. Define the class name TwoComplement which encapsulates Ones complement binary
representation of decimal number. This class also implements interface BinaryInterface.The
class TwoComplement is described as follows:
Attribute:
+ Arr : ArrayList<Integer> // It stores the twos complement binary representation of decimal number.
Operation:
+ toBinary (N: int ): void
/* This method converts the decimal number N into twos compliment binary
representation in an ArrayList Arr. */
e. Write the suitable Driver class named BinaryTest to show the Runtime Polymorphism
Approach.
Sample Output:
One'sComplement of n = -16 is [1, 1, 1, 0, 1, 1, 1, 1]
Two'sComplement of n = -16 is [1, 1, 1, 1, 0, 0, 0, 0]
Sign Magnitude of n = -16 is [1, 0, 0, 1, 0, 0, 0, 0]
CS F213 (Lab-6)
Page 9 of 9