• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

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);
0
jedistar
Asked:
jedistar
  • 2
1 Solution
 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now