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 toCar
class- as both
GasCar
andElectricCar
extendsCars
, 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 class
is created, in memory, both child and parent class is made Electric car
classextends
theCar class
- when
ElectricCar electricCar = newElectricCar();
- in memory, it looks like this
- When
method
is called - 2οΈβ£ first find in created
instance type
- if
ElectricCar electricCar = new ElectricCar();
, look inElectricCar
- if
- 3οΈβ£ if
method
does not exist, - look in
extends parent type
, look inCar
- if
method
does 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 bothCar
andElectric car
ElectricCar
κ°λλCar
λ μκ³Electric car
λ μμ- look in
ElectricCar
first - bc
electricCar
type isElectricCar
, findElectricCar
first μ λλ
electricCar
νμ μ΄λ€.electricCar
λ¨Όμ μ°Ύμ- βοΈ call method
electricCar.move();
- look in
ElectricCar
first - λλ
electricCar
typeμ΄λκΉelectricCar
λ¨Όμ μ°Ύμλλ°,move()
μμ - method
move()
does not exist - look in
Car
(parent class) next - κ·Έλ¬λ©΄ λΆλͺ¨ ν΄λμ€
Car
μ°Ύμ
β Extends and overriding
- child class can
override
parent 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();
electricCar
type isElectricCar
, findElectricCar
first- 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
extends
relationship, child class recreates parent method
β Overriding conditions
- same method name
- same parameter
- same return value
- broader access modifier
static
β : method overriding takes place inheap
area of memorystatic
is inmethod
area 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
public
protected
: same package, allowextends
in different packagedefault
: same packageprivate
: same class- Parent
private field and method
can NOT be used by child - Parent
defaut field and method
can be used only if parent and child are in same package Parent
protected field and method
can be used even if parent and child are in different package- If
parent and child
are 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
@Override
parent class, childfield/method
will be used - however, if child class wants to use parent class
field/method
, usesuper
- super: in same
field/method
name 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 instance
andparent instance
is created - in
child constructor
, callsparent constructor
withsuper
- βοΈ order:
parent
β‘οΈchild
classA
classB extends classA
classC 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.