Professional Documents
Culture Documents
Sapt 3 SECTIUNEA 3 Lectia 1 Java Class Design
Sapt 3 SECTIUNEA 3 Lectia 1 Java Class Design
Overview
Classes
Classes can:
• Declare constants and variables.
• Inherit methods from another class.
• Define and implement methods.
• Override inherited methods when the method is public or
protected.
Classes can:
• Be instantiated by:
– A public or protected constructor.
– A public or protected static method or nested class.
Classes cannot:
• Override inherited methods when the method is final.
The super() method call lets you call the parent class in a
class hierarchy. In this case, the static nested class
extends the behavior of the containing class.
Interface
Possibilities of Interfaces
An interface:
• Can declare public constants.
• Defines methods without implementation.
• Can only refer to its constants and defined methods.
• Can be used with the instanceof operator.
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.
Declaring an Interface
Chassis Example 1
Classes that extend Chassis
will have to provide working
public interface Chassis methods for methods defined
{
here.
public final String chassis = "Generic";
... public Chassis getChassisType();
public void setChassisType(String vehicleFrameType);}
this.chassisName = Chassis.chassis;
Chassis Example 2
...
public ManufacturedEngine(
String engineManufacturer
, Date engineManufacturedDate
, String engineMake
, String engineModel
Step 5: Assign call
, DriveTrain driveTrain parameters to local instance
, int engineCylinders variables.
, Fuel engineType ) {
this.engineManufacturer = engineManufacturer;
this.engineManufacturedDate = engineManufacturedDate;
this.engineMake = engineMake;
this.engineModel = engineModel;
... }
... Step 10: Final prevents subclassing of the method. Step 11: Implement
methods from the
Engine interface.
public final void setEngineManufacturedDate(
Date manufacturedDate) {
vehicleEngine.setEngineManufacturedDate(
manufacturedDate);
...
Step 12: Call the implementation of the interface method from
the ManufacturedEngine because you can't call it from the
Engine interface and the methods can't be subclassed.
Store Example
Abstract Classes
An abstract class:
• Must be defined by using the abstract keyword.
• Cannot be instantiated into objects.
• Can have local declared variables.
• Can have method definitions and implementations.
• Must be subclassed rather than implemented.
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 {
...
For example:
• (String instaneOf Object) would return true.
• (String instanceOf Integer) would return false.
...
public String toString() { Assigning the object
String list = ""; to the String calls that
for (int i = 0; i < this.feature.length; i++) { object's toString
if (list.length() == 0) { method.
list += this.feature[i]; }
else {
list += "\n : " +this.feature[i];}
}
return super.toString() + "\n" +
"Features : " + list + "\n" +
"Car Axle : " + carAxle; }
...
Upcasting Example
Downcasting Example
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);
...
((Car) vehicle).getExternalFeatures();
Succeeds because the
... method belongs to the Car
subclass.
46 Copyright © 2013, Oracle and/or its affiliates. All rights
reserved.
Java Class Design
Terminology
Summary