My Java Learning notes (8) About polymorphism

Source: Internet
Author: User

About polymorphic

1. In the Java programming language, the object variable is polymorphic.

2. A reference to a superclass cannot be assigned to a subclass variable.

The 3.private method, the static method, the final method, or the constructor, the compiler will be able to know exactly which method should be called, and we will call this called static binding.

4. Dynamic binding has a very important feature: there is no need to modify the existing code, you can expand the urban and rural surprises.

5. When overriding a method, the subclass method cannot be less than the visibility of the superclass method.

6. Classes that do not allow extensions are called final classes.

7. If a class is declared final, only the method in it automatically becomes final, not the domain.

8. When a value is stored in a variable, the compiler checks to see if the operation is allowed. A reference to a subclass is assigned to a superclass variable, which is allowed by the compiler. However, assigning a reference to a superclass to a subclass variable requires a type conversion so that it can be checked by the runtime.

9. Type conversions can only be performed within an inheritance hierarchy and should be checked with instanceof before the superclass is converted to subclasses.

10. No exception is generated, just return false. This is done because NULL does not reference any object, and certainly does not refer to objects of type C.

11. If the bottom-up is moved up in the class's inheritance hierarchy, the classes on the upper level are more generic and may even be more abstract.

12. In order to improve the clarity of the program, the class containing one or more abstract methods must itself be declared as pumped.

13. In addition to abstract methods, abstract classes can also contain specific data and specific methods.

14. Abstract class methods act as placeholders for their specific implementations in subclasses.

15. There are two ways to extend an abstract class: One is to define a subset of abstract or abstract methods in a subclass, so that subclasses must also be marked as abstract classes, and the other is to define all the abstract methods so that they are not abstract.

16. Classes can also be declared as abstract classes, even if they do not contain abstract methods. Abstract classes cannot be instantiated. However, you can define an object variable of an abstract class.

17.private is visible only to this class, public is visible to all classes, protected is visible to this package and all subclasses, and the default modifier is visible to this package.

Instance Program:

Test class


public class test001 {public static void main (string[] arge) {person[] people = new person[2];p eople[0] = new Employee ("Wor Ker ", 1000,1987,1,1);p eople[1] = new Student (" Boy "," Computer "); for (person P:people) {System.out.println (P.getname () +", "+ P . GetDescription ());}}

Human


public abstract class Person {public abstract string getdescription ();p rivate string name;public Person (string n) {name = n ;} Public String GetName () {return name;}}

Employee class


Import java.util.*;p ublic class Employee extends person {private double salary;private Date hireday;public Employee (strin G n,double s,int year,int Month,int day) {super (n); salary = s; GregorianCalendar gr = new GregorianCalendar (year,month-1,day); hireday = Gr.gettime ();} Public double getsalary () {return salary;} Public Date Gethireday () {return hireday;} Public String GetDescription () {return String.Format ("a worker with a wage of%.2f", salary); public void raise (double p) {Double raise = salary * p/100;salary + = Raise;}}

Student class


public class Student extends person {private string major;public Student (String n,string m) {super (n); major = M;} Public String GetDescription () {return "a" + major + "professional Student";}}


My Java Learning notes (8) About polymorphism

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.