0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

superとsuper()

Last updated at Posted at 2022-08-03

thisと差

thisは現在のクラスのメンバー変数を指し、superは相続クラスのメンバー変数を指す。

現在のクラス: this.変数
相続クラス: super.変数

Overriding

overridingは親クラスを受け継いだメソッドを子クラスで再利用することだ
条件 : メソッド名, リターンタイプ ,パラメータの個数が同じでなければならない

public class index{
  public static void main(String[] args){
     Parent parent = new Parent();
     parent.sum();

     Child child = new Child();
     child.sum();
  }
}

public class parent{
  int sum;
  public int sum(int x, int y){
     sum = x + y;
     
     return sum;
  }
}

public class child extend parent{
  int sum;
@Overriding
  public int sum(int x, int y){
     sum = x + y + x;

     return sum;
  }
}

super

superとは親クラスのメンバー変数を指す。

public class parent{
  int x;
  int y;

  public parent(){
     x = 10;
     y = 5;
  }

  public parent(int x, int y){
     this.x = x;
     this.y = y;
  }
}

public class child extends parent{
  public child(){
     super.x = 1;
     super.y = 2;
  }

super()

super()は相続したクラスのConstructorを呼び出す

public class parent{
   int x;
   int y;

   public parent(){
      x = 10;
      y = 5;
   }

   public parent(int x, int y){
      this.x = x;
      this.y = y;
   }
}

public class child extends parent{
   public child(int x, int y){
      super(x, y);
   }
} 

規則

this()、super()呼び出し時に一番最初の行に書かなければならない規則がある。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?