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.