Basic Java Tutorial: Object-Oriented programming [2]

Source: Internet
Author: User

Basic Java Tutorial: Object-Oriented programming [2] three features package

Encapsulation (English: encapsulation) refers to a method of wrapping and hiding the implementation details of an abstract function interface . Encapsulation can be thought of as a protective barrier against random access to code and data of that class by code defined by external classes .

Using encapsulation we can control member variables more precisely, while hiding information, implementing details, and so on.

Method:
public class person{    private String name;    private int age;?    public int getage () {      return age;    }?    Public String GetName () {      return name;    }?    public void Setage (int.) {      this.age = age;    }?    public void SetName (String name) {      this.name = name;    }}

Description

The public method in the above instance is the entry for the external class to access the class member variable. Typically, these methods are called getter and setter methods. Therefore, any class that accesses private member variables in a class is passed through these getter and setter methods .

Inherited

Inheritance is a cornerstone of Java object-oriented programming technology because it allows classes of hierarchical hierarchies to be created. inheritance is the child class inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class's instance domain and method, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class . The inheritance conforms to the relationship that is is-a, the parent class is more general, the subclass is more specific !

  

Method
Class Parent Class {} class subclass extends parent class {}

Description

  • subclasses have properties that are not private to the parent class, methods .

  • Subclasses can have their own properties and methods, that is, subclasses can extend the parent class .

  • subclasses can implement methods of the parent class in their own way .

  • Java inheritance is a single inheritance, but can be multiple inheritance , single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so the Class C is the parent class of Class B, and Class B is the parent class of Class A, which is different from c+. Java inheritance. + an attribute of inheritance.

  • Increases the coupling between classes (the disadvantage of inheritance, high coupling will cause the relationship between the code).

Polymorphic

Polymorphism is the ability of the same behavior to have many different manifestations or forms. Polymorphism is the same interface that uses different instances to perform different operations,

  

Method:

public class Test {public static void main (string[] args) {Show (New Cat ());  Call the Show method Show (New Dog ()) as a Cat object;  Call the Show method with the Dog object Animal a = new Cat ();               Upward transformation a.eat ();        Called Cat eat cat C = (cat) A;        Downward transformation c.work ();          The call is Cat's work} public static void Show (Animal a) {a.eat ();              Type Judging if (a instanceof cat) {//cat do things cat C = (cat) A;          C.work ();              } else if (a instanceof dog) {//dogs do things dog C = (dog) A;          C.work ();  }}} abstract class Animal {abstract void eat ();      } class Cat extends Animal {public void Eat () {System.out.println ("eat fish");      } public void Work () {System.out.println ("catch Mouse");      }} class Dog extends Animal {public void Eat () {System.out.println ("eat Bones"); } public void Work () {System.out. println ("housekeeping"); }  }

Description

Three prerequisites for polymorphic presence:

  • Inherited
  • Rewrite
  • Parent class reference to child class object

When calling a method using polymorphic mode, first check whether the method is in the parent class, if not, compile the error, and if so, call the subclass's same name method .

The benefits of polymorphism: The program can be well extended, and the objects of all classes can be universally handled .

Basic Java Tutorial: Object-Oriented programming [2]

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.