Non-professional Code Nong Java Learning Note 4 Java inheritance and polymorphism

Source: Internet
Author: User
Tags define abstract

Inheritance and polymorphism

(i) Basic concepts of succession

Subclass extends Parent class (superclass) (defined by inheritance using variables common to the parent class, methods and properties, etc. other than private)

(ii) Inheritance of classes

1. Derived subclasses-Gets the parent class non-private defined method properties and related variables

2. Hiding and inheriting domains

(1) Inheritance of domains-inherit all non-privatized domains

(2) Domain hide-Defines the same variable as the parent class, the parent class's variable is hidden in the subclass, but the subclass executes its own method or a subclass of the variable, and executes the method inheriting the parent class to automatically use the hidden domain

3. Inheritance and overrides of methods

(1) Inheritance of methods-inheriting non-private methods of the parent class

(2) method overrides-unlike domain hiding, subclasses define the same method as the parent class, overwriting the parent class in a subclass, and the subclass can no longer use a method of the same name as the parent class

4.this and Super

(1) this-a reference to the current object, referring to the current object

(2) super-the properties and methods of the parent class

(iii) Polymorphism-situations in which different methods of the same name in a program coexist

1. Method coverage to achieve polymorphism

2. Overloads of the method implement polymorphism (you will learn the overloaded knowledge later, and consider reloading and implementing different functions)

3. The polymorphism of the object reference: The subclass object can be an object of the parent class at the same time

(iv) Overloading of methods-redefinition of methods that already have the same name ( more important )

(For more information, see the blog post today)

(v) Overloads of a constructor-the same class, multiple constructors with the same name but different argument lists

With so much to ask, what's the use of this? Take a look at Baidu feel meaningful, textbooks really do not see what .....

The main benefit of method overloading is that you do not have to write multiple functions in order to have different parameter types or number of arguments. Multiple functions with the same name, but the parameter table, that is, the number of parameters or (and) data type can be different, when called, although the method name is the same, but according to the parameter table can automatically call the corresponding function. If we use reflector to see the base Class library of. NET written by Microsoft, we can find that he uses a lot of method overloads, so that when we call, we do not need to remember so many method names, but know the function of the method can directly give him to pass different parameters, the compiler will clearly know which method we call.

(vi) package and its use

1. Basic concept of the package-personal understanding of C # namespace (but the difference is still large, the package can be associated with the directory)

2. Create: Pakage package name {class a1{} class a2{}};, Command mode: javac–d location *.java (current directory with-D.)

3. Use: Import package. class; You can specify where the package is stored by classpath the environment variable

(vii) Interface problems-not much different from C #

1. Interface Overview: Similar classes, which define abstract methods and constants, form a collection of attributes to help implement multiple inheritance

2. Declare interface [public] interface interface Name [extends parent interface list]{constants; abstract method;}

3. Interface implementation: Through the class to implement the interface, class Class1 extends * * * implements interface name {}, where implement is the Declaration of implementation

Non-professional Code Nong Java Learning Note 4 Java inheritance and 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.