Java OOP

class Car { public void start() { // … } public void start(int a) { // … } public void start(String str) { // … } } class Colour { public void printColour() System.out.println(“This is general colour”); } } class Blue extends Colour { public void printColour() System.out.println(“This is blue colour”); } }

Read More Method Overloading vs Method Overriding in Java

class Vehicle { // … } class Car extends Vehicle { // … } class Test { public static void main(String[] args) { Vehicle vehicle = new Vehicle(); Car car = new Car(); System.out.println(vehicle instanceof Object); System.out.println(car instanceof Vehicle); System.out.println(car instanceof Car); } } Output: true true true   As you can see, the Vehicle…

Read More Object Class in Java

public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name } } class Test { public static void main(String[] args) { Student s = new Student(); // System.out.println(s.name); // DOES NOT COMPILE s.setName(“John”); System.out.println(s.getName()); } } Output: John   If we try to…

Read More Encapsulation in Java

package vehicle; public class Car { // fields and methods } package vehicle; // OK public class Car { package vehicle; // DOES NOT COMPILE // fields and methods } package vehicle; // DOES NOT COMPILE package vehicle; class Car { public void printRandomNumber() { Random r = new Random(); // DOES NOT COMPILE System.out.println(r.nextInt(10));…

Read More Java Packages

Before Java 8, there were many more differences between interfaces and abstract classes, but with the introduction of default, non-abstract, and static methods in interfaces, the difference has narrowed significantly. See what still stands as the difference between these two types: Abstract Class Interface Methods Can have final methods but can not have default methods.…

Read More Differences Between Interface and Abstract Class in Java

interface Runnable {} interface Vehicle { void move(); } class Car implements Vehicle { @Override public void move() { System.out.println(“The car is moving…”); } } class Test { public static void main(String[] args) { Vehicle car = new Car(); car.move(); } } Output: The car is moving… interface Vehicle { void move(); } class Test…

Read More Interface in Java

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