0

This is the code I'm practicing for multilevel inheritence, but when I run it the online compiler gives me following errors:

public class MyClass {
       ^
/grandFather.java:26: error: non-static method talking() cannot be referenced from a static context
      son.talking();
         ^
/grandFather.java:27: error: non-static method walking() cannot be referenced from a static context
      son.walking();
         ^
/grandFather.java:28: error: non-static method running() cannot be referenced from a static context
      son.running();

  

This is the code I'm running. It's a practice code for multilevel inheritence in java. I have created 3 class(grandFather, father and son):

public class grandFather{
        int age;
        double height;
        
        public void talking(){
            System.out.println("Grandfather can walk");
        }
    }
    
    class father extends grandFather{
        public void walking(){
            System.out.println("Father can walk too");
        }
    }
    
    class son extends father{
        public void running(){
            System.out.println("Son can talk, walk and also run");    
        }
        
    }
    
    public class MyClass {
        public static void main(String args[]) {
          son kamran = new son();
          son.talking();
          son.walking();
          son.running();
        }
    }
Laurel
  • 5,965
  • 14
  • 31
  • 57
kamran afzal
  • 19
  • 1
  • 8
  • `kamran.talking()`, `kamran.walking()`, ... If you followed class naming conventions, the problem would be easier to spot. – Robby Cornelissen Feb 18 '21 at 08:11
  • What does that mean? – kamran afzal Feb 18 '21 at 08:13
  • What does what mean? All details about your error [here](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static). – Robby Cornelissen Feb 18 '21 at 08:16
  • Does this answer your question? [What is the reason behind "non-static method cannot be referenced from a static context"?](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – Laurel Feb 19 '21 at 04:31

1 Answers1

0

Bascially I was doing: "son.talking()" instead it was "kamran.talking".

public class grandFather{
    int age;
    double height;
    
    public void talking(){
        System.out.println("Grandfather can walk");
    }
     public static void main(String args[]) {
      son kamran = new son();
      kamran.talking();
      kamran.walking();
      kamran.running();
    }
}

class father extends grandFather{
    public void walking(){
        System.out.println("Father can walk too");
    }
}

class son extends father{
    public void running(){
        System.out.println("Son can talk, walk and also run");    
    }
    
}
kamran afzal
  • 19
  • 1
  • 8