Inheritance

Inheritance

  • Extends keyword ์‚ฌ์šฉ

  • Java๋Š” Single Inheritance ๋ฅผ ํ•œ๋‹ค

    • ํ•œ๋ฒˆ์— ํ•˜๋‚˜๋งŒ ์ƒ์† ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

  • ex) Dog extends Animal

    => Dog๋ผ๋Š” class๋Š” Animal Class๋ฅผ ์ƒ์† ๋ฐ›๋Š”๋‹ค

  • super keyword๋กœ ๋ถ€๋ชจ class์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค

    => super();

  • ์ƒ๋žตํ•ด๋„ ์ž๋™ ํ˜ธ์ถœ๋˜๋Š” super()๋Š” ๋ถ€๋ชจ์˜ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค

  • ๋ชจ๋“  ๊ฐ์ฒด์˜ ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋Š” object๋‹ค!!!!!!!!

  • ๋ฉ”๋ชจ๋ฆฌ์— ์–น์–ด์งˆ ๋•Œ๋Š” ์ƒ๋‹จ์— ๋ถ€๋ชจ, ํ•˜๋‹จ์— ์ž์‹

  • data๋ฅผ search ํ•  ๋•Œ๋Š” ํ•˜๋‹จ์˜ ์ž์‹๋ถ€ํ„ฐ ์ƒ๋‹จ์˜ ๋ถ€๋ชจ์ชฝ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋Š” ํ˜•์‹

    => ๋ฐ‘์—์„œ๋ถ€ํ„ฐ ์ฐพ์•„์„œ ์—†์œผ๋ฉด ์˜ฌ๋ผ๊ฐ€๋Š” ํ˜•์‹

  • ex) data type์ด Dog์ด๋ฉด Animal, Object ๋ชจ๋‘ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค-

  • super๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ž์‹ class ํ•˜๋‚˜๋ฅผ jumpํ•ด์„œ ๋ถ€๋ชจ๋ถ€ํ„ฐ search ํ•  ์ˆ˜ ์žˆ๋‹ค

  • is a ๊ด€๊ณ„

    => ๋ชจ๋“  ๊ฐ์ฒด์˜ Data Type์€ ๋ถ€๋ชจ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค

    • data ํ˜ธํ™˜์€ ์ƒํ•˜ ๊ด€๊ณ„์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค

      • ํ˜•์ œ ๊ด€๊ณ„์—์„œ๋Š” ์•ˆ๋จ!

    • ๋ถ€๋ชจ type์€ ํ•˜๋‹จ๋ถ€์— ์žˆ๋Š” ์ž์‹ data์— ์ ‘๊ทผ ๋ถˆ๊ฐ€

super();

  • ๋ถ€๋ชจ์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

    • ์ง€์›Œ๋„ ์ž๋™์œผ๋กœ ๋“ค์–ด๊ฐ!

    • ์—†์–ด๋„ ์ž๋™ํ˜ธ์ถœ ๋จ!

  • ์•ˆ๋ณด์ด๋ฉด ์ƒ๋žต๋˜์–ด์žˆ๋Š” ๊ฒƒ์ž„!

  • this() method์ฒ˜๋Ÿผ first statement์—๋งŒ ํ—ˆ์šฉ๋œ๋‹ค!

    • ๊ทธ๋ž˜์„œ this() method๊ฐ€ ์‚ฌ์šฉ๋œ ์ƒ์„ฑ์ž์—์„œ๋Š” super() ๊ฐ€ ์—†๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Œ

    • super.______ ํ•˜๋ฉด ์ž์‹์ด ์•„๋‹ˆ๋ผ ๋ถ€๋ชจ์˜์—ญ๋ถ€ํ„ฐ search ํ•  ์ˆ˜ ์žˆ๋‹ค!

  • this. Keyword์™€ super. Keyword ๋ชจ๋‘ heap์˜์—ญ์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค!!!!

Method Overloading

  • ํ•˜๋‚˜์˜ ํด๋ž˜์Šค ์•ˆ์—๋Š” ๋™์ผํ•œ ์ด๋ฆ„์˜ method๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค

    • ๋‹จ, method์˜ parameter์˜ type์ด๋‚˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ์•ผํ•œ๋‹ค!

Constructor

  • ๊ฐ์ฒด ์ƒ์„ฑ์‹œ์— ๋งŒ๋“ค์–ด์ง€๋Š” ํ•จ์ˆ˜

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ด๋ฆ„์€ class๋ช…๊ณผ ๋™์ผ-

  • return type์— ๋Œ€ํ•ด ์–ธ๊ธ‰ํ•˜๋ฉด ์•ˆ๋จ- method overloading์ด ์ ์šฉ๋œ๋‹ค

  • ์ƒ์„ฑ์ž๋“ค๋„ method name์œผ๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค

    • this();

  • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž = default (๊ธฐ๋ณธ) ์ƒ์„ฑ์ž

  • public์„ ๋ถ™์ผ ์ˆ˜๋„ ์•ˆ ๋ถ™์ผ ์ˆ˜๋„ ์žˆ๋‹ค

  • ์ ‘๊ทผ ์ง€์ •์ž๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

Overriding in Java

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

  • ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ๋ฌผ๋ ค ๋ฐ›์€ ๊ธฐ๋Šฅ์„ ๋‹ค์‹œ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ!

  • method ์„ ์–ธ๋ถ€๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜ค๋ฉด ๋จ!

  • overriding์—๋Š” ๊ฐ•์ œ์„ฑ์ด ์—†๋‹ค!

    • ๊ทธ๋ž˜์„œ ์—๋Ÿฌ ์•ˆ๋œธ.. ์•Œ์•„์„œ ๊ณ ์ณ๋ผ~

  • ์ƒ์†์— ๋Œ€ํ•ด ์–ธ๊ธ‰ํ•˜์ง€ ์•Š์œผ๋ฉด java.lang.object๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์†๋ฐ›๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Œ!

  • Class Animal extends Object ๋‹ค!

  • Object = ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ method

    • ๊ทธ ์–ด๋–ค ๊ฐ์ฒด๋„ ์ตœ์ƒ๋‹จ์—๋Š” Object!

Is a

  • ๋ชจ๋“  ๊ฐ์ฒด์˜ Data Type์€ ๋ถ€๋ชจ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค!!

    • ๋ชจ๋“  ๊ฐ์ฒด์˜ Data Type์€ Object๋‹ค!

      • why? Java์˜ ๋ชจ๋“  ๊ฐ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ตœ ์ƒ๋‹จ๋ถ€์—๋Š” Object๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ!

ex1)

Animal a = new Animal();
Object
  • Animal์˜ ๋ถ€๋ชจ ๊ฐ์ฒด๋Š” Object์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด ์„ ์–ธ ์‹œ data type์œผ๋กœ Object๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค!

  • ํ•˜์ง€๋งŒ Object type์ด๋ฉด, Animal ์•ˆ์— ์žˆ๋Š” data๋Š” ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๊ณ , access ํ•  ์ˆ˜ ์žˆ๋Š” type์ด ์ตœ์ƒ๋‹จ๋ถ€ ๋งŒ ๊ฐ€๋Šฅ!!

ex2)

Animal d = new Dog();
Dog 
    Object
  • Dog์˜ ๋ถ€๋ชจ ๊ฐ์ฒด๋กœ Animal ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด ์„ ์–ธ ์‹œ Animal, Object ํ•  ์ˆ˜ ์žˆ๋‹ค!

    • Animal๋กœ ์„ ์–ธ์‹œ, Dog์€ ์ ‘๊ทผ ๋ถˆ๊ฐ€, Animal ์ด์ƒ(Animal + Object) ์ ‘๊ทผ ๊ฐ€๋Šฅ

  • Java์— ์žˆ๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋Š” Object type์ด ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, access ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ณด๊ฐ€ ์ œํ•œ์ ์ž„!

  • Type casting์„ ํ†ตํ•ด์„œ ๋ถ€๋ชจ ๊ฐ์ฒด์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค!

    => ๊ฐ์ฒด์—์„œ์˜ ์บ์ŠคํŒ… -> Up casting

    Ex) ((Animal)d).kind;

    โ€‹ => ์›๋ž˜ Dog์ธ ๊ฒƒDog๋ฅผ Animal๋กœ up castingํ•˜๋ฉด ํ•œ๋ฒˆ์— Animal๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ

Annotations

: a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.

-> ex) @Override

Polymorphism (๋‹คํ˜•์„ฑ)

  • the ability of an object to take on many forms

    • overriding ๊ธฐ์ˆ ์„ ์ ‘๋ชฉํ•˜์—ฌ method๋Š” ํ•˜๋‚˜์ธ๋ฐ ๋‹ค์–‘ํ•œ type์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ!

Last updated