Java Learning · The first knowledge object-oriented deep one

Source: Internet
Author: User
Tags modifier

Object-Oriented depth

1. Object-oriented three major features

A) Inherit inheritance

Subclasses can inherit properties and methods from the parent class

Subclasses can provide their own property methods

b) Package Encapsulation

Hide some properties and methods from the outside

Exposing certain properties and methods externally

c) polymorphic polymorphism

Multiple variations of adaptive variables

1.1 Package

A) Why encapsulation is required

Hides the complexity inside the object, exposing simple interfaces only externally. Easy to call outside, improve system scalability, maintainability;

b) Implement encapsulation

Using Access-rights characters

Member Four access rights

    1. public.

Project visible

    1. Protected protected

Class itself access

Other class access within the package

Sub-class access

    1. Default defaults (package visible)

Class itself access

Other class access within the package

    1. Private privately owned

Accessed only by this class itself

Two kinds of access rights for a class

    1. Public

All class access in the project

    1. Default

Class access within the same package

Packaging essentials

    1. Handling of properties of a class

A) generally use private unless subclass inheritance is required

b) Provide the Get/set access property, usually by public

c) Auxiliary methods only for this class private

D) methods that you want other classes to call public

1.2 Inheritance

Definition: A class is an abstraction of an object, and inheritance is an abstraction of a batch of classes

Meaning: Improving code reusability

public class Testextend {

public static void Main (string[] args) {

Mammal A = new mammal ();

A.eat (); Inherits the Eat () from the parent class;

A.pure ();

}

}

public class animal{

String name = "Cerebellum Axe";

int age = 2;

public void Eat () {

System.out.println ("eat");

}

}

public class mammal Extend animal{

public void Pure () {

System.out.println ("suckling");

}

}

Other

    1. Java has only single inheritance
    2. The public parent class for all classes is Object

1.3 override of Method

a) = = Method name, same parameter

b) <= return value type and exception type subclass less than parent class

c) >= access permission subclass greater than parent class

Object class

The object class is the base class for all classes and contains many methods

1. Example: overriding ToString (Default return package + class [email protected] hash code)

    1. 2. Comparison of objects = = and Equal

a) = =

A) Compare the values of the two basic variables

b) Compare two reference types memory is the same, point to the same object

b) Equal

A) whether the contents of the two objects are the same

1.4 Super Key Words

b) Super is a reference to the immediate parent class object

c) The properties and methods covered by the quilt class in the parent class can be accessed through super

1.5 polymorphic

Polymorphism is an important feature of OOP, which is used to implement dynamic linking, that is, the final state of a program is determined only by the re-execution process and not by the compilation period.

    1. How to use polymorphic

A) Two types of reference types

I. Compile-time Type: Type determination at the time of declaration

Ii. run-time type: determined by the actual type of the object

b) Three necessary conditions for polymorphic existence

I. To have inheritance

II. To have a method to rewrite

Iii. Parent class reference to child class object

Example

Note: Type conversions for reference types

1.6 Final keyword

    1. The final keyword can be used to modify variables, classes, methods

A) modifier variable: Equivalent to defining a constant that cannot be changed

b) Modification method: cannot overwrite in subclass

c) Modifier class: Cannot be inherited

Java Learning · The first knowledge object-oriented deep one

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.