How to Use a Superclass to Initialize a Subclass Object in Java?

Question

How can I use a superclass to initialize a subclass object in Java?

class Superclass {
    void display() {
        System.out.println("This is a superclass method.");
    }
}

class Subclass extends Superclass {
    void display() {
        System.out.println("This is a subclass method.");
    }
}

public class Main {
    public static void main(String[] args) {
        Superclass obj = new Subclass();
        obj.display(); // Calls the subclass method
    }
}

Answer

In Java, initializing a subclass object using a superclass reference is a common practice that allows for polymorphism, enabling method overriding and dynamic method dispatch.

class Animal {
    void sound() {
        System.out.println("Animal makes a sound.");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks.");
    }
}

class Cat extends Animal {
    void sound() {
        System.out.println("Cat meows.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();
        myDog.sound(); // Outputs: Dog barks.
        myCat.sound(); // Outputs: Cat meows.
    }
}

Causes

  • Understanding inheritance in Java
  • Need for polymorphic behavior in applications
  • Desire to treat objects of different classes similarly

Solutions

  • Use a superclass type reference to hold a subclass object.
  • Override methods in the subclass to provide specific implementations.
  • Call the overridden methods using the superclass reference.

Common Mistakes

Mistake: Confusing the superclass and subclass references

Solution: Always ensure that you use the correct reference type to avoid compilation errors.

Mistake: Failing to override methods in the subclass

Solution: Make sure to implement the overridden methods in the subclass to achieve polymorphic behavior.

Helpers

  • Java superclass
  • initialize subclass object Java
  • Java inheritance
  • polymorphism in Java
  • Java method overriding

Related Questions

⦿How to Configure javac to Recognize JAR Files in Eclipse?

Learn how to configure javac to recognize JAR files in Eclipse for seamless compilation and development. Follow these expert tips and coding practices.

⦿How to Connect to a Local PostgreSQL Instance Using JDBC

Learn how to connect to a local PostgreSQL database using JDBC including code examples and common troubleshooting tips.

⦿How to Execute a JAR File with Parameters Using Gradle

Learn how to run a JAR file with commandline parameters in Gradle. Stepbystep guide with examples and common mistakes.

⦿How Can You Programmatically Request Permissions in Applications?

Learn how to programmatically request permissions in applications with effective strategies and code examples.

⦿How to Use ProGuard in Android Development for Code Optimization?

Learn how to implement ProGuard in your Android projects for code optimization and obfuscation. Follow our detailed guide and best practices.

⦿Understanding the IntelliJ Light Bulb Feature: What It Is and How to Use It

Explore IntelliJs light bulb feature its purpose usage and how it enhances coding efficiency.

⦿What is the Difference Between Spring Authentication Provider and Authentication Processing Filter?

Explore the key differences between Spring Authentication Provider and Authentication Processing Filter in Spring Security with detailed explanations.

⦿How to Effectively Mock getApplicationContext in Android Testing?

Learn how to mock getApplicationContext in Android testing with best practices. Improve your unit tests with our expert tips and code examples.

⦿How to Extract Links from a Web Page Using Python

Learn how to extract hyperlinks from a web page using Python with Beautiful Soup and Requests. Stepbystep guide and code snippets included.

⦿How to Load Freemarker Templates from a Folder Inside a .jar File

Learn how to access and load Freemarker templates stored within a .jar file efficiently.

© Copyright 2025 - CodingTechRoom.com