INSTACK 360

Java Object-Oriented Programming: Classes & Objects

Enter the world of OOP. Understand how to blueprint real-world concepts into Java Classes and instantiate Objects.

Advanced 5 min read

Object-Oriented Programming (OOP)

Java is an Object-Oriented Language. As a language that has the Object-Oriented feature, Java supports the following fundamental concepts:

  • Polymorphism
  • Inheritance
  • Encapsulation
  • Abstraction
  • Classes & Objects
  • Instances & Methods

Classes and Objects

A Class is a blueprint from which individual objects are created. An Object is an instance of a class.

public class Dog {
    String breed;
    int age;
    String color;

    void barking() {
        System.out.println("Woof!");
    }
}

Instantiating an Object

To create an object of Dog, we use the new keyword.

public class Main {
    public static void main(String[] args) {
        // Creating an object
        Dog myDog = new Dog();
        
        // Accessing attributes and methods
        myDog.breed = "Beagle";
        myDog.barking();
    }
}

Constructors

A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method, but without a return type.