1
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 3 years have passed since last update.

【JAVA】 superとは

Posted at

#【JAVA】superとは

##superとは
継承元を呼び出す

##ルール
superは、コンストラクタの最初に書かないといけない

##superの使い方
##super()

main
public class Main {
    public static void main(String[] args) {
        SubRabbit sub = new SubRabbit();
    }
}

// サブクラス
class SubRabbit extends Rabbit {
    public SubRabbit() {
        super();
    }
}

// スーパークラス
class Rabbit {
     public Rabbit(){
        System.out.println("うさぎ");
    }
}
実行結果
うさぎ

##super(引数)

main
public class Main {
    public static void main(String[] args) {
        SubRabbit sub = new SubRabbit();
    }
}

class SubRabbit extends Rabbit {
    public SubRabbit() {
        super("むぎ");
    }
}

class Rabbit {
     public Rabbit(){
        System.out.println("うさぎ");
    }
     public Rabbit(String rabbitName) {
    	 System.out.println("うさぎの名前:" + rabbitName);
     }
}
実行結果
うさぎの名前むぎ

##super.変数

main
public class Main {
    public static void main(String[] args) {
        SubRabbit sub = new SubRabbit();
    }
}

class SubRabbit extends Rabbit {
    public SubRabbit() {
        System.out.println(super.usagi);
    }
}

class Rabbit {
	String usagi = "うさぎ";
}
実行結果
うさぎ

##super.関数

main
public class Main {
    public static void main(String[] args) {
        SubRabbit sub = new SubRabbit();
    }
}

class SubRabbit extends Rabbit {
    public SubRabbit() {
        super.RabbitInfo();
    }
}

class Rabbit {
	public Rabbit() {
	}
	
	public void RabbitInfo() {
		System.out.println("うさぎはかわいい");
	}
}
実行結果
うさぎはかわいい
1
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
1
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?