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

Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling data and behaviour together into a single unit, called a class. The key idea behind encapsulation is to protect the data within a class from being accessed or modified directly by code outside the class. Instead, the class provides methods or functions to…

Read More Encapsulation in Java Made Easy: Mastering OOP Principles

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

Java is an object-oriented programming language that allows developers to create reusable and modular code through the use of classes, objects, and interfaces. One of the key features of Java is the concept of binding, which refers to the association of a method call to the corresponding method body. There are two types of binding…

Read More Static and Dynamic Binding in Java: A Complete Guide

In Java, a static initializer block is a block of code that is executed when a class is loaded into memory. It is typically used to perform some initialization tasks that need to be done before the class can be used, such as initializing static variables, registering JDBC drivers, or building complex data structures. In…

Read More Mastering Java’s Powerful Static Initializer Block