0

Let's say I have a class 'Person' and another class 'Survey' which extends person so Survey is the child class and Person class is the parent. Person was the first class I wrote and hence defined the main method there now since I have a child class, can I call methods of the child class from the main method in the parent class (or do I need to keep transferring the main method to the class that is lower most in the heirarchy although I am pertty sure this is never ever going to be necessary...)? If so is this not counter intuitive to the notion that the child class inherits attributes of the parent class but the parent class does not inherit any attributes of the child class? Please do oblige with a reply. Thanks in advance.

Also I also read another post of having a separate class maybe 'driver.java just for the main method so would this mean that all classes would have to be imported into this class for us to call methods from other class in the main method?

I hope my question is not too convoluted.

anonuser0428
  • 11,789
  • 22
  • 63
  • 86
  • 7
    Unrelated, but how is a `Survey` a `Person`?! – Dave Newton Jun 06 '12 at 15:20
  • 3
    A parent class should, in general, have no knowledge of its subclasses. There can be an arbitrary number of subclasses, with arbitrary functionality. – Dave Newton Jun 06 '12 at 15:22
  • it sounds like you are asking "how do i redesign my code without moving any code around"? and the answer is, "you don't". if you change how your app works, you're going to need to shuffle code around. if you change the class which is your app entry point, then you need to move the main method, it's that simple. – jtahlborn Jun 06 '12 at 15:23

3 Answers3

2

Let me explain it to you,

  1. When you create an instance of the Subclass by calling new on the sub class type, then immediately its Super class constructor is called, and it keeps going till the Object class, this is called Constructor chaining.

  2. All the instance variable are declared and initialized during this process.

  3. And most important is that when constructor of subclass it called it goes to its super class and so on till the Object class, then 1st creating the Object class object, then the class under it, till it reaches the subclass on whose class new was called, You will see that the constructor of the super class is called first then its subclass's

And for your above question i have also created an example which fits appropriately with the theoretical explanation i have given.

eg:

      public class a {
    public static void main(String[] args) {

        B b = new B();
        b.go();
    }
}

class B extends a{


    public void go(){

        System.out.println("hello");

    }
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

Well I tried it out and it does work fine even if I put the main method in the parent class. But I have done some reading and the observed practice is to create the main method in a separate class such that the class has only the main method in it and no other methods. This improves efficiency in some cases and is a much cleaner way of donig things.

anonuser0428
  • 11,789
  • 22
  • 63
  • 86
0

You can call method of class B from class a only by creating object of class B in class a, if you do not want to use inheritance. You can't call method of class B through class a from object of class a without using inheritance.

public class a {
    public static void main(String[] args) {

        B b = new B();
        b.go();
    }
}

class B {


    public void go(){

        System.out.println("hello");

    }
} 

Using inheritance:

//calling method of class B by using object of class a.

public class a extends B {
    public static void main(String[] args) {

        a b = new a();
        b.go();
    }
}

class B {


    public void go(){

        System.out.println("hello");

    }
} 
cigien
  • 57,834
  • 11
  • 73
  • 112
Blo Mo
  • 1
  • 2