목록상속 (3)
개발동
class Product{ int price; Product(int price){ this.price = price; } } class Tv extends Product{ Tv(){ super(100); } public String toString() { return "Tv"; } } class Computer extends Product{ Computer(){ super(200); } public String toString() { return "Computer"; } } class Audio extends Product{ Audio(){ super(50); } public String toString() { return "Audio"; } } class Buyer{ int money = 600; st..
부모클래스 타입의 참조변수가 자식 클래스의 인스턴스를 참조했을때, 부모&자식 내에 이름이 같은 멤버변수나 함수가 있을 경우 멤버함수의 경우 : 참조변수는 자식 인스턴스의 함수를 따라감 (Overriding 되어, 무조건 참조한 자식 인스턴스의 메서드로 적용됨) 멤버변수의 경우 : 참조변수의 데이터 타입을 따라감 (참조변수의 데이터 타입에 따라 달라짐) class Parent { int x = 100; void method() { System.out.println("Parent"); } } class Child extends Parent { int x = 200; void method() { System.out.println("Child"); } } public class BindingTest { publ..
상속이란, 기존 클래스의 코드를 재사용하여 새로운 클래스를 생성하는 것으로 자식클래스는 부모클래스의 멤버를 상속받는다. (초기화 블럭과 생성자는 상속 받지 아니함) ※상속이외에도 클래스간에 포함관계를 맺음으로써 클래스 재사용이 가능하다. class Point { int x, y; } class Circle0 extends Point { int r; } class Circle { Point p = new Point(); int r; } public class ExtendsEx { public static void main(String[] args) { // TODO Auto-generated method stub Circle c = new Circle(); c.p.x = 10; c.p.y = 10; c.r..