Home > Java > javaTutorial > How Does `super()` Enable Proper Constructor Invocation and Parent Method Access in Java Inheritance?

How Does `super()` Enable Proper Constructor Invocation and Parent Method Access in Java Inheritance?

Susan Sarandon
Release: 2024-12-11 03:48:09
Original
165 people have browsed it

How Does `super()` Enable Proper Constructor Invocation and Parent Method Access in Java Inheritance?

Exploring 'super()': Its Role in Java Inheritance

Java's 'super()' keyword plays a crucial role in object-oriented programming, particularly when dealing with inheritance. Let's dive into understanding its usage and capabilities.

Calling the Parent Constructor

'super()' is primarily used to invoke the parent class's constructor when creating a new subclass object. It ensures that the parent's initialization code runs before the subclass's own constructor executes.

When 'super()' is called without any arguments, it defaults to calling the parent constructor with no arguments. This is necessary to ensure proper object initialization and setup inherited state.

Example:

class Parent {
    private String name;

    public Parent() {
        name = "Default parent name";
    }
}

class Child extends Parent {
    public Child() {
        super(); // Calls the Parent constructor with no arguments
    }
}
Copy after login

Passing Arguments to the Parent Constructor

'super()' can also be used to pass arguments to the parent constructor. This is useful when the parent constructor has multiple overloaded methods. By specifying specific arguments, you can call the desired parent constructor.

Example:

class Parent {
    private String name;

    public Parent(String name) {
        this.name = name;
    }
}

class Child extends Parent {
    public Child(String name) {
        super(name); // Calls the Parent constructor with a name argument
    }
}
Copy after login

Calling Parent Methods

In addition to invoking the parent constructor, 'super' can also be used to access parent class methods and variables. This allows subclasses to extend or override inherited behavior.

To call a parent method, use the syntax 'super.methodName()'.

Example:

class Parent {
    public void printName() {
        System.out.println("Parent's name: " + name);
    }
}

class Child extends Parent {
    public void printChildName() {
        super.printName(); // Calls the printName() method from the Parent class
    }
}
Copy after login

For detailed tutorials and further explanation, please refer to the provided resource link.

The above is the detailed content of How Does `super()` Enable Proper Constructor Invocation and Parent Method Access in Java Inheritance?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template