java method overriding and overloading

Method override:

    The subclass method can define new features. When the subclass modifies the method of the parent class to expand and increase the function, it needs to rewrite the method. The rewrite is based on the inheritance relationship, and the subclass can hide and access the parent class. The method of inheriting the parent class can also be overridden, and the method of overriding the inherited parent class is achieved by overriding the method.

    The so-called method overriding: the method in the subclass has exactly the same return value type, method name, number of parameters and parameter types as the inherited method in the superclass.

package javatest;

class Parent {
    public void print(){
        System.out.println("parent class");
    }
}

class Child extends Parent{
    public void print(){
        System.out.println("Child class"); //输出Child class
    }
}

public class TestOverwrite{
    public static void main(String args[]){
        Child c = new Child();
        c.print();
    }
}

Output result:

Child class


After the subclass overrides the method of the parent class, the method called using c must be the method of the subclass, and the method in the parent class is overridden. So, what should I do if I want to call the method of the parent class now?

This is achieved by using the super keyword.

package javatest;

class Parent {
    public void print(){
        System.out.println("parent class");
    }
}

class Child extends Parent{
    public void print(){
        super.print();   //输出parent class
        System.out.println("Child class"); //输出Child class
    }
}

public class TestOverwrite{
    public static void main(String args[]){
        Child c = new Child();
        c.print();
    }
}
输出结果:

parent class
Child class


To summarize the rewrite rules:

    1. The parameter list of the parent class method must be exactly the same as the parameter list of the method overridden by the subclass;

    2. The return type of the superclass method must be exactly the same as the return type of the method overridden by the subclass;

    3. It is stipulated in java that methods overridden by subclasses cannot have stricter access rights than methods of superclasses.

    父类中的方法并不是任何时候都可以进行重写的,当父类方法的访问权限修饰符为private时,该方法只能被自己的类访问,不能被外部的类访问,在子类是不能被重写的。如果定义父类的方法为public,在子类定义为private,程序运行时会报错。

    4、在继承中如果父类中的方法抛出异常, 那么在子类中重写该方法时,也要抛出异常,而且抛出的异常不能多余父类中抛出的异常,换句话说,重写方法,一定不能抛出新的检查异常,或者比被重写方法声明更加宽泛的检查型异常。例如,父类的一个方法申明了一个检查异常IOException,在重写这个方法时就不能抛出Exception,只能抛出IOException的子类异常,可以抛出非检查异常。


方法重载:

    方法重载是让类已统一的方式处理不同类型数据的一种手段,调用方法时,通过传递给它们不同个数和类型的参数来决定具体使用哪个方法,这就是多态

    所谓的方法重载,在一个类中方法的名称相同,但是参数列表不同。参数列表不同指的是参数个数,参数类型或者参数顺序不同。

   

package javatest;

class Person{
    String name;
    int age;
    void print(){
        System.out.println("姓名:" + name + "  年龄:" + age);
    }
    void print(String a,int b){
        System.out.println("姓名:" + a + "  年龄:" + b);
    }
    void print(String a,int b,int c){
        System.out.println("姓名:" + a + "  年龄:" + b + "  ID:" + c);
    }
}
public class TestOverload {
    public static void main(String args[]){
        Person p = new Person();
        p.name = "初始";
        p.age = 12;
        p.print();
        p.print("lucy", 23);
        p.print("toto", 33, 12890);
    }
}


输出结果:

姓名:初始  年龄:12
姓名:lucy  年龄:23
姓名:toto  年龄:33  ID:12890


    在方法重载时,方法之间需要存在一定的联系,因为这样可以提高程序的可读性,一般只重载功能相似的方法重载规则。重载是指我们可以定义一些名称相同的方法,通过定义不同的参数来区分这些方法,然后再调用时,Java虚拟机就会根
据不同的参数列表来选择合适的方法执行。也就是说,当一个重载方法被调用时,Java用参数的类型和.. (或)个数来决定实际调用的重载方法。因此,每个重载方法的参数的类型或个数必须是不同。虽然每个重载方法可以有不同的返回类型,
但返回类型并不足以区分所使用的是哪个方法。当Java调用一个重载方法是,参数与调用参数匹配的方法被执行。在使用重载要注意以下的几点:

1.在使用重载时只能通过不同的参数列表,必须具有不同的参数列表。例如,不同的参数类型,不同的参数个数,不同的参数顺序。当然,同一方法内的几个参数类型必须不一样,例如可以是 fun(int,float),但是不能为 fun(int,int)。
2.不能通过访问权限、返回类型、抛出的异常进行重载。
3.方法的异常类型和数目不会对重载造成影响。.. 
4.可以有不同的返回类型,只要参数列表不同就可以了。
5.可以有不同的访问修饰符。

6.可以抛出不同的异常。


区别点

重载

重写(覆写)

英文

Overloading

Overiding

定义

方法名称相同,参数的类型或个数不同

方法名称、参数类型、返回值类型全部相同

对权限没有要求

被重写的方法不能拥有更严格的权限

范围

发生在一个类中

发生在继承类中

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325562207&siteId=291194637