The three main features of Java object-oriented _java

Source: Internet
Author: User

Java object-oriented three major features: "Encapsulation, inheritance, polymorphism." For more information on Java technology, please log on to the Crazy Software education website. Micro-letter Search micro-signal: Crazy software, to participate in the 2015 promotional events, have the opportunity to obtain coupons and vouchers securities.

In this article, for example, a variable in the user class is a private variable and can only be assigned by creating an object that is automatically invoked by the constructor at that time.

The user class can only be accessed by the Public method API ().

The Admin class inherits the user class, invokes its construction method, and overrides the Method_1 method, adding a unique method power ().

User File

  public class User {/** * private variable, restricted to this class access/private String name;
  private int age;
  /** * Constructs a method that automatically invokes/public User (String name, int age) {this.name = name;
  This.age = age; /** * Private method, restricted to this class access/private void Method_1 () {System.out.println ("I am a" + name +);
  My age are: "+ age";
  /** * can inherit, overwrite, and call with package/protected void Method_2 () {System.out.println ("I am not override");
  /** * Public method, external interface * * () {method_1 ();
  Method_2 (); Admin file public class Admin extends User {/** * Construction method/Public Admin (String name, int age) {//Use the structure of the parent class
  Making method Super (name, age); /** * Overwrite the parent class with the same method/protected void Method_2 () {System.out.println ("NO, you are override"); Ah/** * Subclass specific
  Method */public void power () {System.out.println (' admin is powerful ');  The Main file public class main{publicly static void Main (string[] arg) {//Instantiate a user object and invoke the user's common method User A = new
  User ("User", 12); A.api ();
  Output line-wrapping, convenient to distinguish different code System.out.println ();
  Instantiate an Admin object and call Admin two methods admin Admin_me = new Admin ("admin", 23); Admin_me.api (); Inherits from the user parent class Admin_me.power ();
  Its own unique method of System.out.println ();
  /** * polymorphism */User test_admin = new Admin ("Test_admin", 34);
  Test_admin.api (); Test_admin.power ();
 The power this method is not declared in user, so you cannot use}}

The above is the entire contents of this article, I hope you can enjoy.

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.