无法调用超类方法

时间:2018-09-25 09:06:53

标签: java oop inheritance

  • 这是一个简单的继承演示。在这里我无法打电话给 超类方法。
  • 我正在尝试在此处实现方法重写。

class supers {
    int x, y;

    supers(int a, int b) {
        x = a;
        y = b;
    }

    final void volume() {
        System.out.println("area is " + x * y);
    }
}

class sub extends supers {
    int z;

    sub(int a, int b, int c) {
        super(a, b);
        z = c;
    }

    void volume() {
        System.out.println("volume is " + x * y * z);
    }
}

class m{
    public static void main(String args[]) {
        supers obj = new sub(1, 2, 3);
        obj.volume();
    }
}

5 个答案:

答案 0 :(得分:0)

一个问题是您在volume()类中将final声明为supers。这意味着编译器禁止在子类中覆盖它。

方法可能也需要声明为public。这取决于这些类是否在相同的(命名的)包 1 中。 (如果是,则方法可以保留为包私有。如果不是,则方法,构造函数和/或类本身可能需要为public。)


最后,您需要学习Java命名约定。类名应始终以大写字母开头。


1-从片段中不清楚。它们似乎显示了没有package声明的单个文件。但这将无法编译...,因为包含“默认”包中的类的源文件需要其中一个类为public。很难在“默认”程序包中引用程序包专用类。基本上,对于实际应用而言,使用“默认”包是一个坏主意。

答案 1 :(得分:0)

void volume()而不是final void volume

请勿使用final关键字。您的程序应如下所示:

import java.util.*;

class Supers {
    int x,y; 
    Supers(int a,int b) {
    x = a;
    y = b;
} 
void volume() {
    System.out.println("area is "+ x*y); 
 } 
} 
  class Sub extends Supers {
  int z; 
  Sub(int a,int b,int c) { 
  super(a,b); 
  z = c;
} 
  void volume() { 
  super.volume(); // Call volume() method of Supers class
  System.out.println("volume is "+ x*y*z); 
 }  
}

class M {
   public static void main(String args[]) {
   Sub obj = new Sub(1,2,3);
   obj.volume(); 
 } 
}

如果您想防止子类覆盖超类的成员,请使用final

在以上程序中,子类和父类中的方法volume()具有相同的名称和参数。因此,volume()方法在继承父类时将在子类中被覆盖。这就是为什么无法打印行System.out.println("area is "+ x*y);的原因,因为子类的volume()方法将覆盖该方法。因此,行System.out.println("volume is "+ x*y*z);将被执行。

要调用Supers类的volume方法,最有效的方法是在子类的volume()方法中使用super.volume(),如上面的代码所示。

输出:

area is 2 volume is 6

或者我们可以在main方法中创建一个对象。然后看起来像这样:

class M {
   public static void main(String args[]) {
   Supers obj1 = new Supers(2,4);
   obj1.volume();
   Sub obj = new Sub(1,2,3);
   obj.volume(); 
  } 
}

或者我们可以只在子类的volume()方法中添加System.out.println("area is "+ x*y);而不是访问Super类的volume()方法。

void volume() { 
   System.out.println("area is "+ x*y); 
   System.out.println("volume is "+ x*y*z); 
} 

答案 2 :(得分:0)

  • 首先,由于方法是final,因此不能在派生类中重写该方法。因此必须删除最终关键字。
  • 第二,您可以使用super关键字调用基类方法。
  • 派生类可以通过super.method_name();进行如下修改:

    class sub extends supers {
    
        int z;
    
        sub(int a, int b, int c) {
            super(a, b);
            z = c;
        }
    
        void volume() {
            super.volume();
            System.out.println("volume is " + x * y * z);
        }
    
    }
    

答案 3 :(得分:0)

class Super {

    final void inherit() {
    // the method states that you cannot inherit this method to you sub-class
    }
}

class SubClass extends Super {

    //This is not allowed as you cannot inherit and get the compile time exception to remove final keyword.

    /*void inherit() {

    }*/
}

要调用超类的volume方法,您需要从超类的volume方法中删除final关键字。

答案 4 :(得分:0)

只需删除final关键字,因为它会使您无法覆盖。 另外,在超级关键字之后使用类的名称。