Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both.

We can overload:

  • common method
  • static method
  • main() method

We cannot overload:

  • methods on return type
  • methods that differ only by static keyword


Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. In other words, it is the type of the object being referred to ((not the type of the reference variable)) that determines which version of an overridden method will be executed.

* Tips: 決定 override version 的調用規則 → 形態看左邊(父),運行看右邊(子)

  • Rules for method overriding:
    • subclass has the same name, same parameters or signature and same return type(or sub-type)
    • without override, it cannot invoke the unique method in subclass with the type which referred to parent class
    • the access modifier for an overriding can allow more, but not less, access than the overridden method
    • we can call parent class method in overriding method using super keyword
    • abstract methods in an interface or abstract class are meant to be overridden in derived concrete classes
  • Reason:
    • subclass has unique feature method
    • this is one of the way by which java achieve Run Time Polymorphism
  • Cannot be override:
    • private modifier
    • final methods
    • static methods
  • Exception handling: see override exception in java-except

See more information:

up casting

Upcasting is casting to a supertype, while downcasting is casting to subtype. Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException.


Animal animal = new Dog(); // this is upcasting
Dog dog = (Dog) animal; // this is downcasting


Animal animal = new Animal();
Cat cat = (Cat) animal; // ClassCastException


Objects of different kinds can adhere to the same interface or set of operations.


  • 事物在運行過程中存在不同的狀態。(使用一個父類,可以操作多個子類)
  • 具有 is-a 關係


  • 要有繼承關係
  • 子類要重寫父類的方法
  • 父類引用指向子类别


class Animal {
    int num = 10;
    static int age = 20;

    public void eat() {
        System.out.println("animal eat");

    public static void sleep() {
        System.out.println("animal sleep in static");

    public void run() {
        System.out.println("animal run");

class Cat extends Animal {
    int num = 80;
    static int age = 90;
    String name = "tomcat";

    public void eat() {
        System.out.println("cat eat");

    public static void sleep() {
        System.out.println("cat sleep in static");

    public void catchMouse() {
        System.out.println("cat catch mouse");

TestUnit Demo:

class Demo_Test1 {
    public static void main(String[] args){
        Animal animal = new Cat();

        // animal.catchMouse();
        // log.info(animal.name)



cat eat
animal sleep in static
animal run


  1. 存在继承关系: Cat类继承了Animal类
  2. 子类要重写父类的方法: 子类重写(override)了父类的两个成员方法eat(),sleep()。其中eat()是非静态的,sleep()是静态的(static)。
  3. 父类数据类型的引用指向子类对象: 测试类Demo_Test1中Animal animal = new Cat();语句在堆内存中开辟了子类(Cat)的对象,并把栈内存中的父类(Animal)的引用指向了这个Cat对象


  • 成员变量:编译看左边(父类),运行看左边(父类)。
  • 成员方法:编译看左边(父类),运行看右边(子类)。动态绑定
  • 静态方法:编译看左边(父类),运行看左边(父类)。 (静态和类相关,算不上重写,所以,访问还是左边的) 只有非静态的成员方法,编译看左边,运行看右边


解决方法:强制转型Cat cat = (Cat) animal;