Java Learning notes-inheritance and polymorphism (UP)

Source: Internet
Author: User

1. Using inheritance to develop superclass (superclass)

2. Use super keyword to evoke super class construction method

3. Overriding methods in Super-class

4. Distinguish between override and overload

5. Explore the ToString () class in the object class

6. Discovery of polymorphism and dynamic binding

7. Describe why it is necessary to interpret the downward transition

8. Explore the Equals method in the object class

9. Storage, retrieval, implementation of Arraylis objects

10. Using the ArrayList class to implement the stack

11. Using data and methods in super-class, protected

12. Using the final module to suppress overrides for classes and methods

Review:

Object-oriented thinking: You can get new classes from existing classes, which is inheritance. Inheritance is an important and powerful feature in Java that can be reused, assuming that you encounter the following problems: circles, rectangles, and triangles can be obtained by inheriting a class.

1. Using inheritance to develop superclass (superclass)

Parent class and subclass (superclass and subclass)

Use classes to model the same problems, because different classes have many of the same properties and behaviors that can be generated in some classes, and other classes can also be shared. Inheritance allows you to define a public class and then extend it to more special classes that inherit properties and methods from a common class.

For example, objects of geometric objects, suppose you want to design classes and model geometric objects, such as circle and rectangle. Geometric objects have many of the same properties and behaviors, such as they can be drawn, filled, or not filled in a certain color.

Example: We write these three classes: Geometricobject Circle

  

 Public classGeometricobject {PrivateString color; Private Booleanfilled; Privatejava.util.Date datecreate;  PublicGeometricobject () {;//Constructor Method    }     PublicGeometricobject (String color,Booleanfilled) {        ; //constructor method with parameters    }     PublicString GetColor () {returnColor//get the color for the geometric Object    }     Public voidsetcolor (String color) { This. color = color;//Set the color    }     Public Booleanisfilled () {if(filled)return true; Else return false; }     Public voidSetfilled (Booleanfilled) {         This. filled =filled; }     Publicjava.util.Date getdatecreated () {returndatecreate; }        }

Circle

  

 Public classCircleextendsgeometricobject{Private Doubleradius;  PublicCircle () {; }     PublicCircle (Doubleradius) {         This. Radius =radius; }     PublicCircle (DoubleRadius, String color,Booleanfilled) {         This. Radius =radius;  This. SetColor (color);  This. setfilled (filled); }     Public DoubleGetradius () {returnradius; }     Public voidSetradius (Doubleradius) {         This. Radius =radius; }     Public DoubleGetarea () {returnradius*radius*3.14; }     Public DoubleGetperimeter () {return2*radius*3.14; }     Public DoubleGetdiameter () {return*radius; }     Public voidprintcricle () {System.out.println ("This is a Printcirlce method");; }}

Geometricobject Circle These two classes: the Geometricobject class can be used to build all of the geometric models, which contain color and fill filled, as well as the way to set properties and get properties. In Java about these two class relationships: Geoetricobject is the parent class, Circle is a subclass, and the Circle class inherits all the data regions and can also add data and methods

About grammatical relationships, circle and Geometricobject. Here are a few things to note:

1 sub-class storage for more information and more ways to use

2 Private data areas of the parent class are not allowed to be accessed directly by the external class.

3 Not all we think of xxx is xxx (round is the geometry) can use the idea of inheritance

4 inherited relationship is to XXX is the model of XXX

5 Some programming languages allow subclasses to be detached from the parent class

2. Use super keyword to evoke super class construction method

Subclasses inherit the data extents and methods from the parent class, can inherit the constructor method, or whether the subclass can invoke the constructor of the parent class. Super references the parent class of the class:

1 Calling the construction method of the parent class

2 Calling the parent class method

Call the constructor method of the parent class, with the following syntax

  

Super ();   Super (parameters);
The statement of super () evokes the constructor of the parent class without arguments.
Super (parameters); evokes a parameterized construction method.

Note: You use the Super keyword to evoke the constructor of the parent class, and the arousal must be the first step in the construct.

Construction Chain:

Whenever an instance of a class is constructed in a subclass, the constructor of the parent class is called, and if the parent class is detached from another class, the constructor of the subclass starts by invoking the constructor of the parent class.

Examples are as follows:

  

 Public class Person {    public person  () {    System.out.println ("(1) performs person ' s Tasks");    } }

This is the method for constructing the person class

 Public class extends person{    public  Employee () {        This ("(2) Invoke Employee ' s overloaded Constructor ");        System.out.println ("(3) performs Employee ' s Tasks");    }      Public Employee (String s) {        System.out.println (s);    }}

Generate an employee class from person

 public  class  Faculty extends   employee{ public< /span> static  void   main (string[] args) { //  TODO auto-generated method stub  new   Faculty ();  public   Faculty () {System.out.println (         "(4) performs Faculty's tasks"  //  TODO auto-generated constructor stub  }}  

By materializing an employee into an entity, observing a string of println output, the first step of constructing the person class is found in the order shown in the code above, followed by the invocation of the employee class, and finally the construction of the employee class, calling the employee's construction method. This procedure can be constructed according to the following diagram

Faculty calls the constructor of the employee class, the employee class calls the constructor of the person, and then because the person class is constructed to produce the employee class, the employee class is able to generate the faculty class. Much like the idea of learning stacks in data structures, step by step into the stack, the final call order is out of the stack order.

Note: In order to make the design of the class can be extended well, the general parameters in the parent class are set to NULL to prevent the syntax error, that is, in the design of a lot of classes in general to leave empty construction method, so as to well expand.

Super keywords can also call other methods instead of just calling construction methods. Follow the syntax below

Super.method (parameters);

3. Overriding methods in Super-class

Subclasses inherit the constructor from the parent class, but sometimes the subclass is implemented by modifying the method of the parent class, which is the method's override

1. The method of the instance can be overwritten only when access is allowed, so the private method of the parent class cannot be overwritten, because the private class cannot be accessed externally, the methods in the subclass are private to the parent class, and all the parent classes are not accessible, and the two methods are completely unrelated

2. Like instance methods, static methods can be inherited, but static methods cannot be overwritten, static methods are defined in the parent class, redefined in subclasses, and static methods of the parent class are hidden for subclasses. The hidden method can use the syntax to access

Superclassname.staticmethodname.

4. Distinguish between override and overload

Override is overwrite, overload is overloaded, can be memorized according to English words, ride and load, overwrite and reload, subclass method overrides method in parent class. In the class can have a lot of methods named the same, but different parameters, Java can be overload, that is, according to the use of parameters to find a matching method.

Follow the pseudo code below

Covered:

class a{    publicvoid  p () {       thisclass  A;        class extends a{   publicvoid  p () {     this is  A p method override From A,and we can and other statement   }   }

Overload:

 Public class a{   public  double  p (parametertype PA) {        statement;           return value;       }      Public int p (int  PA) {       statement;        return value;}   }

  

Java Learning notes-inheritance and polymorphism (UP)

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.