Home » Java programming language

Is it possible to have same data members in parent and child classes in Java?

Here, we are going to learn about the property of data members: Can we have same data members in parent and child classes? Is it to possible to have same data members in parent and child classes? Is it to possible to have same data member in Base and Derived classes?
Submitted by Preeti Jain, on July 20, 2019

The question is that "Can we have same data members in parent/base and child/derived classes in Java"?

  • Yes, it is possible to have same data member in Parent and Child classes.
  • Now, we will see the capability or strength of the Parent and Child class. The Parent class reference can hold its own object and Child class object as well and The Child class reference hold its own object only.
  • The Parent class object can access their data only that's means this class doesn't have permission to access Child class data.
  • With the help of example we will see what will happen if we contain same data member in both the class [Parent and Child].

Example:

class Base {
    //  Declare and Instantiate data member  
    String str = "We are in Parent class";
}

class Derived extends Base {
    //  Declare and Instantiate data member  
    String str = "We are in Child class";
}

class Main {
    public static void main(String[] args) {
        // Creating a Base class object
        Base base = new Base();
        // Invoked Base class data member with Base class object.
        System.out.println("Base class reference can access only base class member :" + " " + base.str);

        // Creating a Derived class object
        Derived derived = new Derived();

        // Invoked Derived class data member with Derived class object.
        System.out.println("Derived class reference can access both Base and Derived class member : " + " " + derived.str);

        // Here Base class reference holding an object of
        // Derived class and it is perfectly valid
        base = new Derived();

        // Base class reference can access only Base class 
        // data member whether it is holding an object is of 
        // Base class or Derived class.
        System.out.println("Base class reference holding an object of Derived class but it can access only Base class data member :" + " " + base.str);

        // we don't need to include the below code ….
        /*
        // Here Derived class reference holding an object of
        // Base class and it is perfectly invalid
        derived = new Base();

        // If Derived class reference hold an object of Base 
        // class then we will get compile time error
        System.out.println("We will get compile time error");
        */
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
Base class reference can access only base class member : We are in Parent class
Derived class reference can access both Base and Derived class member:  We are in Child class
Base class reference holding an object of Derived class but it can access only Base class data member: We are in Parent class

In the above program we have four cases:

  1. If Base class reference holds an object of Base class then it is perfectly valid and we can access only Base class data member.
    Base base = new Base();
  2. If Derived class reference holds an object of Derived class then it is perfectly valid and we can access both Base and Derived class data member.
    Derived derived = new Derived();
  3. If Base class reference holds an object of Derived class then it is also perfectly valid but it can access only Base class data member.
    Base base = new Derived();
  4. If Derived class reference holds an object of Base class then it is perfectly invalid and in that case we will get compile time error.
    Derived derived = new Derived();


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.