java oop

class Vehicle { void startEngine() { System.out.println(“Starting the engine…”); } void stopEngine() { System.out.println(“Stopping the engine…”); } } class Car extends Vehicle { void move() { System.out.println(“The car is moving…”); } void invoke() { super.startEngine(); move(); super.stopEngine(); } } class TestSuper2 { public static void main(String args[]){ Car car = new Car(); car.invoke(); } }…

Read More Keyword super in Java

public class SubClass extends SuperClass { // fields // methods } class Vehicle { private String brand; // a superclass constructor that can’t be inherited public Vehicle(String brand) { this.brand = brand; } public void printBrand() { System.out.println(“Brand is: ” + brand); } // overriding the toString() method of a superclass Object public String toString()…

Read More Inheritance in Java

class Car { public static String MODEL = “Ford”; } public class ConstructorExample { public static void main(String[] args) { System.out.println(Car.MODEL); } } Output: Ford class Car { public static String MODEL = “Ford”; public static void printModel() { System.out.println(“Model is: ” + MODEL); } } Output: Model is: Ford class ClassA { static Map…

Read More Keyword static in Java

nameOfTheClass() {} class Car { public Car() { // constructor System.out.println(“The car is mooving!”); } public static void main(String args[]) { Car car = new Car(); // with this, we are actually calling a constructor } } Output: The car is moving!   Note: The compiler automatically creates a default constructor if there is no constructor…

Read More Constructor in Java