Author: alegru

abstract class Vehicle {} public abstract methodA(); abstract class Vehicle { // … } class Test { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); // DOES NOT COMPILE } } abstract class Vehicle { abstract void move(); } class Car extends Vehicle { @Override public void move() { System.out.println(“The car is…

Read More Abstract Classes and Methods in Java

class Vehicle { public void move() { System.out.println(“The vehicle is moving…”); } } class Car extends Vehicle { public void move() { System.out.println(“The car is moving…”); } } class Test { public static void main(String[] args) { //variable of type Vehicle points to the object of its subclass Car Vehicle car = new Car(); car.move();…

Read More Static and Dynamic Binding in Java

class Vehicle { public void start() { System.out.println(“The vehicle is started…”); } } class Car extends Vehicle { public void start() { System.out.println(“The car is started…”); } } public class Test { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Vehicle car = new Car(); vehicle.start(); car.start(); } } Output: The vehicle…

Read More Polymorphism in Java

public static void main(String[] args) { { System.out.println(“Summer”); } } { System.out.println(“Winter”); } 1 public class Car { 2 private String model = “Ford”; 3 4 { 5 System.out.println(“Instance Initializer block…”); 6 } 7 8 public Car () { 9 model = “Opel”; 10 System.out.println(“Constructor…”); 11 } 12 13 public static void main(String[] args) {…

Read More Java Instance Initializer Block

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