Search code examples
javaclasscomposition

Is there any way to call constructor of child class using object of parent class in JAVA?


I have a problem in composition in Java. I want to access the constructor of a class located inside another class. Now if I made an object of parent class and want to access constructor of child class. Is there any possible way to do it?

Here's the code of the both classes and runner class.

package compositon.student;
public class phone {
    String countryCode;
    String number;
    public phone(){}//empty constructor
    public phone(String countryCode, String number)
    {
        this.countryCode = countryCode;
        this.number = number;
    }
}

package compositon.student;

public class address {
    String Street_address;
    String town;
    String city;
    String country;
    
    phone number  = new phone(); //object from class phone, by using composition of classes
    
    public static void main(String[] args) {
        address objAddress = new address();
        objAddress.number()
    }
}


Solution

  • When you say "parent" and "child" class, you're implying inheritance, which is not the case here. You're just using composition: an object of the class "Phone" is a field of the class "Address".

    You can access the Phone object on the class like any other property.

    In your example, objAddress.number() calls the method number on your address instance, which doesn't exist.

    You could create the Phone object first and pass that to the Address constructor or just set the Phone object on the Address object later.

    public class Phone {
        String countryCode;
        String number;
        
        public Phone() {
            
        }
    
        public Phone(String countryCode, String number)
        {
            this.countryCode = countryCode;
            this.number = number;
        }
    }
    
    public class Address {
        String streetAddress;
        String town;
        String city;
        String country;
        Phone phone;
        
        public Address() {
            
        }
    
        public Address(String streetAddress, String town, String city, String country, Phone number) {
            this.streetAddress = streetAddress;
            this.town = town;
            this.city = city;
            this.country = country;
            this.phone = number;
        }
    
        public static void main(String[] args) {
            Phone phone = new Phone("+62", "4412557");
            Address address = new Address("Street", "Town", "City", "Country", phone);
    
            // Update the phone number for an address
            address.phone.number = "12345";
    
            // Replace the phone object entirely for an address
            address.phone = new Phone("+11", "12345");
            
            // Using default contructors
            Address secondAddress = new Address();
            secondAddress.phone = new Phone();
            secondAddress.phone.number = "12345";
            secondAddress.phone.countryCode = "+14";
        }
    }