سؤال

I'm an undergrad college student attempting to understand Inheritance in Java. The docs.oracle site says all members of a class are inherited, with the exception of constructors. Which makes sense. Problem is I did an experiment and it didn't work. Here it is:

public class One{
    public void outPrint(){
        System.out.println("Hello World!");
    }//end outPrint
}//end One.java

public class Two extends One{
    //empty
}//end Two.java

public class Three extends Two{
    public static void main(String[]args){
        outPrint();
    }//end main
}//end Three.java

When I run Three I get: non-static method outPrint() cannot be referenced from a static context. This is of course because the compiler is seeing outPrint() as an instance member. If I add the keyword "static" to the outPrint() method header the whole thing works just fine.

This is where my confusion lies. It seems it not just constructors that are not inheritable, but also all of its instance members. Could anyone explain this a little better to me? Is there a work around that does not involve the use of "static"? I tried a few experiments with "super" to no avail. Thanks in advance!

هل كانت مفيدة؟

المحلول

You need to instantiate an object to call upon.

e.g.

Three t = new Three();
t.outPrint();

The main() method you'd defined is static and doesn't have an instance of an object (One/Two/Three). It's merely existing in a particular namespace.

Note that you can demonstrate that Three is-a One thus:

One t = new Three();
t.outPrint();

and if you override the outPrint() method for each subclass you can see which method is called depending on how you instantiate and/or refer to the original object instance.

نصائح أخرى

You are trying to call a non-static method without an instance. Instance methods can only be called using an isntance of the class. Creating an instance, will help you call the method, something like this:

Three threeInstance = new Three();
threeInstance.outPrint();

For that you need to crate object of class Three or class Two or class One.

    public class Three extends Two
{
    public static void main(String[]args)
    {

              Two t= new Two();         
              t.outPrint();

     }
}

Try this

public class Three extends Two{

    public static void main(String[]args){
        Three three = new Three();
        three.outPrint(); //outPrint() can only be called by an instance
    }//end main

}//end Three.java

You cannot access non-static methods from a static method even if it was in the same class. You will have to access them with an instance.

However, the following is also possible inside Three.java class:

public class Three extends Two{

    public static void main(String[]args){
        Three three = new Three();
        three.printSomething("Hi");
        // this will output:
        // Hi
        // Hello World
    }//end main
    public void printSomething(text) {
        System.out.println(text);
        outPrint(); //implicit "this" which refers to this instance....it can be rewritten as this.outPrint();
    }
}//end Three.java
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top