Constructors and Subclasses

Does the constructor of a class, needs to have no arguments..
if it has arguments, is it still a constructor

i.e public class new {
    public new(int x, int y) {..};
}

Say we have class A extend class B
how do we call methods and variables from B in Class A

public class A {

    super.method("a", "b"); // is this how u call methods?
    field1 = super.field1; // is this how u call a field?
}

what is..

 super(name);
jedistarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi jedistar,

1. public class new
THIS WILL NOT COMPILE.  new is a keyword!

2. public class A {  super.method("a", "b"); }
A does not extend anything, so super will probably make an error =)

Try this out:
public class B
{
  protected int anInt;
  // There is a default constructor already in place for B, so I don't need to make a constructor
  // It should set anInt to 0.
}
class A extends B
{
  // Same thing, default constructor, and because this extends B it gets anInt
 
  // Method to get anInt
  public int getTheInt()
  {
    return anInt;
  }
}


// This is a test class
public class testAB
{
  public static void main(String[] args)
  {
    A obj = new A();
    System.out.println(obj.getTheInt());
  }
}

Any questions? My code is really terse.
Joe P
0
jedistarAuthor Commented:
1. yeah my mistake, say public class Test2 {
    public Test2(int x, int y) {..};
}
Does the constructor of a class, needs to have no arguments..
if it has arguments, is it still a constructor

2.
How come you didn't have to put "super"

public int getTheInt()
  {
    return super.anInt;
  }

So if my class A extends B..

public class A extends B {

    super.method("a", "b"); // is this how u call methods?
    field1 = super.field1; // is this how u call a field?
}

what is..  super(name); too? i seen this somewhere.
0
BogoJokerCommented:
Here is the idea extended a little bit:

// Here are your two classes
public class B
{
    // This is protected and is automatically inheirited by A
    protected int anInt;
    // This is private, and can only be accessed by getHidden()
    private int hidden;

    // This is what the default constructor does, you can actually delete this code and it
    // Will work exactly the same!
    public B()
    {
        anInt = 0;
        hidden = 0;
    }

    public int getHidden()
    {
        return hidden;
    }

}
class A extends B
{
    // Default constructor, this happens automatically so if you delete this code too, it will
    // still work exactly the same
    public A()
    {
        super();
    }

    // Just a method, since we already have anInt (because it was protected) we can
    // use it like it is local, because it is local!
    public int getTheInt()
    {
        return anInt;
    }

    // Here is your super call.  It goes to the parent class, B and it
    // calls the getHidden() method in the B class.
    public int getHidden()
    {
        return super.getHidden();
    }
}



// Test class
public class testAB
{
    public static void main(String[] args)
    {
        // Create an A object called aObject
        A aObject = new A();
        // Test the methods
        System.out.println("anInt is "+aObject.getTheInt());
        System.out.println("hidden is " + aObject.getHidden());
    }
}

This is a little more descriptive.
The super goes to the parent class.  In this case B is the parent of A, so anything in A that says super will look to class B.
super will go as high up the tree as required.  lets say:
C --> B --> A
Any super call from A will first go to B, if not found it will go up to C, if not found there then you have a problem =)

Also notice the special super(), that calls the constructor of the parent constructor.
For most cases you will user super.methodName(params);

Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.