Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, March 10, 2025

Protected members, constructors in sub classes.

 

Protected Members in Java

The protected access modifier in Java allows a member to be accessible within its own package and by subclasses, even if they are in different packages. It provides a level of protection between public and private.


1. Protected Members Example

// File: Animal.java (Superclass)

package animals;

 

public class Animal {

    protected String name;

 

    public Animal(String name) {  // Constructor

        this.name = name;

    }

 

    protected void display() {  // Protected method

        System.out.println("Animal: " + name);

    }

}


2. Accessing Protected Members in Subclass

// File: Dog.java (Subclass in another package)

package pets;

 

import animals.Animal;

 

public class Dog extends Animal {

 

    public Dog(String name) {

        super(name); // Calling the superclass constructor

    }

 

    public void show() {

        display();  // Accessing protected method from superclass

        System.out.println("Dog name: " + name);  // Accessing protected field

    }

}


3. Testing the Protected Members

// File: Main.java

package test;

 

import pets.Dog;

 

public class Main {

    public static void main(String[] args) {

        Dog dog = new Dog("Bruno");

        dog.show();

    }

}


Output

Animal: Bruno

Dog name: Bruno


🔍 Explanation

  1. Protected Field & Method: name and display() are accessible in the Dog class, even though it belongs to a different package.
  2. Inheritance Compatibility: The Dog class can access the protected members of the Animal class due to inheritance.

Constructors in Subclasses

Constructors are not inherited, but a subclass can call the superclass’s constructor using the super() keyword. If you don't explicitly call super(), Java will automatically call the default constructor of the superclass if it exists.


1. Using super() in Subclass Constructor

class Parent {

    String name;

 

    // Parent class constructor

    public Parent(String name) {

        this.name = name;

        System.out.println("Parent class constructor called.");

    }

}

 

class Child extends Parent {

    int age;

 

    // Child class constructor

    public Child(String name, int age) {

        super(name); // Calling Parent class constructor

        this.age = age;

        System.out.println("Child class constructor called.");

    }

 

    public void display() {

        System.out.println("Name: " + name + ", Age: " + age);

    }

}

 

public class Main {

    public static void main(String[] args) {

        Child obj = new Child("Krishna", 33);

        obj.display();

    }

}


Output

Parent class constructor called.

Child class constructor called.

Name: Krishna, Age: 33


🔍 Explanation

  1. super() Keyword: It is used to call the parent class’s constructor. It must be the first statement in the subclass constructor.
  2. Chaining of Constructors: If you do not use super(), the compiler tries to call the default constructor of the superclass.
  3. Overloaded Constructors: If the parent class has overloaded constructors, you must call the appropriate one using super() with parameters.

📌 Important Points

  1. Protected members are accessible to:
    • Classes in the same package.
    • Subclasses (even if they are in different packages).
  2. Constructors are not inherited, but they can be accessed using super().
  3. Java always calls the parent class constructor first, either explicitly (super()) or implicitly if not specified.

 

No comments:

Post a Comment