Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access superclass data members in java to create constructor in child class?

I am trying to create a constructor in a subclass which can take the values of data members of its superclass. but I don’t know what is wrong here.
error is saying that ‘method card is undefined for the type creditcard’

class card{
    int cardno;
    String cust_name;
    String bank_name;
    public card(int num,String c_name,String b_name)
    {
       cardno=num;
       cust_name=c_name;
       bank_name=b_name;
    }
    card(){}
}

class creditcard extends card{
    int limit;
    public creditcard(int num,String c_name,String b_name,int lim)
    {
        card(num,c_name,b_name);
        limit=lim;
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please change your method to the code below:

public creditcard(int num, String c_name, String b_name, int lim) {
super(num, c_name, b_name);
limit = lim;
}

super more about super keyword here:
https://docs.oracle.com/javase/tutorial/java/IandI/super.html

Hope this helps.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading