Академический Документы
Профессиональный Документы
Культура Документы
nterface Expandable{
public i // use keyword interface instead of class
int EXPANSION_COST = 300; // public final static attribute
boolean expand(); // public abstract method/s
}
nterface Rectangular{
public i
int getArea();
int getPerimeter();
}
Interfaces cannot be instantiated, they can only be implemented. All the methods in an interface are abstract methods
(i.e., we leave the implementation of the methods in the interface to the implementing classes), so there is no need to
state it. Interfaces can only contain final and static attributes, or simply constants, that can be accessed by the
implementing classes.
Question: What is the access modifier of all the abstract methods in an interface?
Implementing Interfaces
When implementing an interface, we use the keyword implements followed by the interface name. Once a class
implements an interface, it must provide an implementation to all the method signatures found in the interface.
A class can implement multiple interfaces. In doing so, the implementing class must define all the method signatures
found in all the interfaces that it is implementing.
A city has its name and population. Population grows by adding creatures, only one at a time, in the city.
A virtual city is a city that should be rectangular in shape and should be expandable. It has a name, length, width,
population count, and money. Initially, the virtual city has a total amount of 500 and its length and width are 3 and 2
respectively. Each creature occupies one (1) square unit of a virtual city. Number of creatures may be added to the
city provided that its area is still enough to accommodate more creatures. A creature adds 60 to the total money of the
virtual city.
A rectangular city should be able to compute for its area and its perimeter. An expandable city can expand. Cost of
expansion amounts to 300. This will add 1 unit each for the length and width.
City.java
package city;
Expandable.java
package city;
Effects of Interface
Implementing multiple interfaces can give the illusion that a class is able to employ multiple inheritance. It gives such
an illusion because in terms of typing, instances of the implementing class can be casted to the types of all the
interfaces it implements. Just like in inheritance where instances of the subclass can be casted to the type of the
superclass. However, nothing is really inherited by the implementing class from the interface; the interface merely
imposes to the implementing class what behaviors or methods it should support.
Example:
public class VirtualCity extends City implements Rectangular, Expandable { … }
Casting
Casting can be performed in order to limit the valid operations an object can perform to operations warranted by the
type it is casted to. Java supports widening conversions; that is, instances of a subclass can be casted to the type of
the superclass. Narrowing conversions are possible if and only if the object came from a preceding widening
conversion. In the case of interfaces, the instances of the implementing class can be casted to the type of its
interfaces. See the following example:
Instantiation
City city = new VirtualCity(“Los Banos”); Type is the superclass of VirtualCity (casting)
Main.java
import city.City;
import city.Rectangular;
import city.Expandable;
import city.VirtualCity;
System.out.println(a.getName());
System.out.println(b.getName());
System.out.println(c.getArea());
System.out.println(d.expand());
a.addCreature();
}
}
Since c is of type Rectangular, the operations it can invoke are only getArea() and getPerimeter(). The method
expand() cannot be invoked by c because the operations allowed are limited by its type.