Java Basics-Encapsulation, inheritance, polymorphism

Source: Internet
Author: User

JavaBasics-encapsulation, inheritance, polymorphism

--Fast growth of small instances

Abstract:

1. Objective: To simplify the complex things and to order the contents of Fanluan.

2. Process: The same, similar to the same thing, the different content of the door class.

3. Result: Class, interface.

Packaging:

1. Purpose: Peers, the scattered content of the attribution, control of authority.

2. Procedure: Unify the properties, methods (functions) of an object into its name, and set it separately. The appropriate permissions for control management.

3. Result: Object, interface.

Inherited:

1. The existence of a small and different; In a series, most of the content is classified as the parent class, and the subclass inherits the contents of the parent class, and can have some development and individuality of its own.

2. Enhance reusability and extensibility.

Polymorphic:

Be flexible and move as needed.

1. Runtime polymorphism-When the program is running, the interface is intelligently assigned and docked according to the facts at that time.

2. Compile-time polymorphism--pre-written, sub-class on the parent class based on the performance of the same function changes.

functionfunction):

1. Structure: Divided into five components--permission modifier, function name, parameter, function body, return value type. (in different cases, some content needs to be omitted)

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

3. Overloading: A complete transformation of the structural level of a function. The parameter must be changed, the return value type, or the function body implementation can be variable (where the number of "parameters" or at least one of the parameter types changes).

Example code:
public class Testbase {public static void main (string[] args) {//cat cat1 = new Cat (); The error "actual parameter list and formal parameter list length is different" is suggested by cat cat2 = new Cat ("Persian"); Wolf Wolf1 = new Wolf (); Wolf1.run ("Mongolian Wolf"); Wolf Wolf2 = new Wolf ("Mongolian Wolf"); Wolf2.run ("Mongolian Wolf"); Tiger Tiger = new Tiger (); Tiger.eat ("Meat"); Animal Animal = new Wolf ("Northwest Wolf"); Animal.run ();}} Abstract class Animal {string food;protected string name;//parameterless constructor public Animal () {System.out.println ("\n--Parent class Non-parametric construction Method! \ n ");};/ /parameter Constructor public Animal (String name) {this. name = name; The parent of the SYSTEM.OUT.PRINTLN (Name + ") has a constructor method! \ n ");} Abstract method Eat (String food),//general method public void Run () {System.out.println ("Parent general Method Run ()" + "I like run!\n");}} Inherit parent class Animalclass Cat extends Animal {//Parameter constructor public Cat (String name) {//Initialize parent class construction method super (name); System.out.println ("Subclass Cat has a parametric constructor!) \ n ");} public void Eat (String food) {///member variable food inherits from the parent class--protected member variable this. food = food; System.out.println ("Cat +food+" for food!) \ n ");}} Inherit parent class Animalclass Wolf extends Animal {//Non-parametric construction method public Wolf () {///non-display calls the parent class constructor method, when the program uses this parameterless constructor method, the system automatically calls the parentClass no parameter construction method//test ———— The time of the call of the parameterless constructor System.out.println ("called The Wolf class of the parameterless construction Method!") \ n ");}; Public Wolf (String name) {////non-display calls the parent class constructor method, when the program uses this argument constructor method, the system will automatically call the parent class without the parameter constructor method//member variable name inherits from the parent class--public member variable this. name = name; System.out.println ("Subclass Wolf has a parametric constructor!") \ n ");} public void Eat (String food) {///member variable food inherits from the parent class--protected member variable this. food = food; System.out.println ("Wolf with" +food+ "for food!) \ n ");} Overriding the general method of the parent class public void run () {System.out.println ("subclass Wolf overrides the parent class's run () method" + "I don't like run, but I must is to do.\n");} General method of overloading the parent class public void run (String name) {System.out.println (name + "Subclass Wolf Overloads the Run () method of the parent class--" + "name" + "says: \" "+" I don ' t Li Ke run, but I must do.\ "\ n");}} Inherits the parent class Animalclass Tiger extends Animal {public void eat (String food) {this. Name = "Siberian tiger"; System.out.println ("My name is:" + name + "; I eat with" + Food + ")! \ n ");} }

Run:

Java Basics-Encapsulation, inheritance, polymorphism

Related Article

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.