Post

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 to Car class
    • as both GasCar and ElectricCar extends Cars, can easily add field, methods
  • πŸ‘πŸ» save code repetition
    • move() can be called both in GasCar, 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 class extends the Car class
  • when ElectricCar electricCar = newElectricCar();
  • in memory, it looks like this

Image

  • When method is called
  • 2️⃣ first find in created instance type
    • if ElectricCar electricCar = new ElectricCar();, look in ElectricCar
  • 3️⃣ if method does not exist,
  • look in extends parent type, look in Car
  • 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 both Car and Electric car
  • ElectricCarκ°”λ”λ‹ˆ Car도 있고 Electric car도 있음
  • look in ElectricCar first
  • bc electricCar type is ElectricCar, find ElectricCar 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
}

Image

  • βœ”οΈ call method electricCar.move();
  • electricCar type is ElectricCar, find ElectricCar first
  • there is move() in ElectricCar
  • 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 in heap area of memory
    • static is in method area of memory
  • final ❌ : final means method cannot be altered, thus cannot override
  • private ❌ : private means only accessible in same class, cannot be accessed in child class
  • constuctor is not overrideable

βœ… Overriding and protected

  • public
  • protected: same package, allow extends in different package
  • default: same package
  • private: 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, child field/method will be used
  • however, if child class wants to use parent class field/method, use super
  • 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, both child instance and parent instance is created
  • in child constructor, calls parent constructor with super
  • ⭐️ 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.