Virtual Methods in Java

Source: Internet
Author: User
A reference variable declared as a parent class type can only call a method in the parent class, and if the variable actually refers to a subclass object, and the subclass object overrides the method of the parent class, then the parent class object invokes a method in the subclass, which becomes a virtual method call. So, the same two reference variables that call the same method result may be different.
The polymorphism between the parent class and the subclass, redefining the function of the parent class. If you define a method in a subclass with the same name and parameters as its parent class, we say that the method is overridden (overriding). In Java, subclasses can inherit methods from the parent class without having to rewrite the same method. But sometimes subclasses do not want to inherit the method of the parent class intact, but want to make some modifications, this requires the use of the method of rewriting. Method overrides are also called method overrides.
If a method in a subclass has the same method name, return type, and parameter table as a method in the parent class, the new method overwrites the original method. You can use the Super keyword, which refers to the parent class of the current class, if you want to have methods in the parent class.
The access modifier permission for a subclass function cannot be less than the parent class;


public class Employee
{
private String name;
Private String address;
private int number;
Public Employee (string name, string address, int number)
{
SYSTEM.OUT.PRINTLN ("Constructing an Employee");
THIS.name = name;
this.address = address;
This.number = number;
}
public void MailCheck ()
{
SYSTEM.OUT.PRINTLN ("Mailing a check to" + THIS.name
+ "" + this.address);
}
Public String toString ()
{
Return name + "" + Address + "" + number;
}
Public String GetName ()
{
return name;
}
Public String getaddress ()
{
return address;
}
public void setaddress (String newaddress)
{
address = newaddress;
}
public int GetNumber ()
{
return number;
}
}










public class Salary extends Employee
{
private double salary; Annual salary
Public Salary (string name, string address, int number, double
Salary
{
Super (name, address, number);
Setsalary (Salary);
}
public void MailCheck ()
{
System.out.println ("Within MailCheck of Salary class");
System.out.println ("Mailing Check to" + GetName ()
+ "with salary" + salary);
}
Public double getsalary ()
{
return salary;
}
public void Setsalary (double newsalary)
{
if (newsalary >= 0.0)
{
salary = Newsalary;
}
}
Public double Computepay ()
{
System.out.println ("Computing salary pay for" + getName ());
return SALARY/52;
}
}








public class Virtualdemo
{
public static void Main (String [] args)
{
Salary s = new Salary ("Mohd Mohtashim", "Ambehta, Up", 3, 3600.00);
Employee e = new Salary ("John Adams", "Boston, MA", 2, 2400.00);
System.out.println ("Call MailCheck using Salary reference-");
S.mailcheck ();
System.out.println ("\ n call MailCheck using Employee reference--");
E.mailcheck ();
}
}


We have instantiated two salary objects. One uses the salary reference s, and the other uses the employee reference.
At compile time, the compiler checks the declaration of the MailCheck () method in the salary class.
When S.mailcheck () is invoked, the Java Virtual Machine (JVM) invokes the MailCheck () method of the salary class.
Because E is a reference to employee, the MailCheck () method of calling E has a completely different result.
When the compiler checks the E.mailcheck () method, the compiler checks the MailCheck () method in the Employee class.
At compile time, the compiler uses the MailCheck () method in the employee class to validate the statement, but at run time, the Java Virtual Machine (JVM) invokes the MailCheck () method in the salary class.
This behavior is referred to as a virtual method call, which is called a virtual method.
All of the methods in Java can be represented in this way, by which the overridden method can be invoked at run time, regardless of the data type of the reference variable in the source code at compile time.













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.