Professional Documents
Culture Documents
Overview
This lesson covers the following topics:
Model business problems using Java classes
Make classes immutable
Compare access levels default and public
Use the instanceof operator to compare object types
Use virtual method invocation
Use upward and downward casts
Classes
A Java class is a template that defines the features of one
or more objects.
A class can be thought of as a category used to define
groups of things.
Classes:
Define and implement methods.
Implement methods from implemented interfaces.
Interface
An interface is a Java construct that helps define the roles
that an object can assume. It is implemented by a class or
extended by another interface.
Interfaces define collections of related methods without
implementations.
All methods in a Java interface are abstract.
Possibilities of Interfaces
Interfaces give developers two possibilities:
They can develop a concrete class that just implements
the interface and then use that class by creating a
variable of that type.
This means the class that is a copy of the class and has its
behaviors through aggregation.
Aggregation is simply the process of gaining access to the
behaviors of an object type when you declare a variable of
that type in a class.
10
11
Interface Method
An interface method:
In an interface, each method is public even when you
forget to declare it as public.
Is implicitly abstract but you can also use the abstract
keyword.
12
Declaring an Interface
To declare a class as interface you must replace the
keyword class with the keyword interface.
This will declare your interface and force all methods to be
abstract and make the default access modifier public.
Replace class with interface.
public interface Chassis
{
public final String chassis = "Generic";
... public Chassis getChassisType();
public void setChassisType(String vehicleFrameType);}
13
Chassis Example 1
Classes that extend Chassis
will have to provide working
methods for methods defined
here.
Classes implementing an
interface can access the
interface constants.
14
15
Chassis Example 2
Implementing classes must
public interface Chassis
{
provide the working method.
public final String chassis = "Generic";
... public Chassis getChassisType();
public void setChassisType(String vehicleFrameType);}
engineManufacturer;
engineManufacturedDate;
public ManufacturedEngine() {
this.engineManufacturer = "Generic";
this.engineManufacturedDate = new Date();
this.engineMake = "Generic";
this.engineModel = "Generic";
... }
17
19
20
21
Store Example
A store owner wants to create a website that displays all
items in the store. We know:
Each item has a name.
Each item has a price.
Each item is organized by department.
It would be in the store owner's best interest to create an
interface for what defines an item. This will serve as the
blueprints for all items in the store, requiring all items to at
least have the defined qualities above.
22
23
Abstract Classes
An abstract class provides a base class from which other
classes extend.
26
27
28
Immutable Classes
Immutable classes:
Make the class final, which means that other classes
can't extend its behavior.
Only allow instance variable assignments in the default
or overloaded constructor.
...
public final class Car extends Vehicle {
...
31
32
33
34
36
37
...
public String toString() {
String list = "";
for (int i = 0; i < this.feature.length; i++) {
if (list.length() == 0) {
list += this.feature[i]; }
else {
list += "\n
: " +this.feature[i];}
}
return super.toString() + "\n" +
"Features
: " + list + "\n" +
"Car Axle
: " + carAxle; }
...
38
39
40
41
42
Upcasting Example
Even though vehicle is a Car, it was upcast to Vehicle so it
lost access to its Car-specific methods and fields.
Attempts to access subclass methods fail at compile time.
Vehicle is a superclass.
Car is a subclass of Vehicle.
...
Vehicle vehicle =
(Vehicle) new Car("Honda",new Date(1325599999999L)
,"Honda","Prelude","Coupe"
, new VehicleChassis("Unibody")
, new ManufacturedEngine("Honda"
, new Date(), "H-Series","H23A1"
, DriveTrain.TWO_WHEEL_DRIVE,4
, Fuel.MIDGRADE_GASOLINE),f,2);
...
Fails because the method belongs to
vehicle.getExternalFeatures();
the Car subclass and access to it is
...
...
Vehicle vehicle =
(Vehicle) new Car("Honda",new Date(1325599999999L)
,"Honda","Prelude","Coupe"
Vehicle is a superclass.
, new VehicleChassis("Unibody")
, new ManufacturedEngine("Honda"
, new Date(), "H-Series","H23A1"
, DriveTrain.TWO_WHEEL_DRIVE,4
, Fuel.MIDGRADE_GASOLINE),f,2);
...
Fails because the method belongs to
vehicle.getExternalFeatures();
the Car subclass and access to it is
...
Downcasting Example
Downcasting the superclass works here because the
runtime object is actually a Car instance. The Vehicle
instance is first cast to a subclass of itself before running
the Car instance method.
Vehicle is a superclass.
...
Car is a subclass of Vehicle.
Vehicle vehicle =
(Vehicle) new Car("Honda",new Date(1325599999999L)
,"Honda","Prelude","Coupe"
, new VehicleChassis("Unibody")
, new ManufacturedEngine("Honda"
, new Date(), "H-Series","H23A1"
, DriveTrain.TWO_WHEEL_DRIVE,4
, Fuel.MIDGRADE_GASOLINE),f,2);
...
Succeeds because the
((Car) vehicle).getExternalFeatures();
method belongs to the Car
...
subclass.
45
subclass.
46
Terminology
Key terms used in this lesson included:
Abstract class
Abstract constructor
Casting
Downward cast
Immutable classes
instanceof
Interface
Upward cast
Virtual method invocation
47
Summary
In this lesson, you should have learned how to:
Model business problems using Java classes
Make classes immutable
Compare access levels default and public
Use the instanceof operator to compare object types
Use virtual method invocation
Use upward and downward casts
48