Method overloading and method rewriting in JAVA SE and overloading of sub-programs in databases

Source: Internet
Author: User
Tags java se

The first method overrides and method overloads are based on the Java object-oriented inheritance and polymorphism characteristics. As for object-oriented inheritance and polymorphism, I'm not here to say more. Inheritance refers to the creation of a subclass on the basis of a parent class, so that the class has a non-private attribute of the parent class, and the subclass can extend its own attributes, which leads to method overrides and method overloads!

One, method override (Override)

How to define overrides in Java: The inheritance attribute of a class in a Java program can produce a subclass in which the subclass inherits the parent class's non-private properties (methods and variables), can add its own properties (methods and variables) in the subclass, and can also extend the methods in the parent class. To enhance their functionality, this is called rewriting, also known as replication or overwrite. The so-called method overrides are subclasses of methods and methods inherited from the parent class have exactly the same method name, return value type, method parameter number, and parameter type, in order to be called method rewrite.

The code reflects:

This is the definition of the parent class
public class Person {

public void Eat () {
System.out.println ("===== This is the Eat method ======= of the parent person");
}
}

This is the definition of a subclass
public class Student extends person {

@Override
public void Eat () {
System.out.println ("= = = This is a subclass of student eat method = = =");
}

Main Method Test
public static void Main (string[] args) {
Student Student = new Student ();
Student.eat (); Output: = = = This is the Eat method for subclass student = = =
}
}

When a subclass overrides a method of a parent class, it calls a method in the subclass when the subclass is instantiated, and the parent class's method is as if it were overwritten. If you need to call a method of the parent class in a subclass, use the Super keyword in the subclass method to call the method of the parent class, in the format: Super. The name of the method in the parent class (parameter list).

Rewrite rule:

The following rules are required to implement a method rewrite when you perform a method rewrite:

(a) The parameter list of a subclass method must be the same as the parameter list (number of arguments and parameter type) of the overridden method in the parent class, otherwise only the overloads of the method can be implemented.

(b) The return value type of the subclass method must be the same as the overridden method return value type in the parent class, otherwise the method overload can only be implemented.

(iii) in Java, a subclass method cannot have access to a method that is overridden in the parent class and must be greater than or equal to the access rights of the parent class.

(iv) in the process of rewriting, if the overridden method throws an exception in the parent class, the method in the subclass also throws an exception. But thrown exceptions also have certain constraints---> subclasses cannot throw more exceptions than the parent class, and can only throw exceptions that are smaller than the parent class, or do not throw exceptions. For example, if the parent class method throws exception, the subclass can only throw IOException or throw an exception that is smaller than exception or throws an exception.

Second, method overloading (overload)

How to define overloading in Java: In Java, by writing multiple methods in a class, the method name of the method is the same, the method's parameter list is different (the number of arguments and the parameter type differ) relationship serve the current method overload. The so-called method overloading is: In a class, there are a series of methods with the same method name, but the parameter list is different, such methods are now the method overload. Note the construction method can also be overloaded.

The code reflects:

This is the definition of the parent class
public class Person {

/*
* protected void Eat () throws exception{
* SYSTEM.OUT.PRINTLN ("===== This is the Eat method ======= of the parent person"); }
*/

Public person () {
System.out.println ("This is a non-parametric construction method");
}

Public person (String name) {
System.out.println ("This is the constructor method with a parameter name=" + name);
}

void print () {
System.out.println ("= = = = = = = = = = = = = = =");
}

void print (String str) {
System.out.println ("= = = = = = = = = = = = = = = = = = =")
}

void print (String str, int a) {
System.out.println ("= = = two parameters of the Print method = = = =");
}
}

When the object calls these methods, the Java VM executes different method bodies according to the different parameter lists, which is a manifestation of polymorphism.

Overloading rules:

The following rules are required for the method overloading:

(a) When you reuse a method overload, you must implement the method overloading by using a different parameter list in the method. such as: The number of parameters of the method is different or the method parameter type is different.

(ii) Overloads cannot be implemented by access rights, return value types, and thrown exceptions

(c) The exception type of the method and the number of thrown exceptions do not affect the overloads of the method, which means that different exceptions are allowed in overloaded methods

(iv) can have different return value types, as long as the method's parameter list is different

(v) can have different access modifiers

Third, the difference between overriding and overloading

Distinguishing points

Overload

Rewrite (overwrite)

English

Overloading

Overiding

Defined

Method name is the same, parameter type or number is different

Method names, parameter types, return value types are all the same

No Permissions Required

Overridden methods cannot have more restrictive permissions

Range

Occurs in a class

Occurs in an inheriting class

Note: The construction method cannot be inherited and therefore cannot be overridden, and the constructor of the parent class can only be called by the Super keyword in the subclass, but may be overloaded.

Main Reference article connection: http://blog.csdn.net/zhangyabinsky/article/details/7047330

Iv. Database Sub-program overloading

PL/SQL allows overloading of both the child and local sub-programs. Overloading refers to two or more sub-programs having the same name, but with different parameter variables, parameter order, or parameter data types

Example: CREATE OR REPLACE Package Demo_pack1

Is

Deptrec Dept%rowtype;

V_sqlcode number; V_sqlerr VARCHAR2 (2048);

FUNCTION query_dept (dept_no in number)

RETURN INTEGER;

FUNCTION query_dept (dept_no in VARCHAR2)

RETURN INTEGER;

END Demo_pack1;

Attention:

1, if the parameters of two sub-programs are only different names and ways, they cannot be overloaded.

Example: PROCEDURE overlodeme (p_parameter in number)

PROCEDURE Overlodeme (p_parameter out number)

2. Cannot be overloaded based on the return type of two functions only

Example: FUNCTION overlodeme RETURN Date

FUNCTION Overlodeme RETURN Boolean

3, the parameters of the overloaded function must be different in the type series, can not be overloaded on the same type series

Example: PROCEDURE overlodeme (P_parameter in char)

PROCEDURE Overlodeme (P_parameter out VARCHAR2 (10))

Method overloading and method rewriting in JAVA SE and overloading of sub-programs in databases

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.