Java Learning Diary Week3 Object-oriented 2: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

One, pack (package)

1.package:

Why is the package required?

To resolve the problem of duplicate names between classes.

For ease of management class: The right class is located in the appropriate package!

How does the package work?

is usually the first non-annotative statement of a class.

Package Name: The domain name is written backwards, plus the module name, and the internal management class.

In fact, internal implementation is by the directory structure to do.

Com.sun.test

Com.oracle.test (COM.ORACLE.TEST.TEST1) is a two different package;

Main packages in 2.JDK:

java.lang-contains some of the core classes of the Java language, such as String, Math, Integer, System, and thread, to provide common functionality.

Java.awt-contains several classes that make up an abstract window toolset (abstract Windows toolkits), which are used to build and manage the graphical user interface (GUI) of the application.

The java.net-contains classes that perform network-related operations.

The java.io-contains classes that can provide multiple input/output functions.

java.util-contains utility classes, such as defining system attributes, using functions related to date calendars.

3.import

Import can be imported into classes under other packages, which can be called directly from the class name in this class.

If you do not use the import, the other package class, can only write: Java.util.Date, the code is too large, not conducive to writing and maintenance.

  Import using:

      Import Java.util.Date;

Import java.util.*; Import all the classes under the package. Will slow down the compilation speed, but will not slow down.

Java imports all classes under the Java.lang package by default, so we can use these classes directly.

If you import two classes with the same name, you can only display the calling related class with the package name + class Name:

Java.util.Date Date = new Java.util.Date ();

    Static import (added after JDK 1.5):

Role of static import: For importing static properties of a specified class

     Import Static java.lang.math.*;//Import all the properties of the Math class

Import static java.lang.math.pi;//The PI property of the Math class

Then, we can use it directly in the program: System. out. println (PI);

Ii. Succession (Inheritance,extend)

Inheritance can improve the reusability of code and simplify the code.

1. Keywords: extends

One class inherits another class called the inheritors as a subclass, and the successor is the parent class (superclass);

The subclass contains all the properties of its parent class, (private) method;

Inheritance is implemented through extends: (used in the class declaration, in Java, single inheritance inherits only one parent class, the object class is inherited by default when extends defaults, and the object class is a superclass of all classes)

    Class A extends Class B () {};

2. Overriding the method

differs from method overloading, where a method override is possible only if the subclass inherits the parent class;

The return value type method Name parameter list is the same for the parent class method override that requires its child class method;

Overridden method access permission cannot be more restrictive than its inherited method;

3.super keywords

Super is a reference to the immediate parent class object. You can access the methods or properties covered by the quilt class in the parent class through Super.

Common methods: no sequential restrictions. can be called casually.

Constructor : in the constructor of any class, if the first line of code for the constructor does not explicitly call super (...); Then Java will call Super () by default, as the initialization function of the parent class. So the super () you are here, it doesn't matter if you add or not.

4.Object class

The object class is the root class of all Java classes,

If the base class is not indicated in the declaration of the class with the extends keyword, the default base class is the object class

Some methods in the object class:

ToString () Method:

      The object class is defined with the public String toString () method whose return value is a String type that describes information about the current object.

The ToString () method of the object class is automatically called when a string is connected to other types of data, such as SYSTEM.OUT.PRINTLN ("info" +person)

You can override the ToString () method in a user-defined type as needed

Equals (Java.lang.Object) method:

The Equals method of object is defined as: X.equals (y) returns True when X and Y are applied to the same object otherwise false

Some of the classes provided by J2SDK, such as String,date, override the Equals method of object, call the Equals method of these classes, X.equals (y), and when X and Y refer to objects that are of the same class and attribute contents are equal (not necessarily the same object), return True otherwise returns false.

You can override the Equals method in a user-defined type as needed.

5. Permission modifier (Public private protected)

The same class

In the same package

Sub-class

All classes

Private

*

Default (Defaults)

*

*

Protected

*

*

*

Public

*

*

*

*

The table displays the range of permissions that variables/methods/classes modified by different modifiers can be accessed;

Tri-polymorphic (polymorphism)

If the compile-time type is inconsistent with the runtime type, it can cause polymorphism.

Polymorphism is an important feature of OOP, which is mainly used to implement dynamic linking, in other words, the final state of a program is determined only during the execution process and not during compilation. This can improve the flexibility and scalability of the system for large systems.

Polymorphism is the polymorphism of the method, and the attribute is not polymorphic.

There are 3 prerequisites for polymorphic existence: To have an inheritance, to have a method override, a parent class reference to a child class object

Java Learning Diary Week3 Object-oriented 2: encapsulation, inheritance, 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.