Method overloading of Java learning notes, dynamic method scheduling and abstract classes, and java learning notes

Source: Internet
Author: User

Method overloading of Java learning notes, dynamic method scheduling and abstract classes, and java learning notes

I. Method Overloading

If the method in the subclass has the same method name as the method in its superclass, it is called the method in the subclass to overload the methods in the superclass, especially when the method names and parameter types in the superclass and subclasses are the same, when this method is called in a subclass, the methods in the superclass are hidden. Consider the following program:

 1 class A  2 { 3     int i, j;  4     A(int a, int b)  5     {  6         i = a;  7         j = b;  8     }  9     10   // display i and j 11     void show() 12     { 13         System.out.println("i and j: " + i + " " + j); 14     } 15 } 16  17 class B extends A 18 { 19     int k; 20  21     B(int a, int b, int c)22     {23         super(a, b); 24         k = c; 25     } 26      27       // display k – this overrides show() in A 28     void show()29     { 30         System.out.println("k: " + k); 31     } 32 }33 34 35 public class myJavaTest 36 { 37     public static void main(String args[]) 38     { 39         B subOb = new B(1, 2, 3); 40      41         subOb.show(); // this calls show() in B 42     } 43 } 

Running result: k: 3

The show () in subclass B overload show () in the superclass A. When the overload method is called from the subclass (41 rows), it always references the method defined by the subclass (28-31 rows ), the show () method (line 11-14) in the superclass is hidden. Of course, if you want to access the overloaded superclass, you can use super as follows:

 1 class B extends A  2 {  3     int k;  4       5     B(int a, int b, int c)  6     {  7         super(a, b);  8         k = c;  9     } 10     void show() 11     { 12         super.show(); // this calls A's show() 13         System.out.println("k: " + k); 14     } 15 } 

 

If you replace the above version with this code, the running result will be changed:

I and j: 1 2
K: 3

Super. show () calls the hidden superclass method.

Note: When the subclass and the superclass only have the same method name but different parameter types, the system determines which version is used based on the input parameter type when the subclass calls the method.

Ii. Dynamic Method Scheduling

The above example explains how to overload methods. The true significance of method Overloading is that it forms the foundation of one of the most powerful Java concepts: dynamic method scheduling. Dynamic Method scheduling is a mechanism that calls overloaded methods during compilation instead of running programs. It is the basis for implementing Runtime polymorphism.

Consider the following program:

1 // Dynamic Method Dispatch 2 class A 3 {4 void callme () 5 {6 System. out. println ("Inside A's callme method"); 7} 8} 9 10 class B extends A 11 {12 // override callme () 13 void callme () 14 {15 System. out. println ("Inside B's callme method"); 16} 17} 18 19 class C extends A 20 {21 // override callme () 22 void callme () 23 {24 System. out. println ("Inside C's callme method"); 25} 26} 27 28 public class myJavaTest 29 {30 public static void main (String args []) 31 {32 A = new a (); // object of type A 33 B B = new B (); // object of type B 34 C c = new C (); // object of type C 35 A r; // declare A reference to a r 36 37 r =; // reference the r object pointing to A 38 r. callme (); // calla's version of callme39 40 r = B; // reference the object where r points to B 41 r. callme (); // callb's version of callme42 r = c; // reference the object where r points to C 43 r. callme (); // callc's version of callme44} 45}

 

The output is as follows:

Inside A's callme method
Inside B's callme method
Inside C's callme method

The program creates A superclass named A and its two sub-classes B and C. Subclass B and C reload the callme () method defined in. The main () function declares the objects of Class A, Class B, and class C. In addition, A reference r of the type is also declared. As shown in the output, the executed callme () version is determined by the type of the referenced object during the call.

From the above example, we can see that the overload method allows Java to support Runtime polymorphism, that is, the method of which version is selected when the program is running, so as to implement "one interface, multiple methods ". The superclass provide all elements that can be directly used by subclasses. Polymorphism also defines the methods that these derived classes must implement by themselves. This allows subclass to flexibly define their own methods while enhancing consistent interfaces.

3. Application Method Overloading

The following program creates a super class named Figure, which stores the sizes of different two-dimensional objects. It also defines a method area (), which calculates the area of an object. The program derives two subclasses from Figure. The first is Rectangle, and the second is Triangle. Each subclass loads the area () method and returns the area of a rectangle and a triangle respectively.

 1 class Figure  2 {  3     double dim1;  4     double dim2;   5   6     Figure(double a, double b)  7     {  8         dim1 = a;  9         dim2 = b; 10     } 11  12     double area() 13     { 14           System.out.println("Area for Figure is undefined."); 15           return 0; 16     } 17 } 18  19 class Rectangle extends Figure 20 { 21     Rectangle(double a, double b) 22     { 23         super(a, b); 24     } 25  26   // override area for rectangle 27     double area() 28     { 29         System.out.println("Inside Area for Rectangle."); 30         return dim1 * dim2; 31     } 32 } 33      34 class Triangle extends Figure 35 { 36     Triangle(double a, double b) 37     { 38         super(a, b); 39     } 40      41       // override area for right triangle 42     double area() 43     { 44         System.out.println("Inside Area for Triangle."); 45         return dim1 * dim2 / 2; 46     } 47 } 48      49 public class myJavaTest 50 { 51     public static void main(String args[]) 52     { 53         Figure f = new Figure(10, 10); 54         Rectangle r = new Rectangle(9, 5); 55         Triangle t = new Triangle(10, 8); 56      57         Figure figref; 58      59         figref = r; 60         System.out.println("Area is " + figref.area()); 61      62         figref = t; 63         System.out.println("Area is " + figref.area()); 64      65         figref = f; 66         System.out.println("Area is " + figref.area()); 67       68     }69 }

Output:

Inside Area for Rectangle.
Area is 45.0
Inside Area for Triangle.
Area is 40.0
Area for Figure is undefined.
Area is 0.0

In this case, if an object is derived from Figure, its area can be obtained by calling area. No matter which image type is used, the Operation interfaces are the same.

4. Use abstract classes

In the previous example, in a super-class Figure, the area () definition is only a placeholder, and it does not calculate or display the area of any type object. That is to say, sometimes we want to define a super class, but this super class only gives a structure of a class but does not provide implementation of the method, and inherits the subclass of the super class to share this structure, the specific implementation is filled in by each subclass.

Consider the above example. For the Triangle class, if it does not define area (), it will become meaningless. In this case, make sure that the subclass actually reloads all required methods. Java uses abstract method to solve this problem ). the abstract modifier specifies that some methods must be implemented by sub-classes. If you do not implement them, you will not be allowed to run them. In this way, sub-classes must overload them, rather than simply using the versions defined in the superclasses.

Consider the following program:

1 // A Simple demonstration of abstract. 2 abstract class A 3 {4 abstract void callme (); // declare the abstract method, which is not implemented in detail, leave it to the subclass to implement 5 6 // It is not allowed to instantiate an object with an abstract class, but it is still allowed to implement a specific method in the class to implement 7 void callmetoo () 8 {9 System. out. println ("This is a concrete method. "); 10} 11} 12 13 class B extends A 14 {15 void callme () // implements the super class abstract Method 16 {17 System. out. println ("B's implementation of callme. "); 18} 19} 20 21 public class myJavaTest 22 {23 public static void main (String args []) 24 {25 A; 26 B B = new B (); 27 a = B; 28. callme (); 29. callmetoo (); 30} 31}

 

 

Output:

B's implementation of callme.
This is a concrete method.

Note:

  • Declare a general form of an abstract method: abstract type name (parameter-list) (4th rows)
  • As long as a class contains one or more abstract class methods, it becomes an abstract class and must be declared as an abstract class (as shown in the second line)
  • Although the abstract class cannot be used for instantiation (that is, it cannot be used to create A specific object, Statement a A = new A () is invalid ), however, they can be used to create object references (as shown in 25 rows)
  • The abstract class cannot be instantiated, but the abstract class can implement any number of specific methods (for example, a specific method is implemented in rows 7-10)
  • All subclasses must implement the abstract method in the superclass or change the subclass to be declared as abstract

The following uses an abstract class to improve the previous Figure class:

1 // Using abstract methods and classes. 2 abstract class Figure 3 {4 double dim1; 5 double dim2; 6 7 Figure (double a, double B) 8 {9 dim1 = a; 10 dim2 = B; 11} 12 13 // area is now an abstract method 14 abstract double area (); 15} 16 17 class Rectangle extends Figure 18 {19 Rectangle (double a, double B) 20 {21 super (a, B); 22} 23 24 // override area for rectangle 25 double area () 26 {27 System. out. println ("Inside Area for Rectangle. "); 28 return dim1 * dim2; 29} 30} 31 32 class Triangle extends Figure 33 {34 Triangle (double a, double B) 35 {36 super (a, B ); 37} 38 39 // override area for right triangle 40 double area () 41 {42 System. out. println ("Inside Area for Triangle. "); 43 return dim1 * dim2/2; 44} 45} 46 47 public class myJavaTest 48 {49 public static void main (String args []) 50 {51 // Figure f = new Figure (10, 10); // invalid because the abstract class cannot create 52 Rectangle r = new Rectangle (9, 5); 53 Triangle t = new Triangle (10, 8); 54 Figure figref; // only declares a reference pointing to the Figure class and will not create a specific object, therefore, the statement is valid 55 56 figref = r; 57 System. out. println ("Area is" + figref. area (); 58 59 figref = t; 60 System. out. println ("Area is" + figref. area (); 61} 62}

 

The 56-row variable figref is a reference of Figure, which means that it can be used to reference any object derived from Figure.

 

 

 

 

 

 

 

 

 

 

 

 

 

 


What are the conditions for method overloading in java? What are the functions of method rewriting and method overloading?

The overload involves the pain and name methods in the same class. The method name must be the same, and the parameter list is different, which is irrelevant to the type of the returned value.
Rewriting involves a method of the same name between the subclass and the parent class. The method name must be the same, the parameter list is different, and the return value type is the same (or its subclass ).

In JAVA, do classes that implement an interface need to reload all abstract methods in this interface?

To implement an interface, you must override all the methods of this interface. The methods in the java interface are abstract and there is no method body.

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.