Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

JOHANE GREI C.

DINGDING – BSIT 2C SYNTAX


CONCEPT: INHERITANCE

OBJECT: CAR

What possible states can this object perform?

The possible states of the Object Car are:


 Running
 Park
What possible behavior can this object perform?
 Drive
 Forward
 Reverse
 Shift
 Change Gear
 Hazard
 Blink Lights
 Top Speed
 Low Fuel Warning Light
 On Board Computer
 Power Steering
 Cruise Control

class Car {

// field and method of the parent class

String brandcar;
public void Status() {

System.out.println("CAR BRAND : " +brandcar);

System.out.println("\n\n>>State\n\n");

System.out.println("Running");

System.out.println("PARK");

System.out.println("\n\nBEHAVIOR: DRIVE");

System.out.println("BEHAVIOR: FORWARD");

System.out.println("BEHAVIOR: REVERSE");

System.out.println("BEHAVIOR: SHIFT");
System.out.println("BEHAVIOR: CHANGE GEAR");

System.out.println("BEHAVIOR: HAZARD");

System.out.println("BEHAVIOR: BLINK LIGHTS");

// inherit from car

class SportsCar extends Car {

// new method in subclass


public void Behavior() {

System.out.println("SPORTS CAR BEHAVIOR : TOP SPEED");

System.out.println("SPORTS CAR BEHAVIOR : LOW FUEL


WARNING LIGHT");

System.out.println("SPORTS CAR BEHAVIOR : ON BOARD


COMPUTER");

System.out.println("SPORTS CAR BEHAVIOR : POWER


STEERING");

System.out.println("SPORTS CAR BEHAVIOR : CRUISE


CONTROL");

System.out.println("SPORTS CAR BEHAVIOR : SPORTS


CONTROL");

class act1 {
public static void main(String[] args) {

// create an object of the subclass

Car Normalcar = new Car();

Normalcar.brandcar = "Mercedes";

// access field of superclass

System.out.println("\n\n>>NORMAL CAR --> MERCEDES


SUV\n\n");

Normalcar.Status();
// create an object of the subclass

SportsCar luxurycar = new SportsCar();

luxurycar.brandcar = "Mercedes";

// access field of superclass

System.out.println("\n\n>>SPORTS CAR --> Mercedes-AMG GT


Coupe\n\n");

luxurycar.Status();

// call method of superclass

// using object of subclass


luxurycar.Behavior();

Explanation:
The code uses the concept of Inheritance by using the
keyword extends. Inheritance is an important pillar of OOP
(Object-Oriented Programming). It is the mechanism in java by
which one class is allowed to inherit the features (fields and
methods) of another class. ... The subclass can add its own fields
and methods in addition to the superclass fields and methods.
The child class SportsCar inherits the features(fields and
methods) of the parent class which is the Car.

CONCEPT: ENCAPSULATION
OBJECT: LAPTOP

What possible states can this object perform?

The possible states of the Object Car are:


 Power On
 Shut Down/Off
What possible behavior can this object perform?
 Type
 Connect /Surf to Wifi
 Watch Movies
 Print Documents
 Edit Videos and Pictures

class laptop{

private String laptop, brand, behavior, status;

public void setLaptop(String newLaptop) {

laptop = newLaptop;

}
public String getLaptop() {

return laptop;

public void setBrand(String newBrand) {

brand = newBrand;

public String getBrand() {

return brand;

}
public void setStatus(String newStatus) {

status = newStatus;

public String getStatus() {

return status;

public void setBehavior(String newBehavior) {

behavior = newBehavior;

}
public String getBehavior() {

return behavior;

class act1encap {

public static void main(String[] args) {

object obj = new object();

obj.setLaptop("\nLaptop-Macbook");
System.out.println(obj.getLaptop());

obj.setBrand("\nApple");

System.out.println(obj.getBrand());

obj.setStatus("\nSTATUS: on/off");

System.out.println(obj.getStatus());

obj.setBehavior("\nBEHAVIOR: Typing");

System.out.println(obj.getBehavior());

obj.setBehavior("\nBEHAVIOR: Connecting to Wifi");

System.out.println(obj.getBehavior());

obj.setBehavior("\nBEHAVIOR: Watch Movies");

System.out.println(obj.getBehavior());
obj.setBehavior("\nBEHAVIOR: Print Documents");

System.out.println(obj.getBehavior());

obj.setBehavior("\nBEHAVIOR: Edit Videos and Pictures");

System.out.println(obj.getBehavior());

}
Explanation:
The code uses the concept of Encapsulation by using
Encapsulation; the code was able to hide the values or state of a
structured data object inside a class, preventing unauthorized
parties' direct access to them. By declaring the objects; laptop,
brand, behavior, status as private, it prevents the data from being
accessed by the code outside this shield. The program also uses
Getters and Setters, Getters and Setters play an important role in
retrieving and updating the value of a variable outside the
encapsulating class. A setter updates the value of a variable,
while a getter reads the value of a variable.

You might also like