CSE215.4 Lab Manual 6 & 7 Summer 2024
CSE215.4 Lab Manual 6 & 7 Summer 2024
Theory
class is a blueprint for objects in Java, defining their properties (attributes) and behaviours
A
(methods). A class encapsulates data for the object and provides methods to interact with that
data. The key principles of a class include:
● A ttributes (Fields): Variables that represent the state of an object. For example, in a
class Car, attributes might include color, model, and speed.
● Methods: Functions that define the behaviors of an object. Methods allow interaction
with an object's attributes. For instance, a Car class might have methods like
accelerate() or brake().
● Constructors: Special methods used to initialize objects. Constructors can be overloaded
to provide multiple ways of initializing an object.
Example:
Car: The name of the
class
.
ttributes
A :
color
(private
,
String
):
Represents
the
color
ofthe
car.
model
(private
,
String
):
Represents
the
model
ofthe
car.
speed
(private
,
int
):
Represents
the
speed
ofthe
car.
ethods
M :
accelerate
():
Increases
thecar'
s
speed
.
brake
():
Decreases the
car
'
s
speed.
displayInfo
():
Prints
the
car'
s
details
.
Coding example:
/ Car.java
/
public
class
Car
{
private
String color;
private
String model;
private
int
speed;
/ Constructor
/
public
Car(String color, String model,
int
speed)
{
this
.color = color;
this
.model = model;
this
.speed = speed;
}
public
static
voidmain
(String[] args) {
Car myCar =
newCar(
"Red"
,
"Toyota"
,
0);
myCar.displayInfo();
myCar.accelerate();
myCar.brake();
}
}
Output:
Theory
olymorphism is a key concept in OOP that allows objects to be treated as instances of their
P
parent class, providing the ability to call methods defined in a superclass while referring to a
subclass object. Polymorphism enables flexibility and integration of different behaviors through
method overriding and method overloading.
● M ethod Overloading: Occurs when multiple methods in the same class have the same
name but different parameters. It allows a class to perform different tasks based on
different method signatures.
● Method Overriding: Allows a subclass to provide a specific implementation of a method
already defined in its superclass. This ensures that the correct method is called for an
object, depending on its actual subclass type, not the type of reference used to call the
method.
● Constructors and Polymorphism: Constructors can also be overloaded to allow different
ways of initializing an object.
UML Class Diagram Description
Example of a polymorphic class hierarchy with constructors:
Animal
Class
:
ttributes:
A
name (
public
, String) - Name
of
the animal.
ethods:
M
Animal(name):
Constructorto
initialize
the
name
.
makeSound()
: A
method
that
will
be
overridden
in
thesubclasses
.
Dog
Class
:
Inheritance: Inherits
from
Animal.
ethods:
M
Dog(name): Calls the parent
constructor
to
set
the
animal
'
s
name
.
makeSound()
: Overrides the parent
method
to
provide
a
specific
sound
("Bark")
.
Coding Example
/ Animal.java
/
class
Animal
{
protected
String name;
/ Constructor
/
public
Animal(String name) {
this
.name = name;
}
/ Method to be overridden
/
public
void
makeSound
() {
System.out.println(
"Some generic animal sound"
);
}
}
// Dog.java
class
Dog
extends
Animal
{
/ Constructor
/
public
Dog(String name) {
super
(name);
}
/ Overridden method
/
@Override
public
void
makeSound
() {
System.out.println(name +
" says: Bark"
);
}
public
static
void
main(String[] args) {
Animal myAnimal =
newAnimal(
"Generic Animal"
);
myAnimal.makeSound();
Output:
Theory
Inheritance is a mechanism where a new class (subclass) is derived from an existing class
(superclass). The subclass inherits all the non-private attributes and methods of the superclass,
allowing code reuse and the creation of a hierarchical relationship between classes.
● S uperclasses and Subclasses: The superclass is the general class (e.g., Animal), while
subclasses are more specific (e.g., Dog, Cat). Subclasses extend superclasses to inherit
their behaviors.
● Method Overriding: Subclasses can override methods of the superclass to provide
specialized behavior while maintaining the same method signature.
UML Class Diagram Description
Example of inheritance with method overriding:
Vehicle
Class
:
ttributes:
A
speed (
public
, int) - Represents the speed
of
the vehicle.
ethods:
M
Vehicle(speed):
Constructor
to
initialize
speed
.
move()
: A generic movement
method
.
Car
Class:
Inheritance: Inherits
from
Vehicle.
ethods:
M
Car(speed): Calls the parent
constructor.
move()
: Overrides the move()
method
to
provide
specific
movement
behavior
forthe
Car
.
Coding Example
/ Vehicle.java
/
class
Vehicle
{
protected
int
speed;
/ Constructor
/
public
Vehicle(
int
speed) {
this
.speed = speed;
}
/ Method to be overridden
/
public
void
move
() {
System.out.println(
"The vehicle is moving
at speed: "
+ speed);
}
}
/ Car.java
/
class
Car
extends
Vehicle
{
/ Constructor
/
public
Car(
i
nt
speed) {
super
(speed);
}
/ Overridden method
/
@Override
public
void
move
() {
System.out.println(
"The car is driving at
speed: "
+ speed);
}
public
static
void
main
(String[] args) {
Vehicle myVehicle =
new
Vehicle(
50
);
myVehicle.move();
Output:
Theory
bstract classes and interfaces are used in Java to define common templates for other classes.
A
They help achieve abstraction, one of the four fundamental OOP concepts, allowing the creation
of complex applications with simple, defined structures.
● A bstract Class: A class that cannot be instantiated and often includes abstract methods
(methods without implementation). Subclasses must provide implementations for
abstract methods.
● Abstract Methods: Methods declared without a body in an abstract class. Subclasses
must implement them.
● Interfaces: A completely abstract class that only includes abstract methods (before Java
8) and static final variables. Classes implementing an interface must provide
implementations for all the methods defined in the interface.
Appliance
Class
:
ttributes:
A
power (boolean) - Indicates
if
the appliance
is
on
or
off.
ethods:
M
turnOn(): Turns the appliance
on
.
turnOff(): Turns the appliance off.
adjust():
Abstract
method
to
beimplemented
by
subclasses
.
Fan
Class
:
Inheritance: Inherits
from
Appliance.
ethods:
M
adjust(): Provides specific adjustment
functionality.
Playable
Interface
:
ethods:
M
play(): A
method
that
mustbe
implemented
by
any
class
that
implements
theinterface
.
VideoGame
Class
:
Implements
:
Implements
the Playable
interface
.
ethods:
M
play(): Provides
implementation
specific
to
a
video game.
Coding Example
/ Appliance.java
/
abstract
class
Appliance
{
protected
boolean
power;
/ Abstract method
/
public
abstract
void
adjust
();
}
/ Fan.java
/
class
Fan
extends
Appliance
{
public
static
void
main(String[] args) {
Fan myFan =
newFan();
myFan.turnOn();
myFan.adjust();
myFan.turnOff();
}
}
/ Playable.java
/
interface
Playable
{
void
play();
}
// VideoGame.java
class
VideoGame
implements
Playable
{
public
static
void
main
(String[] args) {
VideoGame game =
new
VideoGame();
game.play();
}
}
Output: