class Parent {
void show() {
System.out.println("Parent");
}
}
class Child extends Parent {
void show() {
System.out.println("Child");
}
}
public class Test1 {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}
๐๏ธ Child
ํด์ค:Parent obj = new Child();
์ํ์์obj.show()
ํธ์ถ โ ์ค๋ฒ๋ผ์ด๋ฉ๋Child
์show()
์คํ
class Overload {
void print(int a) {
System.out.println("int");
}
void print(double a) {
System.out.println("double");
}
}
public class Test2 {
public static void main(String[] args) {
Overload obj = new Overload();
obj.print(10.5f);
}
}
๐๏ธ double
ํด์ค:10.5f
์float
์ด์ง๋ง ์ ํํ ์ผ์นํ๋ ๋ฉ์๋๊ฐ ์๊ณ , ์๋ ํ๋ณํ์ผ๋กdouble
๋ฒ์ ํธ์ถ๋จ
class A {
static void hello() {
System.out.println("Hello from A");
}
}
class B extends A {
static void hello() {
System.out.println("Hello from B");
}
}
public class Test3 {
public static void main(String[] args) {
A obj = new B();
obj.hello();
}
}
๐๏ธ Hello from A
ํด์ค:static
๋ฉ์๋๋ ์ธ์คํด์ค ํ์ ์ด ์๋๋ผ ์ฐธ์กฐ ๋ณ์ ํ์ ๊ธฐ์ค์ผ๋ก ํธ์ถ๋จ โA obj = new B();
์ด๋ฏ๋กA.hello()
ํธ์ถ
๐ ํ๋ฆฐ ์ด์ :static
์ ์ค๋ฒ๋ผ์ด๋ฉ์ด ์๋ ํ์ด๋ฉ์ด๋ฉฐ, ๋์ ๋ฐ์ธ๋ฉ์ด ์ ์ฉ๋์ง ์์
public class Test4 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
๐๏ธ
false
true
ํด์ค:
==
โ ์ฃผ์ ๋น๊ต (false),equals()
โ ๋ฌธ์์ด ๋ด์ฉ ๋น๊ต (true)
class Super {
String name = "Super";
String getName() {
return name;
}
}
class Sub extends Super {
String name = "Sub";
String getName() {
return name;
}
}
public class Test5 {
public static void main(String[] args) {
Super obj = new Sub();
System.out.println(obj.name);
System.out.println(obj.getName());
}
}
๐๏ธ
Super
Sub
ํด์ค:obj.name
์ ๋ณ์ ์จ๊ธฐ๊ธฐ ๋๋ฌธ์Super
์ถ๋ ฅ,
getName()
์ ์ค๋ฒ๋ผ์ด๋ฉ๋Sub
์ชฝ ์คํ
class Demo {
void process(Object o) {
System.out.println("Object");
}
void process(String s) {
System.out.println("String");
}
}
public class Test6 {
public static void main(String[] args) {
Demo demo = new Demo();
demo.process(null);
}
}
๐๏ธ String
ํด์ : ์ค๋ฒ๋ก๋ฉ์ ๊ฐ์ฅ ๊ตฌ์ฒด์ ์ธ ํ์ ์ฐ์ !
null
์Object
,String
๋ชจ๋์ ํด๋นํ์ง๋ง,String
์ด ๋ ๊ตฌ์ฒด์ ์ด๋ฏ๋ก ์ ํ๋จ
โ ํ๋ฆฐ ์ด์ : ์ค๋ฒ๋ก๋ฉ ํด์ ์ "๋ ๊ตฌ์ฒด์ ์ธ ํ์ ์ฐ์ " ์ด๋ ๊ท์น์ ๋์นจ
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Dog extends Animal {
void speak() {
System.out.println("Dog barks");
}
void wagTail() {
System.out.println("Dog wags tail");
}
}
public class Test7 {
public static void main(String[] args) {
Animal a = new Dog();
a.speak();
// a.wagTail(); // ์ปดํ์ผ ์๋ฌ
}
}
๐๏ธ Dog barks
ํด์ค:Animal a = new Dog();
โa.speak()
ํธ์ถ ์ ์ค๋ฒ๋ผ์ด๋ฉ๋Dog
์speak()
ํธ์ถ๋จ
class Person {
String name;
Person(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (o instanceof Person) {
return this.name.equals(((Person) o).name);
}
return false;
}
}
public class Test8 {
public static void main(String[] args) {
Person p1 = new Person("John");
Person p2 = new Person("John");
System.out.println(p1 == p2); // Line A
System.out.println(p1.equals(p2)); // Line B
}
}
๐๏ธ
false
true๐ Line A: p1 == p2
==
๋ ๊ฐ์ฒด์ ์ฐธ์กฐ๊ฐ์ ๋น๊ตํด.- p1๊ณผ p2๋
new
ํค์๋๋ก ์๋ก ๋ค๋ฅธ ๊ฐ์ฒด๋ก ์์ฑ๋๊ธฐ ๋๋ฌธ์ ์ฐธ์กฐ๊ฐ ๋ค๋ฆ.- ๋ฐ๋ผ์ ๊ฒฐ๊ณผ๋: โ false
๐ Line B: p1.equals(p2)
equals()
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํ๊ธฐ ๋๋ฌธ์, ๋จ์ํ ์ฐธ์กฐ๊ฐ์ด ์๋ ๋ด์ฉ ๋น๊ต๋ฅผ ํด.this.name.equals(((Person) o).name)
โ ์ฆp1.name.equals(p2.name)
- ๋ ๋ค "John"์ด๋๊น ๊ฒฐ๊ณผ๋: โ true
class Base {
Base() {
print();
}
void print() {
System.out.println("Base");
}
}
class Derived extends Base {
int num = 10;
void print() {
System.out.println("Derived: " + num);
}
}
public class Test9 {
public static void main(String[] args) {
new Derived();
}
}
๐๏ธ Derived: 10
ํด์ค:Derived
์์ฑ์ ํธ์ถ ์,Base
์์ฑ์ ๋ด๋ถ์์print()
ํธ์ถ
โDerived.print()
์ค๋ฒ๋ผ์ด๋ฉ๋จ โnum
์ ์ด๋ฏธ ์ด๊ธฐํ๋ ์ํ โ 10
class One {
final void show() {
System.out.println("One");
}
static void greet() {
System.out.println("Static One");
}
}
class Two extends One {
// void show() {} // ์ปดํ์ผ ์๋ฌ: final ๋ฉ์๋๋ ์ค๋ฒ๋ผ์ด๋ฉ ๋ถ๊ฐ
static void greet() {
System.out.println("Static Two");
}
}
public class Test10 {
public static void main(String[] args) {
One o = new Two();
o.show();
o.greet();
}
}
๐๏ธ
One
Static One
ํด์ค:
show()
๋final
โ ์ค๋ฒ๋ผ์ด๋ฉ ๋ถ๊ฐ โOne
์ ๋ฉ์๋ ์คํ๋จ (O)greet()
๋static
โ ์ฐธ์กฐ ํ์ ๊ธฐ์ค์ผ๋ก ํธ์ถ โOne o = new Two();
โOne.greet()
์คํ๋จ โ โ Static One์ด ๋ง๋ ์ถ๋ ฅ