search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Java Override/Overload

Collection 231
Read 65460
update time 2016-09-11

Override

Override is the subclass's rewriting of the implementation process of the parent class's methods that allow access! Neither the return value nor the formal parameters can be changed. That is, the shell remains unchanged and the core is rewritten!

The advantage of overriding is that subclasses can define their own behavior as needed.

That is to say, the subclass can implement the methods of the parent class as needed.

In object-oriented principles, overriding means that any existing method can be overridden. The example is as follows:

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("狗可以跑和走");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象

      a.move();// 执行 Animal 类的方法

      b.move();//执行 Dog 类的方法
   }
}

The compilation and running results of the above example are as follows:

动物可以移动
狗可以跑和走

As you can see in the above example, although b belongs to the Animal type, it runs the move method of the Dog class.

This is because during the compilation phase, only the reference type of the parameter is checked.

However, at runtime, the Java Virtual Machine (JVM) specifies the type of object and runs the object's methods.

So in the above example, the reason why the compilation is successful is because the move method exists in the Animal class. However, at runtime, the method of the specific object is run.

Think about the following example:

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("狗可以跑和走");
   }
   public void bark(){
      System.out.println("狗可以吠叫");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象

      a.move();// 执行 Animal 类的方法
      b.move();//执行 Dog 类的方法
      b.bark();
   }
}

The compilation and running results of the above example are as follows:

TestDog.java:30: cannot find symbol
symbol  : method bark()
location: class Animal
                b.bark();
                 ^

This program will throw a compilation error because the reference type of b, Animal, does not have a bark method.


Method rewriting rules

  •             The parameter list must be exactly the same as that of the overridden method;

  •             The return type must be exactly the same as the return type of the overridden method;

  •             Access rights cannot be lower than the access rights of the overridden method in the parent class. For example: If a method of the parent class is declared as public, then the method cannot be declared as protected when overriding it in the subclass.

  •           Member methods of a parent class can only be overridden by its subclasses.

  •           Methods declared final cannot be overridden.

  •           Methods declared static cannot be overridden, but they can be declared again.

  •           If the subclass and the parent class are in the same package, the subclass can override all methods of the parent class, except methods declared as private and final.

  •           If the subclass and the parent class are not in the same package, the subclass can only override the non-final methods of the parent class that are declared public and protected.

  •           An overridden method can throw any unforced exception, regardless of whether the overridden method throws an exception. However, an overridden method cannot throw new mandatory exceptions, or mandatory exceptions that are broader than those declared by the overridden method, and vice versa.

  •           Constructors cannot be overridden.

  • If you cannot inherit a method, you cannot override this method.


Use of Super keyword

When you need to call the overridden method of the parent class in a subclass, use the super keyword.

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      super.move(); // 应用super类的方法
      System.out.println("狗可以跑和走");
   }
}

public class TestDog{

   public static void main(String args[]){

      Animal b = new Dog(); // Dog 对象
      b.move(); //执行 Dog类的方法

   }
}

The compilation and running results of the above example are as follows:

动物可以移动
狗可以跑和走

Overload(Overload)

Overloading (overloading) is in a class, the method name is the same, but the parameters are different . The return types can be the same or different.

Each overloaded method (or constructor) must have a unique parameter type list.

Only constructors can be overloaded

Overloading rules

  • The overloaded method must change the parameter list;

  • The overloaded method can change the return type;

  • The overloaded method can change the access modifier;

  • Overloaded methods can declare new or wider checked exceptions;

  • methods can be overloaded in the same class or in a subclass.

Example

public class Overloading {
 
	public int test(){
		System.out.println("test1");
		return 1;
	}
 
	public void test(int a){
		System.out.println("test2");
	}	
 
	//以下两个参数类型顺序不同
	public String test(int a,String s){
		System.out.println("test3");
		return "returntest3";
	}	
 
	public String test(String s,int a){
		System.out.println("test4");
		return "returntest4";
	}	
 
	public static void main(String[] args){
		Overloading o = new Overloading();
		System.out.println(o.test());
		o.test(1);
		System.out.println(o.test(1,"test3"));
		System.out.println(o.test("test4",1));
	}
}

The difference between overwriting and overloading

Difference pointsOverloaded methodOverridden method
Parameter listMust be modifiedMust not be modified
Return typeCan be modifiedMust not be modified
ExceptionCan be modified Can be reduced or deleted, must not throw new or wider exceptions
AccessCan be modifiedMust be Cannot make stricter restrictions (can lower restrictions)
Hot AI Tools
Undress AI Tool
Undress AI Tool

Undress images for free

AI Clothes Remover
AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress
Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT
ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT
Stock Market GPT

AI powered investment research for smarter decisions

Popular tool
Notepad++7.3.1
Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version
SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1
Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6
Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version
SublimeText3 Mac version

God-level code editing software (SublimeText3)