Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Linux
- javascript
- for
- Java
- control
- MIPS
- MacOS
- function
- github
- DB
- CSS
- computer
- Class
- while
- DoM
- mysql
- architecture
- Algorithm
- system
- XML
- instruction
- react
- DS
- php
- Pipelining
- html
- data structure
- web
- DATAPATH
- python
Archives
- Today
- Total
YYYEJI
[JAVA] Super 메소드 본문
728x90
Super을 공부하기 위해서는 상속의 개념을 알아야 됩니다.
https://yyyeji.tistory.com/348
[JAVA] 상속(Inheritance)란?
상속(Inheritance)란? 패키지(Package) 안에는 상위 클래스(부모 클래스, base or parent class)와 하위 클래스(자식 클래스, derived or child class)가 존재합니다. 하위 클래스는 선택한 상위 클래스의 멤버를 상
yyyeji.tistory.com
Super() 는
하위 클래스에서 상위 클래스의 instance 변수를 초기화할 때 사용하는 메소드(method)입니다.
상위 클래스(부모 클래스)
public class Person {
private String name;
public Person() {
name = "";
}
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
하위 클래스(자식 클래스)
public class Student extends Person {
private int studentNumber;
public Student() {
super();
studentNumber = 0;
}
public Student(String name, int number) {
super(name);
studentNumber = number;
}
}
상위 클래스의 멤버에 접근할 때 상위 클래스의 생성자(constructor)를 호출할 때 사용하게 됩니다.
상위 클래스의 method를 호출할 수도 있습니다.
// Parent class
public void writeOutput() {
System.out.println("Name: " + name);
}
// Child class
public void writeOputput() {
super.writeOutput();
System.out.println("Age: " + age);
}
super를 통해 상위클래스의 메소드(method)를 호출하고 그 뒤로 자기 자신 부분만 수행할 코드만 작성해 주면 됩니다.
◡̈
'Java' 카테고리의 다른 글
[JAVA] 인터페이스(Interface)란? (0) | 2022.12.27 |
---|---|
[JAVA] 오버라이딩(Overriding)이란? (0) | 2022.12.27 |
[JAVA] 상속(Inheritance)란? (0) | 2022.12.27 |
[JAVA] Java의 다차원 Array (0) | 2022.12.27 |
[JAVA] Java의 1차원 array (0) | 2022.12.27 |