DEV Community

Cover image for Constructors and Inheritance in Java (Simplified)
Sharique Siddiqui
Sharique Siddiqui

Posted on • Edited on

Constructors and Inheritance in Java (Simplified)

Grasping the concepts of constructors and inheritance is key to writing effective and robust Java code. Both are cornerstones of Java’s object-oriented approach, enabling you to create reusable, organized, and flexible programs. Let’s demystify them with easy-to-understand explanations and examples.

Constructors in Java

A constructor is a special block of code in a class that runs when you create a new object. Its main job is to initialize (set up) the object's variables with starting values.

Key Features

  • Same name as the class (e.g., Car() is the constructor of class Car).

  • No return type, not even void.

  • Runs only once, at the time of object creation.

Example: Default Constructor

If you don’t write any constructor, Java provides a default one for you.

java
public class Car {
    String color;

    // Default constructor
    public Car() {
        color = "red";
    }
}
Enter fullscreen mode Exit fullscreen mode
java
Car myCar = new Car();
System.out.println(myCar.color); // Output: red
Enter fullscreen mode Exit fullscreen mode

Parameterized Constructor

You can create constructors that receive arguments to set properties for each object individually.

java
public class Car {
    String color;
    int year;

    // Parameterized constructor
    public Car(String c, int y) {
        color = c;
        year = y;
    }
}
Enter fullscreen mode Exit fullscreen mode
java
Car myCar = new Car("blue", 2023);
System.out.println(myCar.color); // Output: blue
System.out.println(myCar.year);  // Output: 2023
Enter fullscreen mode Exit fullscreen mode

Inheritance in Java

Inheritance lets you build new classes based on existing ones—for free! This means you can reuse and extend the code, making it easier to maintain and scale your applications.

Why Use Inheritance?

  • Share common code between classes (avoid repetition).

  • Build a hierarchy (e.g., AnimalDog, Cat).

  • Override or extend behaviors.

How Does It Work?

  • The existing class is called the superclass (parent).

  • The class that inherits is the subclass (child).

  • Use the extends keyword.

Example:

java
// Superclass
public class Animal {
    void eat() {
        System.out.println("Animal eats.");
    }
}
Enter fullscreen mode Exit fullscreen mode
// Subclass
public class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks.");
    }
}
Enter fullscreen mode Exit fullscreen mode
java
Dog myDog = new Dog();
myDog.eat();  // Inherited method
myDog.bark(); // Dog's own method
Enter fullscreen mode Exit fullscreen mode

Output:

text
Animal eats.
Dog barks.
Enter fullscreen mode Exit fullscreen mode

Overriding Methods
A subclass can change (override) the methods it inherits:

java
public class Dog extends Animal {
    @Override
    void eat() {
        System.out.println("Dog eats bones.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Quick Tips

  • Constructor chaining: Use super() in a subclass constructor to call the parent constructor.

  • Single inheritance: Java allows one class to extend only one superclass (no multiple inheritance, but you can use interfaces for flexibility).

  • You cannot inherit constructors, but you can call them with super(...) in the subclass.

Final Thoughts

Understanding constructors allows you to create objects with sensible initial values, while inheritance helps you reuse and extend code smartly. These concepts lay the groundwork for more advanced Java programming—practice writing simple classes using constructors and inheritance to solidify your grasp!

Check out the YouTube Playlist for great java developer content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud

Top comments (0)