Java basics-encapsulation, inheritance, and Polymorphism

Source: Internet
Author: User

Java basics-encapsulation, inheritance, and Polymorphism
Java basics-encapsulation, inheritance, and Polymorphism

-- Rapid growth of small instances

Abstract:

1. Objective: to simplify complicated things and make the complicated content orderly.

2. Process: Combine the same and similar things and classify different contents.

3. Result: class and interface.

 

Encapsulation:

1. Objective: to convert the scattered content into an integer and implement permission control.

2. Process: unify the attributes and methods (functions) of an object to its name and set them separately. Manage appropriate permissions.

3. Result: object and interface.

 

Inheritance:

1. for similar content, most of the content in a series is classified as the parent class. The Child class inherits the content of the parent class and can have its own development and personality.

2. Enhanced reusability and scalability.

 

Polymorphism:

Adapt to random situations and act as needed.

1. Runtime polymorphism-after the program runs, the interface intelligently allocates and connects to each other based on the current situation.

2. polymorphism during compilation-write it in advance, and the subclass changes the performance of the same function on the basis of the parent class.

 

Function ):

1. Structure: it consists of five components: Permission modifier, function name, parameter, function body, and return value type. (Some content needs to be omitted in different situations)

2. Rewrite: Different implementations of function bodies with the same function name and parameters.

3. Overload: a complete structural change to a function. The parameter must be changed, the return value type, or the function body implementation can be changed (the number of "Parameters" or the parameter type must at least change ).

 

Sample Code:
Public class TestBase {public static void main (String [] args) {// Cat cat1 = new Cat (); // The error Cat cat2 = new Cat (Persian); Wolf wolf1 = new Wolf (); wolf1.run ); wolf wolf2 = new Wolf (yarn); wolf2.run (yarn); Tiger tiger = new Tiger (); tiger. eat (meat); Animal animal = new Wolf (Northwest Wolf); animal. run () ;}// abstract class Animal {String Food; protected String Name; // public Animal () {System. out. pri Ntln (-- no parameter constructor for the parent class !);}; // The constructor with parameters public Animal (String name) {this. Name = name; System. out. println (Name +'s parent class has a constructor !);} // Abstract method abstract void eat (String food); // general method public void run () {System. out. println (general method of parent class run () + I like run !);}} // Inherit the parent class Animalclass Cat extends Animal {// The constructors with parameters public Cat (String name) {// initialize the parent class constructor super (name); System. out. println (subclass Cat constructor with parameters !);} Public void eat (String food) {// The member variable Food inherits from the parent class -- protected member variable this. food = food; System. out. println (Cat eats + food + !);}} // Inherit the parent class Animalclass Wolf extends Animal {// the construction method of the non-argument public Wolf () {// The non-explicit call to the parent class constructor. When the program uses the non-argument constructor, the System automatically calls the no-argument constructor method of the parent class // validation ---- No-argument constructor call time System. out. println (the serverless constructor of the Wolf class is called !);}; Public Wolf (String name) {// a non-explicit call to the parent class constructor. When the program uses this constructor, the system automatically calls the non-argument constructor of the parent class // The member variable Name inherits from the parent class -- public member variable this. name = name; System. out. println (subclass Wolf constructor with parameters !);} Public void eat (String food) {// The member variable Food inherits from the parent class -- protected member variable this. food = food; System. out. println (wolf eats + food + !);} // Override the general method of the parent class public void run () {System. out. println (subclass wolf override the run () method of the parent class + I don't like run, but I must be to do .);} // reload the general method of the parent class public void run (String name) {System. out. println (name + subclass wolf reload the run () method of the parent class -- + name +: + I don't like run, but I must do. \ n) ;}}// inherit the parent class Animalclass Tiger extends Animal {public void eat (String food) {this. name = northeast tiger; System. out. println (MY Name IS + Name +; I feed on + food + !);}}

 

 

Run:

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.