전체 글
-
-
다형성, instanceof, 메서드 오버라이딩JAVA/개념 - 김영한 강의 2024. 10. 8. 08:02
다형성하나의 객체가 다른 타입으로 사용될 수 있다는 뜻이다.부모는 자식을 품을 수 있다. 직계 뿐만이 아니라 손자 등 모든 자식 타입을 참조할 수 있다. 이렇게 다양한 형태를 참조할 수 있다고 해서 다형적 참조라 한다.Parent poly = new Parent() Parent poly = new Child() Parent poly = new Grandson() 캐스팅업캐스팅(upcasting): 자식 -> 부모 타입으로 변경다운캐스팅(downcasting): 부모 -> 자식 타입으로 변경 다운캐스팅public class Parent { public void parentMethod() { System.out.println("Parent.parentMethod"); }}public..
-
왜 static 메서드는 오버라이딩 될 수 없을까?JAVA/궁금증 2024. 10. 2. 17:52
오버라이딩은 인스턴스 메서드에서 가능한데, static 메서드는 클래스에 속한 메서드니까. 1. 인스턴스 메서드인스턴스 메서드는 객체가 생성되어야만 사용할 수 있으며, 객체에 속한 데이터(속성)를 다룬다. 인스턴 메서드는 객체마다 다른 데이터를 사용할 수 있게 해준다. class MyClass { public void instanceMethod() { System.out.println("This is an instance method."); }}MyClass obj = new MyClass(); // 인스턴스 생성obj.instanceMethod(); // 인스턴스 메서드 호출 2. 클래스에 속한 static 메서드static 메서드는 클래스에 속해 있으며 객체를 생성하지 않..
-
상속(Inheritance), @Override, superJAVA/개념 - 김영한 강의 2024. 9. 27. 18:34
상속기존 클래스의 속성과 메서드를 새로운 클래스가 물려받아 사용할 수 있게 함(자식클래스명) extends (부모클래스명)부모 클래스: 상속을 통해 자신의 필드와 메서드를 다른 클래스에 제공하는 클래스자식 클래스: 부모 클래스로부터 필드와 메서드를 상속받는 클래스단 하나의 부모클래스만 상속받을 수 있다자식은 부모클래스에 접근할 수 있지만, 부모는 자식 클래스에 접근할 수 없다.(= 자식은 부모로부터 유산을 상속받지만 // 부모 클래스class Animal { void sound() { System.out.println("동물이 소리를 냅니다."); }}// 자식 클래스class Dog extends Animal { // 부모클래스의 메서드를 재정의(오버라이딩) @Override..
-
클래스 메서드, 인스턴스 메서드JAVA/개념 - 김영한 강의 2024. 9. 17. 08:39
1. 클래스 메서드 vs 인스턴스 메서드1) 인스턴스 메서드static이 붙지 않은 메서드. 인스턴스를 생성해야 호출할 수 있다.다음 인스턴스 메서드인 deco() 메서드를 보자.public class DecoUtil1 { public String deco(String str) { String result = "*" + str + "*"; return result; }}public class DecoMain1 { public static void main(String[] args) { String s = "hello java"; DecoUtil1 utils = new DecoUtil1(); String deco = utils.deco(s); System.out.pri..