Shape Polymorphism with Interface

ShapePolymorphism-Interface

Shape 추상클래스 has-a ShapeType, ShapeColor, ShapeRect

Shape 추상클래스를 상속받은 Triangle, Rectangle, Square, Circle 클래스는 추상메소드인 void area()를 반드시 구현하였다.

Shape 추상클래스는 Moveable, Scalable 인터페이스를 상속하여 추상메소드인 Moveable 인터페이스의 void move(int, int)와 Scalable 인터페이스의 void scale(int)를 구현하였다.

Lab5

Lab5 – ImageProcessorTestOOP (Due by 5/14)
본인이 작성한 Lab4를 OOP 방식으로 바꾼다.

Lab5_template

java1-lab5-ImageProcessorTestOOP

Lab5 프로젝트 디렉토리 안에 모든 파일(src/*.java & bin/*.class)와 보고서(3-4장정도 장수제한없음)를 넣고 Lab5_학번_이름.zip 압축한 후 e-learning (http://lms.dankook.ac.kr/index.jsp)으로 제출

ConvertMode는 TO_JPG, TO_PNG, TO_GIF 열거형 제공
ImageMode는 CONVERT, RESIZE, ROTATE, GRAYSCALE, BLUR, BRIGHTNESS_ADJUST, NEGATIVE, EDGE_DETECT 열거형 제공
UserInput 클래스는 getString, getInteger, getIntegerBetween, getImageMode, getConvertMode, getExitKey 메소드 제공
Photo 클래스는 이미지파일을 읽어서 이미지 버퍼(BufferedImage)로 저장

-ImageProcessor 클래스를 상속받은 ImageBlur, ImageBrightnessAdjust, ImageConvert, ImageGrayscale, ImageResize, ImageRoate 클래스를 작성한다.
-ImageProcessorTestOOP 클래스에서는 ImageBlur, ImageBrightnessAdjust 등 클래스를 이용하여 각종 이미지 변환을 수행
-본인이 원하는 코드를 추가작성한다 (ImageNegative, ImageEdgeDetect, 등).

보고서의 내용은 기존 코드 분석과 이해한 내용 그리고 본인이 추가한 코드내용을 적는다.

Equals & Contains

import java.util.*;

class Person implements Comparable<Person> {
    private static int count = 0; // static (class) field
    protected String name; // instance field
    protected int age; // instance field

    public Person() {
        //System.out.println("Person Constructor"); // this("", 0); error: call to this must be first statemenht in constructor
        this("", 0);
    }

    public Person(String name, int age) {
        count++;
        this.name = name;
        this.age = age;
    }

    public Person(Person other) {
        this(other.name, other.age);
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void set(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void set(Person other) {
        this.name = other.name;
        this.age = other.age;
    }

    public Person clone() {
        Person p = new Person(this.name, this.age);
        return p;
    }

    @Override
    public boolean equals(Object other) { // Object.equals overriding
        if (other instanceof Person) {
            Person that = (Person) other;
            return that.canEqual(this) && this.getName().equals(that.getName()) && this.getAge() == that.getAge();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return (41 * getName().hashCode() + getAge());
    }

    public boolean canEqual(Object other) {
        return (other instanceof Person);
    }

    @Override
    public String toString() { // Object.toString() overriding
        return "Person Name: " + name + " Age: " + age;
    }

    public void print() { // instance methods
        System.out.println("Person Name: " + name + " Age: " + age);
    }

    public static void printCount() { // static (class) methods
        System.out.println("Person Count: " + count);
    }

    public static int getCount() {  // static (class) methods
        return count; 
    }

    public static void setCount(int value) { // static (class) methods
        count = value; 
    } 

    public int compareTo(Person other) {
        String thisName = this.getName().toUpperCase();
        String otherName = ((Person)other).getName().toUpperCase();
        //ascending order
        return thisName.compareTo(otherName);
        //descending order
        //return otherName.compareTo(thisName);
    }

    public static Comparator<Person> AgeComparator = new Comparator<Person>() {
        public int compare(Person p1, Person p2) {
            int age1 = p1.getAge();
            int age2 = p2.getAge();
            //ascending order
            return age1 – age2;
            //descending order
            //return age2 – age1;
        }
    };
}

///////////////////////////////////////////////////////////////////////////////////////
class Student extends Person {
    private static int count = 0; // static (class) field
    protected int id;

    public Student() {
        id = 5208;
    }

    public Student(String name, int age, int id) {
        super(name, age);
        this.id = id;
        count++;
    }

    public int getID() {
        return id;
    }

    public void setID(int id) {
        this.id = id;
    }

    public void set(String name, int age, int id) {
        super.set(name, age);
        this.id = id;
    }

    public void set(String name, int age) {
        super.set(name, age);
    }

    public void set(Student other) {
        this.set(other.name, other.age, other.id);
    }

    public void set(Person other) {
        if (other instanceof Person)
            super.set(other);
        else
            this.set((Student)other);
    }

    public Student clone() {
        Student s = new Student(this.name, this.age, this.id);
        return s;
    }

    @Override
    public boolean equals(Object other) { // Object.equals overriding
        if (other instanceof Student) {
            Student that = (Student) other;
            return that.canEqual(this) && this.getName().equals(that.getName()) && this.getAge() == that.getAge() && this.getID() == that.getID();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return (41 * super.hashCode() + getID());
    }

    public boolean canEqual(Object other) {
        return (other instanceof Student);
    }

    @Override
    public String toString() { // Object.toString() overriding
        return "Student Name: " + name + " Age: " + age + " ID: " + id;
    }

    public void superPrint() {
        super.print();
    }

    public void print() { // Person.print() method overriding
        System.out.println("Student Name: " + name + " Age: " + age + " ID: " + id);
    }

    public static void printCount() { // static (class) methods
        System.out.println("Student Count: " + count);
    }

    public static int getCount() { // static (class) methods
        return count; 
    } 
    public static void setCount(int value) { // static (class) methods
        count = value; 
    }

    public int compareTo(Student other) {
        String thisName = this.getName().toUpperCase();
        String otherName = ((Student)other).getName().toUpperCase();
        //ascending order
        return thisName.compareTo(otherName);
        //descending order
        //return otherName.compareTo(thisName);
    }

    public static Comparator<Student> AgeComparator = new Comparator<Student>() {
        public int compare(Student p1, Student p2) {
            int age1 = p1.getAge();
            int age2 = p2.getAge();
            //ascending order
            return age1 – age2;
            //descending order
            //return age2 – age1;
        }
    };

    public static Comparator<Student> IDComparator = new Comparator<Student>() {
        public int compare(Student p1, Student p2) {
            int id1 = p1.getID();
            int id2 = p2.getID();
            //ascending order
            return id1 – id2;
            //descending order
            //return id2 – id1;
        }
    };
}

class PersonStudentTest {

    public static void print(Object[] array) {
        for(Object o : array) {
            System.out.println(o);
        }
    }

    public static void main(String[] args) {
///////////////////////////////////////////////////////////////////////
        Person jason1 = new Person("Jason", 10);
        Person jason2 = new Person("Jason", 10);
        Person jason3 = jason1;
        Person jason4 = new Person("Jason", 20);

        if (jason1 == jason2)
            System.out.println("jason1 == jason2");
        else
            System.out.println("jason1 != jason2"); // 동일한 reference를 가리키지 않으므로 jason1 != jason2

        if (jason1 == jason3)
            System.out.println("jason1 == jason3"); // 동일한 reference이므로 jason1 == jason3
        else
            System.out.println("jason1 != jason3");

        if (jason1.equals(jason4))
            System.out.println("jason1 == jason4");
        else
            System.out.println("jason1 != jason4"); // 동일한 reference를 가리키지 않으므로 jason1 != jason4

        if (jason1.equals(jason2))
            System.out.println("jason1 equals to jason2"); // 내용이 같으므로 jason1 equals to jason2
        else
            System.out.println("jason1 is not equal to jason2");

        if (jason1.equals(jason3))
            System.out.println("jason1 equals to jason3"); // 내용이 같으므로 jason1 equals to jason3
        else
            System.out.println("jason1 is not equal to jason3");

        if (jason1.equals(jason4))
            System.out.println("jason1 equals jason4");
        else
            System.out.println("jason1 is not equal to jason4"); // 내용이 다르므로 jason1 is not equal to jason4

///////////////////////////////////////////////////////////////////////
        Student john1 = new Student("John", 10, 100);
        Student john2 = new Student("John", 10, 100);
        Student john3 = john1;
        Student john4 = new Student("John", 20, 100);

        if (john1.equals(john2))
            System.out.println("john1 equals to john2"); // 내용이 같으므로 john1 equals to john2
        else
            System.out.println("john1 is not equal to john2");

        if (john1.equals(john3))
            System.out.println("john1 equals to john3"); // 내용이 같으므로 john1 equals to john3
        else
            System.out.println("john1 is not equal to john3");

        if (john1.equals(john4))
            System.out.println("john1 equals to john4");
        else
            System.out.println("john1 is not equal to john4"); // 내용이 다르므로 john1 is not equal to john4

///////////////////////////////////////////////////////////////////////
        Student[] sList = new Student[3];
        sList[0] = new Student("Kevin", 0, 222);
        sList[1] = new Student("Jason", 1, 333);
        sList[2] = new Student("John", 2, 111);

        System.out.println("STUDENT SORT BY NAME (DEFAULT)!!!");
        Arrays.sort(sList);
        print(sList);

        System.out.println("STUDENT SORT by AGE!!!");
        Arrays.sort(sList, Student.AgeComparator);
        print(sList);

        System.out.println("STUDENT SORT by ID!!!");
        Arrays.sort(sList, Student.IDComparator);
        print(sList);

///////////////////////////////////////////////////////////////////////
        Student[] sList2 = new Student[3];
        sList2[0] = new Student("Kevin", 0, 222);
        sList2[1] = new Student("Jason", 1, 333);
        sList2[2] = new Student("John", 2, 111);

        System.out.println("STUDENT SORT BY NAME (anonymous method)!!!");
        Arrays.sort(sList2, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getName().toUpperCase().compareTo(s2.getName().toUpperCase());
            }
        });
        print(sList2);

        System.out.println("STUDENT SORT by AGE (anonymous method)!!!");
        Arrays.sort(sList2, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getAge() – s2.getAge();
            }
        });
        print(sList2);

        System.out.println("STUDENT SORT by ID (anonymous method)!!!");
        Arrays.sort(sList2, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getID() – s2.getID();
            }
        });
        print(sList2);

///////////////////////////////////////////////////////////////////////
        Student[] sList3 = sList2;

        System.out.println("STUDENT SORT BY NAME (lambda)!!!");
        Arrays.sort(sList3, (Student ss1, Student ss2) ->
                            ss1.getName().compareTo(ss2.getName())
        );
        Arrays.stream(sList3).forEach((s) -> System.out.println(s));

        System.out.println("STUDENT SORT by AGE (lambda)!!!");
        Arrays.sort(sList3, (Student ss1, Student ss2) ->
                            Integer.compare(ss1.getAge(), ss2.getAge())
        );
        Arrays.stream(sList3).forEach((s) -> System.out.println(s));

        System.out.println("STUDENT SORT by ID (lambda)!!!");
        Arrays.sort(sList3, (Student ss1, Student ss2) ->
                            Integer.compare(ss1.getID(), ss2.getID())
        );
        Arrays.stream(sList3).forEach((s) -> System.out.println(s));

///////////////////////////////////////////////////////////////////////
        List<Student> sList4 = new ArrayList<Student>();
        sList4.add(new Student("Kevin", 0, 222));
        sList4.add(new Student("Jason", 1, 333));
        sList4.add(new Student("John", 2, 111));

        System.out.println("STUDENTLIST SORT BY NAME (lambda)!!!");
        sList4.sort((Student ss1, Student ss2) ->
                    ss1.getName().compareTo(ss2.getName())
        );
        sList4.forEach((s) -> System.out.println(s));

        System.out.println("STUDENTLIST SORT by AGE (lambda)!!!");
        sList4.sort((Student ss1, Student ss2) ->
                    ss1.getAge() – ss2.getAge()
        );
        sList4.forEach((s) -> System.out.println(s));

        System.out.println("STUDENTLIST SORT by ID (lambda)!!!");
        sList4.sort((Student ss1, Student ss2) ->
                    ss1.getID() – ss2.getID()
        );
        sList4.forEach((s) -> System.out.println(s));
    }

}

Comparable & Comparator Interface

import java.util.*;

class Person implements Comparable<Person> {
    private static int count = 0; // static (class) field
    protected String name; // instance field
    protected int age; // instance field

    public Person() {
        //System.out.println("Person Constructor"); // this("", 0); error: call to this must be first statemenht in constructor
        this("", 0);
    }

    public Person(String name, int age) {
        count++;
        this.name = name;
        this.age = age;
    }

    public Person(Person other) {
        this(other.name, other.age);
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void set(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void set(Person other) {
        this.name = other.name;
        this.age = other.age;
    }

    public Person clone() {
        Person p = new Person(this.name, this.age);
        return p;
    }

    @Override
    public boolean equals(Object other) { // Object.equals overriding
        if (other instanceof Person) {
            Person that = (Person) other;
            return that.canEqual(this) && this.getName().equals(that.getName()) && this.getAge() == that.getAge();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return (41 * getName().hashCode() + getAge());
    }

    public boolean canEqual(Object other) {
        return (other instanceof Person);
    }

    @Override
    public String toString() { // Object.toString() overriding
        return "Person Name: " + name + " Age: " + age;
    }

    public void print() { // instance methods
        System.out.println("Person Name: " + name + " Age: " + age);
    }

    public static void printCount() { // static (class) methods
        System.out.println("Person Count: " + count);
    }

    public static int getCount() {  // static (class) methods
        return count; 
    }

    public static void setCount(int value) { // static (class) methods
        count = value; 
    } 

    public int compareTo(Person other) {
        String thisName = this.getName().toUpperCase();
        String otherName = ((Person)other).getName().toUpperCase();
        //ascending order
        return thisName.compareTo(otherName);
        //descending order
        //return otherName.compareTo(thisName);
    }

    public static Comparator<Person> AgeComparator = new Comparator<Person>() {
        public int compare(Person p1, Person p2) {
            int age1 = p1.getAge();
            int age2 = p2.getAge();
            //ascending order
            return age1 – age2;
            //descending order
            //return age2 – age1;
        }
    };
}

///////////////////////////////////////////////////////////////////////////////////////
class Student extends Person {
    private static int count = 0; // static (class) field
    protected int id;

    public Student() {
        id = 5208;
    }

    public Student(String name, int age, int id) {
        super(name, age);
        this.id = id;
        count++;
    }

    public int getID() {
        return id;
    }

    public void setID(int id) {
        this.id = id;
    }

    public void set(String name, int age, int id) {
        super.set(name, age);
        this.id = id;
    }

    public void set(String name, int age) {
        super.set(name, age);
    }

    public void set(Student other) {
        this.set(other.name, other.age, other.id);
    }

    public void set(Person other) {
        if (other instanceof Person)
            super.set(other);
        else
            this.set((Student)other);
    }

    public Student clone() {
        Student s = new Student(this.name, this.age, this.id);
        return s;
    }

    @Override
    public boolean equals(Object other) { // Object.equals overriding
        if (other instanceof Student) {
            Student that = (Student) other;
            return that.canEqual(this) && this.getName().equals(that.getName()) && this.getAge() == that.getAge() && this.getID() == that.getID();
        }
        return false;
    }

    @Override
    public int hashCode() {
        return (41 * super.hashCode() + getID());
    }

    public boolean canEqual(Object other) {
        return (other instanceof Student);
    }

    @Override
    public String toString() { // Object.toString() overriding
        return "Student Name: " + name + " Age: " + age + " ID: " + id;
    }

    public void superPrint() {
        super.print();
    }

    public void print() { // Person.print() method overriding
        System.out.println("Student Name: " + name + " Age: " + age + " ID: " + id);
    }

    public static void printCount() { // static (class) methods
        System.out.println("Student Count: " + count);
    }

    public static int getCount() { // static (class) methods
        return count; 
    } 
    public static void setCount(int value) { // static (class) methods
        count = value; 
    }

    public int compareTo(Student other) {
        String thisName = this.getName().toUpperCase();
        String otherName = ((Student)other).getName().toUpperCase();
        //ascending order
        return thisName.compareTo(otherName);
        //descending order
        //return otherName.compareTo(thisName);
    }

    public static Comparator<Student> AgeComparator = new Comparator<Student>() {
        public int compare(Student p1, Student p2) {
            int age1 = p1.getAge();
            int age2 = p2.getAge();
            //ascending order
            return age1 – age2;
            //descending order
            //return age2 – age1;
        }
    };

    public static Comparator<Student> IDComparator = new Comparator<Student>() {
        public int compare(Student p1, Student p2) {
            int id1 = p1.getID();
            int id2 = p2.getID();
            //ascending order
            return id1 – id2;
            //descending order
            //return id2 – id1;
        }
    };
}

class PersonStudentTest {

    public static void print(Object[] array) {
        for(Object o : array) {
            System.out.println(o);
        }
    }

    public static void main(String[] args) {
///////////////////////////////////////////////////////////////////////
        Student[] sList = new Student[3];
        sList[0] = new Student("Kevin", 0, 222);
        sList[1] = new Student("Jason", 1, 333);
        sList[2] = new Student("John", 2, 111);

        System.out.println("STUDENT SORT BY NAME (DEFAULT)!!!");
        Arrays.sort(sList);
        print(sList);

        System.out.println("STUDENT SORT by AGE!!!");
        Arrays.sort(sList, Student.AgeComparator);
        print(sList);

        System.out.println("STUDENT SORT by ID!!!");
        Arrays.sort(sList, Student.IDComparator);
        print(sList);

///////////////////////////////////////////////////////////////////////
        Student[] sList2 = new Student[3];
        sList2[0] = new Student("Kevin", 0, 222);
        sList2[1] = new Student("Jason", 1, 333);
        sList2[2] = new Student("John", 2, 111);

        System.out.println("STUDENT SORT BY NAME (anonymous method)!!!");
        Arrays.sort(sList2, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getName().toUpperCase().compareTo(s2.getName().toUpperCase());
            }
        });
        print(sList2);

        System.out.println("STUDENT SORT by AGE (anonymous method)!!!");
        Arrays.sort(sList2, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getAge() – s2.getAge();
            }
        });
        print(sList2);

        System.out.println("STUDENT SORT by ID (anonymous method)!!!");
        Arrays.sort(sList2, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getID() – s2.getID();
            }
        });
        print(sList2);

///////////////////////////////////////////////////////////////////////
        Student[] sList3 = sList2;

        System.out.println("STUDENT SORT BY NAME (lambda)!!!");
        Arrays.sort(sList3, (Student ss1, Student ss2) ->
                            ss1.getName().compareTo(ss2.getName())
        );
        Arrays.stream(sList3).forEach((s) -> System.out.println(s));

        System.out.println("STUDENT SORT by AGE (lambda)!!!");
        Arrays.sort(sList3, (Student ss1, Student ss2) ->
                            Integer.compare(ss1.getAge(), ss2.getAge())
        );
        Arrays.stream(sList3).forEach((s) -> System.out.println(s));

        System.out.println("STUDENT SORT by ID (lambda)!!!");
        Arrays.sort(sList3, (Student ss1, Student ss2) ->
                            Integer.compare(ss1.getID(), ss2.getID())
        );
        Arrays.stream(sList3).forEach((s) -> System.out.println(s));

///////////////////////////////////////////////////////////////////////
        List<Student> sList4 = new ArrayList<Student>();
        sList4.add(new Student("Kevin", 0, 222));
        sList4.add(new Student("Jason", 1, 333));
        sList4.add(new Student("John", 2, 111));

        System.out.println("STUDENTLIST SORT BY NAME (lambda)!!!");
        sList4.sort((Student ss1, Student ss2) ->
                    ss1.getName().compareTo(ss2.getName())
        );
        sList4.forEach((s) -> System.out.println(s));

        System.out.println("STUDENTLIST SORT by AGE (lambda)!!!");
        sList4.sort((Student ss1, Student ss2) ->
                    ss1.getAge() – ss2.getAge()
        );
        sList4.forEach((s) -> System.out.println(s));

        System.out.println("STUDENTLIST SORT by ID (lambda)!!!");
        sList4.sort((Student ss1, Student ss2) ->
                    ss1.getID() – ss2.getID()
        );
        sList4.forEach((s) -> System.out.println(s));
    }

}

Abstract Class, Polymorphism

Abstract Class (추상 클래스)는 반드시 하나 이상의 abstract method(추상메소드)를 가지며, 객체를 생성할 수 없다.

Abstract Method (추상 메소드)는 구현 내용이 없는 메소드이다.

추상클래스를 상속받은 하위클래스는 반드시 추상 메소드를 재정의(method override) 구현해야한다.

Polymorphism (다형성)은 프로그램 실행(run-time execution)시 형(type)이 변화되는 객체.

다형성에 따라서, 클래스 상속관계(Inheritance)에서 메소드 재정의(Method overriding)된 것이 동적 바인딩(dynamic binding)으로 호출되었을 시,  해당 객체에 맞는 재정의된 메소드가 호출되는 것이다.

 

ShapePolymorphism

Shape 추상클래스 has-a ShapeType, ShapeColor, ShapeRect

Shape 추상클래스를 상속받은 Triangle,  Rectangle, Square, Circle 클래스는 추상메소드인 area()를 반드시 구현해야한다.

https://www.cs.usfca.edu/~wolber/SoftwareDev/OO/abstractInterfacesIntro.htm

Upcasting Downcasting Dynamic Binding

Upcasting: 하위 클래스의 객체를 상위 클래스의 객체로 assign하여 변환하는 것

Downcasting: 상위 클래스의 객체를 하위 클래스의 객체로 assign하여 변환하는 것

Dynamic Binding (동적 바인딩):  Dynamic binding is also known as late binding or run-time binding. 동적 바인딩이란 실행시 해당 객체에 맞는 오버라이딩된 메소드가 호출되는 것

// PersonStudentTest
class PersonStudentTest {
    static void print(Object o) {
        System.out.println(o);
    }
    static void print(Person[] arr) {
        for (Person p : arr) System.out.println(p);
    }

    public static void main(String[] args) {
        Person h1 = new Person("JAVA", 2018);
        h1.print();
        print(h1);
        System.out.println(h1);

        Person h2 = new Student(); // upcasting
        h2.print(); // dynamic binding Student.print()
        //print(h2);
        //System.out.println(h2);

        Object o = h2; // upcasting
        ((Student)o).print(); // downcasting o=> Student 
        ((Person)o).print(); // downcasting o=> Person => Person.print() dynamic binding Student.print()

        Person[] pList = new Person[5];
        pList[0] = new Person("JAVA1", 1);
        pList[0].print();
        pList[1] = pList[0];
        pList[1].print();
        pList[2] = new Student(); // upcasting
        pList[2].print(); // dynamic binding Student.print()
        pList[3] = new Student("JAVA2", 2, 222); // upcasting
        pList[3].print(); // dynamic binding Student.print()
        pList[4] = pList[3];
        pList[4].print(); // dynamic binding Student.print()

        System.out.println("AFTER SET");
        pList[0].set("JAVA3", 3); // Person.set(string, int)
        pList[4].set("JAVA4", 4); // Person.set(string, int)
        print(pList);

        System.out.println("AFTER SET2");
        Student s = (Student)pList[4]; // downcasting
        s.set("JAVA5", 5, 555); // Student.set(string, int, int)
        print(pList);

        System.out.println("AFTER SET3");
        pList[0].set(pList[4]); // Person.set(Person)
        print(pList);

        System.out.println("AFTER SET4");
        ((Student)pList[2]).set(pList[4]); // downcasting Student.set(Person)
        //((Student)pList[2]).set((Student)pList[4]); // Student.set(Student)
        print(pList);

        System.out.println("AFTER SET5");
        ((Student)pList[2]).set(new Person("JAVA6", 6)); // downcasting Student.set(Person)
        //((Student)pList[2]).set((Student)pList[4]); // Student.set(Student)
        print(pList);

        Person.printCount(); // calls Person printCount() static method
        Student.printCount(); // calls Student printCount() static method
        System.out.println();
    }
}

Instance vs Static Method Overriding

PersonStudent-MethodOverriding2

/// Person
class Person {
    private static int count = 0; // static (class) field
    protected String name; // instance field
    protected int age; // instance field

    public Person() {
        //System.out.println("Person Constructor"); // this("", 0); error: call to this must be first statemenht in constructor
        this("", 0);
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        count++;
    }

    public static void printCount() { // static (class) method
        System.out.println("Person Count: " + count);
    }

    public static int getCount() {  // static (class) method
        return count; 
    }

    public String toString() { // Object.toString() overriding
        return "Person Name: " + name + " Age: " + age;
    }

    public void print() { // instance method
        System.out.println("Person Name: " + name + " Age: " + age);
    }
}

/// Student
class Student extends Person {
    private static int scount = 0; // static (class) variables
    protected int id;

    public Student() {
        this("", 0, 5208);
    }

    public Student(String name, int age, int id) {
        super(name, age);
        this.id = id;
        scount++;
    }

    public static void printCount() { // static (class) method overriding
        System.out.println("Student Count: " + scount);
    }

    public String toString() { // Object.toString() overriding
        return "Student Name: " + name + " Age: " + age + " ID: " + id;
    }

    public void print() { // Person.print() method overriding
        System.out.println("Student Name: " + name + " Age: " + age + " ID: " + id);
    }
}

class PersonStudentTest {
    public static void main(String[] args) {
        Person p1 = new Student("K", 20, 2018); // upcasting 1번째 Student 객체 생성
        p1.print(); // dynamic binding Student Name: K Age: 20 ID: 2018
        p1.name = "JAVA";
        p1.age = 1;
        //p1.id = 2017; // cannnot call id because p1 is Person type
        System.out.println(p1); // dynamic binding p1.toString() Student Name: JAVA Age: 1 ID: 1208
        p1.printCount(); // p1 is Person, Person.printCount() Person.count=1
        Person.printCount(); // Person.printCount() Person.count=1
        Student.printCount(); // Student.printCount() Student.scount=1
        System.out.println();

        Student s1 = (Student)p1; // downcasting
        s1.name = "JAVA2"; 
        s1.age = 2;
        s1.id = 2017; // can call id, because s1 is Student type
        s1.print(); // Student Name: JAVA2 Age: 2 ID: 2017
        s1.printCount(); // s1 is Student, Student.printCount() scount=1
        Person.printCount(); // Person.printCount() count=1
        Student.printCount(); // Student.printCount() scount=1
        System.out.println();

        Student s2 = new Student("S", 30, 1207); // 2번째 Student 객체 생성
        s2.print(); // Student Name: S Age: 30 ID: 1217
        s2.printCount(); // s2 is Student, Student.printCount() scount=2
        Person.printCount(); // Person.printCount() count=2
        Student.printCount(); // Student.printCount() count=2
        System.out.println();

        Person p2 = new Person("Park", 1); // 1번째 Person 객체 생성
        System.out.println(p2); // Person Name: Park Age: 1
        p2.printCount(); // p2 is Person, Person.printCount() count=3
        Person.printCount(); // Person.printCount() count=3
        Student.printCount(); // Student.printCount() scount=2
        System.out.println();

        Person p3 = new Person(); // 2번째 Person 객체 생성
        System.out.println(p3); // Person Name: Age: 0
        p3.printCount(); // p3 is Person, Person.printCount() count=4
        Person.printCount(); // Person.printCount() count=4
        Student.printCount(); // Student.printCount() scount=2
        System.out.println();

        System.out.println("Number of Person: " + Person.getCount()); // Person.getCount() count=4
    }
}

Method Overloading vs Overriding

http://www.programmerinterview.com/index.php/java-questions/method-overriding-vs-overloading/

Method Overloading: 동일한 함수명에 매개변수가 다른 함수를 둘 이상 정의하는 것으로, 동일한 함수 기능을 수행하지만 다른 매개변수의 경우를 처리할 때 사용

//compiler error – can’t overload based on the
//type returned (one method returns int, the other returns a float):
//int changeDate(int Year) ;
//float changeDate (int Year);

//compiler error – can’t overload by changing just
//the name of the parameter (from Year to Month):
//int changeDate(int Year);
//int changeDate(int Month) ;

//valid case of overloading,
//since the methods have different number of parameters:
int changeDate(int Year, int Month) ;
int changeDate(int Year);

//also a valid case of overloading,
//since the parameters are of different types:
int changeDate(float Year) ;
int changeDate(int Year);

Method Overriding: 상속받은 파생 클래스에서 동일한 함수명에 동일한 매개변수로 정의하여 함수를 재정의하는 것으로 상속되어진 함수의 기능을 변경해서 재사용하고 싶을 때 사용

public class Parent {
    public int someMethod() {
        return 3;
    }
}

public class Child extends Parent{

    // this is method overriding:
    public int someMethod() {
        return 4;
    }
}