March 31, 2013

How to access information from a superclass and subclass?

Question by SkyVar

What I am trying to do is access information (variables, methods, etc) from a superclass and it’s subclasses.

If, what I know about inheritance is right, by accessing the superclass we have access to it’s subclasses by default. So I am thinking that I only need to be able to access the parent (super) class.

But how do I do that?

I am not going to post all the code up here, and that will turn this post into 3 pages.

The superclass is just a general code to create a contact and the class that needs to access the superclass is a class that creates an arraylist and records each contact in the arraylist.

I am not trying to get the code written for me, but all the help to understand how this will work, will be greatly appreciated.

To keep this short, I won’t post the subclasses unless needed.

Contacts (Superclass):

public class Contacts
{
    protected String fname;
    protected String lname;
    protected String email;
    protected String phone;

    public Contacts(String fname, String lname, String email, String phone)
    {
        this.fname=fname;
        this.lname=lname;
        this.email=email;
        this.phone=phone;

    }

    public String getfname()
    {
        return fname;
    }

    public void setfname(String first)
    {
        this.fname=first;       
    }

    public String getlname()
    {
        return lname;
    }

    public void setlname(String last)
    {
        this.lname=last;
    }

    public String getemail()
    {
        return email;
    }

    public void setemail(String e)
    {
        this.email=e;
    }

    public String getphone()
    {
        return phone;
    }

    public void setphone(String num)
    {
        this.phone=num;
    }

    public String getFullName()
    {
        String full=fname+" "+lname;
        return full;
    }

I haven’t done much on this code because I have been trying to figure it out without really knowing where to start. I do not think the arguments should be null, I just included those to satisfy the evil compiler.

Addressbook:

import java.util.ArrayList;
public class AddressBook
{
    Contacts enteredContact = new Contacts(null, null, null, null);
}

Here is one of the subclasses to get an idea of what is included.

Friends (subclass):

public class Friend extends Contacts
{
    private String dob;

    /**
     * Constructs a new Friend object. (Insert any further description that is needed)
     * @param fname
     * @param lname
     * @param email
     * @param phone
     */
    public Friend(String fname, String lname, String email, String phone)
    {
        super(fname, lname, email, phone);
    }

    /**
     * @return the dob
     */
    public String getDob()
    {
        return dob;
    }

    /**
     * @param dob the dob to set
     */
    public void setDob(String dob)
    {
        this.dob = dob;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        return fname+", "+lname+", "+email+", "+phone+", "+dob;
    }

}

Answer by Starx

Use super function/keyword to access parent class. You can call a parent method like this

super.methodName();

To access parent constructor, you can do it in the following way using super() method.

super(null, null, null, null);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!