私は上書きしたい2つのメソッドのスーパークラスを持っています。ここに私のコードです:私はいくつかのコンストラクタを持っている拡張スーパークラスとClassCastException
public class MyCustomClass extends SomeSuperClass {
protected MyCustomClass(params) {
super(params);
}
@Override
public void method1() {
super.method1();
/* here goes my code */
}
@Override
public void method2() {
super.method2();
/* here goes my another code */
}
、パラメータとしてSomeSuperClassオブジェクトを渡し、私は次の何をすべきか:
MyCustomClass object;
/* now i have object of type SomeSuperClass,
but with my own method1() and method2() */
object = (MyCustomClass) MyCustomClass.item(blahblah);
/* eclipse suggests casting, because MyCustomClass.item()
constructor still returns SomeSuperClass object */
otherobject = OtherConstructor.object(object);
//OtherConstructor passes SomeSuperClass object
右のようですが、私は、Javaを取得していますこと。実行中のSomeSuperClassでlang.ClassCastExceptionが発生しました。
SomeSuperClassObjectを作成すると、オーバーライドされたメソッドが失われます。
キャストすると、eclipseにエラーがなくてもアプリケーションがクラッシュします。 つまり、私はSomeSuperClassを自分のメソッドでオーバーライドでき、SomeSuperClassオブジェクトをOtherConstructorと共に使用することができますか? 重要な場合は、このコードはAndroidアプリ向けです。
「MyCustomClass.item」のコードは何ですか? – Powerlord
これはオーバーライドされていないので、スーパークラスメソッド – artemy
を使用するだけで、 'MyCustomClass.item()'はコンストラクタでも 'OtherConstructor.object() ' –