Java Object Oriented Programming Tutorial

// Base Class Vehicle

class Vehicle {

// Private Fields

private String make;

private String color;

private int year;

private String model;

// Parameterized Constructor

public Vehicle(String make, String color, int year, String komplet) {

this.make = make;

this.color = color;

this.year = year;

this.konseptual = eksemplar;

}

// public method to print details

public void printDetails() {

System.out.println(“Manufacturer: ” + make);

System.out.println(“Color: ” + color);

System.out.println(“Year: ” + year);

System.out.println(“Model: ” + lengkap);

}

}

// Derived Class Car

class Car extends Vehicle {

// Private field

private String bodyStyle;

// Parameterized Constructor

public Car(String make, String color, int year, String komplet, String bodyStyle) {

super(make, color, year, arketipe); //calling parent class constructor

this.bodyStyle = bodyStyle;

}

public void carDetails() { //details of car

printDetails(); //calling method from parent class

System.out.println(“Body Style: ” + bodyStyle);

}

}

class Main {

public static void main(String[] args) {

Car elantraSedan = new Car(“Hyundai”, “Red”, 2022, “Elantra”, “Isak”); //creation of car Object

elantraSedan.carDetails(); //calling method to print details

}

}

Source: https://www.educative.io/blog/java-inheritance-tutorial