Extends, Overriding
โ Extends
- if
extends, extends the field and method of parent class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Car {
public void move(){}
}
public class GasCar extends Car{
public void fillGas(){}
}
public class ElectricCar extends Car{
public void charge(){}
}
public class CarMain {
public static void main(String[] args) {
ElectricCar electricCar = new ElectricCar()
electricCar.charge();
electricCar.move(); //electricCar can move(parent car class)
}
}
- ๐๐ป if I want to add
openDoor()to all cars, just add toCarclass- as both
GasCarandElectricCarextendsCars, can easily add field, methods
- as both
- ๐๐ป save code repetition
move()can be called both inGasCar,ElectricCar
โ Extends and memory
- 1๏ธโฃ when
child class extending a parent classis created, in memory, both child and parent class is made Electric carclassextendstheCar class- when
ElectricCar electricCar = newElectricCar(); - in memory, it looks like this
- When
methodis called - 2๏ธโฃ first find in created
instance type- if
ElectricCar electricCar = new ElectricCar();, look inElectricCar
- if
- 3๏ธโฃ if
methoddoes not exist, - look in
extends parent type, look inCar - if
methoddoes not exist in any parent, compile error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Car {
public void move(){}
}
public class ElectricCar extends Car{
public void charge(){}
}
public class CarMain {
public static void main(String[] args) {
ElectricCar electricCar = new ElectricCar();
electricCar.charge();
electricCar.move();
}
}
- โ๏ธ call method
electricCar.charge(); - in
ElectricCar electricCar = new ElectricCar();, there is bothCarandElectric car ElectricCar๊ฐ๋๋Car๋ ์๊ณElectric car๋ ์์- look in
ElectricCarfirst - bc
electricCartype isElectricCar, findElectricCarfirst ์ ๋๋
electricCarํ์ ์ด๋ค.electricCar๋จผ์ ์ฐพ์- โ๏ธ call method
electricCar.move(); - look in
ElectricCarfirst - ๋๋
electricCartype์ด๋๊นelectricCar๋จผ์ ์ฐพ์๋๋ฐ,move()์์ - method
move()does not exist - look in
Car(parent class) next - ๊ทธ๋ฌ๋ฉด ๋ถ๋ชจ ํด๋์ค
Car์ฐพ์
โ Extends and overriding
- child class can
overrideparent class method and recreate a new method
1
2
3
4
5
6
7
8
public class Car { //parent
public void move(){}
}
public class ElectricCar extends Car { //child
@Override
public void move(){} //override
}
- โ๏ธ call method
electricCar.move(); electricCartype isElectricCar, findElectricCarfirst- there is
move()inElectricCar - does not find parent class for
move() - โญ๏ธ ์์ ํด๋์ค์์
field/method์ฐพ์์ผ๋๊น ๋ถ๋ชจ ํด๋์ค๊น์ง ์ฐพ์ง ์์ - โ
field/method์ด๋ฆ์ด ๊ฐ์๋ ๋ถ๋ชจ ํด๋์ค ์ฐธ์กฐํ๊ณ ์ถ์ผ๋ฉด? โก๏ธsuper
Overriding ๐ Overloading
- Overloading: create several method with same method name, different parameter
- Overriding: in
extendsrelationship, child class recreates parent method
โ Overriding conditions
- same method name
- same parameter
- same return value
- broader access modifier
staticโ : method overriding takes place inheaparea of memorystaticis inmethodarea of memory
finalโ : final means method cannot be altered, thus cannot overrideprivateโ : private means only accessible in same class, cannot be accessed in child class- constuctor is not overrideable
โ Overriding and protected
publicprotected: same package, allowextendsin different packagedefault: same packageprivate: same class- Parent
private field and methodcan NOT be used by child - Parent
defaut field and methodcan be used only if parent and child are in same package Parent
protected field and methodcan be used even if parent and child are in different package- If
parent and childare in different packageโฆ
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Parent {
public int publicValue;
protected int protectedValue;
int defaultValue;
private int privateValue;
}
public class Child extends Parent{
publicValue = 1;
protectedValue = 2; //even if different package, extends can use protected field
//defaultValue //only same package
//privateValue
}
โ Super
- when child class
@Overrideparent class, childfield/methodwill be used - however, if child class wants to use parent class
field/method, usesuper - super: in same
field/methodname of child/parent class, use parent class
1
2
3
4
5
public class Parent {
public String value = "parent"; //same field name
public void hello(){} //same method name
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Child extends Parent{ //extends parent
public String value = "child"; //same field name
@Override
public void hello(){} //same method name โก๏ธ override
public void call(){
System.out.println("this.value= " + this.value); //find in my class(child)
System.out.println("super.value= " + super.value); //โ๏ธ find in parent
this.hello(); //find in my class(child)
super.hello(); //โ๏ธ find in parent
}
}
โ Super and Constructor
- when create
child instance, bothchild instanceandparent instanceis created - in
child constructor, callsparent constructorwithsuper - โญ๏ธ order:
parentโก๏ธchildclassAclassB extends classAclassC extends classB- order:
classAโก๏ธclassBโก๏ธclassC - ๊ฒฐ๋ก : ์์ ํด๋์ค์ ๊ฒฝ์ฐ ์์๋ง ์์ฑํด๋ ํญ์ ๋ถ๋ชจ๊ฐ ์์ฑ๋๋๋ฐ, ์์๋ ๋ถ๋ชจ๊ฐ ๋จผ์ ์์ฑ์ด ๋๊ณ , ์์์ด ์์ฑ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ClassA {
public ClassA() { //classA constructor //3๏ธโฃ
System.out.println("class A constructor"); //4๏ธโฃ
}
}
public class ClassB extends ClassA{
public ClassB(int a, int b){ //classB constructor
super(); //call parent classA constructor //2๏ธโฃ
System.out.println("Class B constructor a= " + a + " b= "+ b); //5๏ธโฃ
}
}
public class ClassC extends ClassB{
public ClassC(int a, int b) { //classC constructor
super(a, b); //call parent classB constructor //1๏ธโฃ
System.out.println("Class c constructor"); //6๏ธโฃ
}
}
- โ๏ธ Result
1
2
3
class A constructor
Class B constructor a= 7 b= 1
Class c constructor
โ
This post is licensed under CC BY 4.0 by the author.
