상속성(inheritance)
다형성(polymorphism)
package sample29_Polymorphism;
public class MainClass {
public static void main(String[] args) {
Parent pobj = new Child();
pobj.method();
Child c = new Child();
}
}
class Parent{
protected String name;
public Parent() {
System.out.println("Parent Parent()");
}
public Parent(String name) {
System.out.println("Parent Parent(String name)");
}
public void pMethod() {
System.out.println("Parent pMethod()");
}
public void method() {
System.out.println("Parent method()");
}
}
class Child extends Parent{
public Child() {
super("성춘향");
System.out.println("Child Child()");
}
public void func() {
name = "홍길동";
pMethod();
}
public void method() {
System.out.println("Child method()");
}
public void process() {
super.method();
this.method();
}
}
Overriding
package sample30;
public class MainClass {
public static void main(String[] args) {
ChildOne pitcher[] = new ChildOne[10];
ChildTwo batter[] = new ChildTwo[10];
Parent parent[] = new Parent[5];
parent[0] = new ChildOne();
parent[1] = new ChildTwo();
parent[2] = new ChildTwo();
parent[3] = new ChildTwo();
parent[4] = new ChildOne();
for (int i = 0; i < parent.length; i++) {
parent[i].method();
}
ChildOne one = (ChildOne)parent[0];
one.func();
if(parent[0] instanceof ChildOne) {
System.out.println("parent[0]는 ChildOne으로 생성되었습니다.");
}
for (int i = 0; i < parent.length; i++) {
if (parent[i] instanceof ChildOne) {
System.out.println("parent["+ i + "]는 ChildOne으로 생성되었습니다.");
}
else if (parent[i] instanceof ChildTwo) {
System.out.println("parent["+ i + "]는 ChildTwo으로 생성되었습니다.");
}
}
}
}
class Parent{
public void method() {
System.out.println("Parent method()");
}
}
class ChildOne extends Parent{
public void method() {
System.out.println("ChildOne method()");
}
public void func() {
System.out.println("Parent func()");
}
}
class ChildTwo extends Parent{
public void method() {
System.out.println("ChildTwo method()");
}
public void process() {
System.out.println("Parent process()");
}
}
super
Main Class
package sample31;
import cls.Batter;
import cls.Human;
import cls.Pitcher;
public class MainClass {
public static void main(String[] args) {
Human humanArr[] = new Human[5];
humanArr[0] = new Pitcher("성춘향", 16, 10, 2);
humanArr[1] = new Pitcher("일지매", 22, 6, 3);
humanArr[2] = new Batter("홍두께", 24, 13, 4);
humanArr[3] = new Batter("홍길동", 25, 21, 6);
humanArr[4] = new Pitcher("이몽룡", 17, 9, 1);
for (int i = 0; i < humanArr.length; i++) {
System.out.println(humanArr[i].toString());
}
for (int i = 0; i < humanArr.length; i++) {
if(humanArr[i] instanceof Pitcher) {
Pitcher p =(Pitcher)humanArr[i];
System.out.println(p.getName() + " " + p.getWin() + "승 " + p.getLose() + "패");
}
else if(humanArr[i] instanceof Batter) {
Batter b = (Batter)humanArr[i];
System.out.println(b.getName() + " " +b.getBatCount() + "타수" + b.getHit() + "안타");
}
}
Object h1 = new Pitcher();
Object h2 = new Batter();
Object objArr = new ThreeClass();
NumberInter arrObj = new ThreeClass();
}
}
interface NumberInter{
}
class OneClass implements NumberInter{
}
class TwoClass implements NumberInter{
}
class ThreeClass implements NumberInter{
}
Batter
package cls;
public class Batter extends Human{
int batCount;
int hit;
public Batter() {
}
public Batter(String name, int age, int batCount, int hit) {
super(name, age);
this.batCount = batCount;
this.hit = hit;
}
public int getBatCount() {
return batCount;
}
public void setBatCount(int batCount) {
this.batCount = batCount;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
@Override
public String toString() {
return "Batter [" + super.toString() + ", batCount=" + batCount + ", hit=" + hit + "]";
}
}
Human
package cls;
public class Human {
private String name;
private int age;
public Human() {
}
public Human(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "name=" + name + ", age=" + age;
}
}
Pitcher
package cls;
public class Pitcher extends Human{
private int win;
private int lose;
public Pitcher() {
}
public Pitcher(String name, int age, int win, int lose) {
super(name, age);
this.win = win;
this.lose = lose;
}
public int getWin() {
return win;
}
public void setWin(int win) {
this.win = win;
}
public int getLose() {
return lose;
}
public void setLose(int lose) {
this.lose = lose;
}
@Override
public String toString() {
return "Pitcher [" + super.toString() + ", win=" + win + ", lose=" + lose + "]";
}
}
Abstract Class(추상 클래스)
package sample32;
public class MainClass {
public static void main(String[] args) {
Child ch = new Child();
ch.normalmethod();
ch.abstractMethod();
AbstractClass acls = new Child();
acls.normalmethod();
acls.abstractMethod();
AbstractClass bcls = new AbstractClass() {
@Override
public void abstractMethod() {
System.out.println("ChildClass abstractMethod()");
}
};
bcls.normalmethod();
bcls.abstractMethod();
}
}
abstract class AbstractClass {
private String name;
public void normalmethod() {
System.out.println("AbstractClass normalMethod()");
}
public abstract void abstractMethod();
}
class Child extends AbstractClass{
@Override
public void abstractMethod() {
System.out.println("ChildClass abstractMethod()");
}
}
final
package sample34_final;
public class MainClass {
public static void main(String[] args) {
final int NUMBER = 1;
int num = NUMBER;
}
}
class MyClass{
public void method() {
}
}
class YouClass extends MyClass{
@Override
public void method() {
method();
}
}
Interface
Main Class
package smaple33;
import cal.Calculator;
import cal.CalculatorImp1;
public class MainClass {
public static void main(String[] args) {
MyClass mycls = new MyClass();
mycls.func();
MyInterface myint = new MyClass();
myint.func();
MyInterface myif = new MyInterface() {
@Override
public void func() {
System.out.println("MyInterface func()");
}
};
myif.func();
Calculator cal = new CalculatorImp1();
cal.plus();
cal.minus();
CalculatorImp1 cali = new CalculatorImp1();
}
}
interface MyInterface{
public void func();
}
class MyClass implements MyInterface{
@Override
public void func() {
System.out.println("MyClass func()");
}
Calculator
package cal;
public interface Calculator {
public void plus();
public void minus();
}
Calculator Implement
package cal;
public class CalculatorImp1 implements Calculator {
@Override
public void plus() {
System.out.println("CalculatorImp1 plus()");
}
@Override
public void minus() {
System.out.println("CalculatorImp1 minus()");
}
}