In java learning, object-oriented features include encapsulation, inheritance, polymorphism, and super keyword and method rewriting (small record in java learning ).

Source: Internet
Author: User

In java learning, object-oriented features include encapsulation, inheritance, polymorphism, and super keyword and method rewriting (small record in java learning ).

In java learning, object-oriented features include encapsulation, inheritance, polymorphism, and super keyword and method rewriting (small record in java learning)

Author: Star)

 

Encapsulation

Permission modifier: public, private

Encapsulation steps:

1. Use private to modify the member variables to be encapsulated.

2. Provide a public method to set or access private properties

The set method is used. The name format is set attribute name (). The first letter of the attribute must be capitalized.

The get method is used for access. The name format is get attribute name (). The first letter of the attribute must be capitalized.

 

1 // method 2 public void setName (String name) {3 this. name = name; 4} 5 // name accessors Method 6 public String getName () {7 return name; 8}

 

Code Writing specification: in java development, generally class member variables (attributes) are encapsulated.

Q: Do all attributes require the set and get methods?

Not necessarily, depending on your needs.

Encapsulation: 1. framework 2. Tool class

Advantages of encapsulation: 1. Improved Data Security 2. Simple operations 3. Hidden implementation of methods.

1 class Student {2 private String name; // after the member variable is modified, it can only be accessed in this class and private. 3 private String sex; // the set and get methods are required for external access. 4 private int age; // you need to collect the data, but it is not public. The age only needs a set method, and the construction method of the 5 6 // two parameters 7 public Student (String name, string sex) {8 this. name = name; 9 if (sex. equals ("male") | sex. equals ("female") {10 this. sex = sex; 11} 12} 13 // normal method 14 public void study () {15 System. out. println (name + ""); 16} 17 // gender settor Method 18 public void setSex (String sex) {19 if (sex. equals ("male") | sex. equals ("female") {20 this. sex = sex; 21} else {22 System. out. println ("your input is invalid. Please enter it again .. "); 23} 24} 25 // gender accessors Method 26 public String getSex () {27 return sex; 28} 29 // method of name setter 30 public void setName (String name) {31 this. name = name; 32} 33 // name accessors METHOD 34 public String getName () {35 return name; 36} 37 // age configurator 38 public void setAge (int age) {39 this. age = age; 40} 41} 42 public class fengzhuang {43 44 public static void main (String [] args) {45 Student star = new Student ("star ", "gender"); // gender is illegal operation 46 star. study (); 47 System. out. println (star. getSex (); // gender null48} 49}

 

Inheritance

Feature: inherits the attributes and methods of the parent class. Feature: A method's single inheritance (Multi-Level inheritance) is not a multi-inheritance, and c ++ is a multi-inherited sub-class with N parent classes.

The Inheritance in java is the same as in OC.

The role of inheritance: optimize the code to reduce repeated use of the code

A: The Inheritance Method in B OC, how to express the inheritance relationship in java, and use the keyword extends to represent the inheritance.

Notes for inheritance:

1. Do not inherit for the sake of inheritance, do not save Code, any relationship is inherited, so it is nondescribable.

2. Private member variables of the parent class cannot be inherited.

3. the constructor of the parent class cannot be inherited.

4. The private method of the parent class cannot be inherited.

5. When a subclass calls its own constructor, it calls the non-argument constructor in the parent class by default.

6. Subclass cannot inherit the member variables with no default permissions in a package.

 

Why does it call the constructor of the parent class?
Subclass initializes the variables of the parent class when creating an object.

 

Tip: after all, these two attributes are inherited from the parent class. It is better to initialize them to the parent class ..

Super (name, sex); // It is generally specified by you (defined). If you specify the following super (), you cannot call it.

Super (); // calls the constructor of the parent class.

This (); // calls your own constructor.

 

The inheritance code is as follows:

1. Three features of package object-oriented; 2 3 class Person {4 String name; // default permission: friendly (friendly) private, public, protected 5 String sex; 6 Dog dog; // people and dogs have established an association relationship as a whole-> Part 7 public void eat () {8 System. out. println (name + ""); 9} 10} 11 12 // The Student class is scheduled to inherit the parent class 13 class Student extends Person {14 int age; 15 public void study () {16 System. out. println (name + ""); 17} 18} 19 20 public class jicheng {21 22 public static void main (String [] args) {23 Student s = new Student (); 24 s. name = "WKL"; // name does not have its own parent class for 25 s. age = 20; // age 26 System. out. println (s. name + "this year" + s. age + "years old .. "); 27} 28}

 

Association (not inheritance ):

1 class person {2 String name; 3 String sex; 4 Dog dog; // an association is established between humans and dogs-> Part 5 public void eat () {6 System. out. println (name + "eat"); 7} 8} 9 10 class Dog {11 String name; 12 public void cry () {13 System. out. println (name + ""); 14} 15} 16 17 18 public class jicheng {19 20 public static void main (String [] args) {21 person star = new person (); 22 star. name = "star"; 23 Dog dog = new Dog (); 24 dog. name = ""; 25 26 star. dog = dog; // give the dog object to star's dog object member attribute 27/* dog. cry (); */28 star. dog. cry (); // The star dog is named 29 star. dog = null; 30 star. eat (); 31} 32}

Super keyword (pointing to the Reference Space of the parent class Object)

Purpose:

1. when the child class and the parent class have member variables of the same name, a default this (this. name), you can use super to call the member variable (super. name ).

2. super can be used to call the constructor of the parent class.

Notes for using super:

1. Use super to call the parent class constructor. It must be the first statement in the constructor.

2. super can only appear in subclass methods or constructor methods.

3. super and this cannot call the constructor at the same time. (Because this is also the first statement in the constructor)

Summary: differences between super and this:

1. different things:

This indicates the caller object of the method.

Super: The reference space of the parent class object.

2. Inconsistent usage prerequisites:

This: it can also be used without inheritance.

Super: it can only be used under inherited conditions.

3. Call the constructor:

This: Call the constructor of this class.

Super: constructor of the called parent class

Method Rewriting)

Function: the method of the parent class does not meet the implementation of the subclass. In this case, you need to perform the behavior of the parent class through the rewrite (override) method.

Note:

1. There must be an inheritance relationship during method rewriting.

2. During method rewriting, the method name and format parameters must be consistent with the parent class.

3. During method rewriting, the permission modifier of the subclass must beGreaterOrEqualThe permission modifier of the parent class. (Private <protected <public, friendly <public)

4. When the method is overwritten, the return value type of the subclass must beLessOrEqualThe Return Value Type of the parent class. (Subclass <parent class) the data type does not have a clear parent-child relationship

5. During method rewriting, the exception type of the subclass must beLessOrEqualThe exception type of the parent class.

Method overloading: The method names are the same, but the number, type, and order of parameter lists are different.

 

Polymorphism (Different representations of the same type of an object)

The reference type of the parent class points to the object of the subclass.

Precautions for using polymorphism:

1. In the case of polymorphism, a member variable with the same name exists in the parent class and subclass. Whether static or non-static, the member variable in the parent class is accessed by default.

2. In the case of polymorphism, a non-static method with the same name exists in the parent class and subclass, and the non-static method of the subclass is accessed.

3. In the case of polymorphism, a static method with the same name exists for the parent class and the subclass, and the static method of the parent class is accessed.

4. In the case of polymorphism, you cannot access the attributes and Methods unique to the subclass.

5. multi-state conditions: there must be an inheritance relationship.

Summary:In the case of polymorphism, if a member with the same name exists in the subclass and parent classes, all members access the parent class, except for non-static variables with the same name.

Caused by java compiler compilation principles.

Compile to the left, and run not necessarily to the right.

Compile: during compilation, the java compiler detects that the reference type contains specified members. If not, an error is returned.

1 package study; 2 3 abstract class Animal {4 String name; 5 static String color = "Animal color"; 6 7 public abstract void run (); 8 9 public void eat () {10 System. out. println ("this is the method in the parent class"); 11} 12} 13 14 class Dog extends Animal {15 static String color = "yellow "; 16 // override method 17 public void run () {18 System. out. println (name + "running with four legs"); 19} 20 // rewrite the eat Method 21 in the parent class public void eat () {22 System. out. println ("this is the method in the subclass"); 23} 24} 25 26 class Fish extends Animal {27 28 // override the parent class method 29 public void run () {30 System. out. println (name + "swimming freely"); 31} 32} 33 34 public class star {35 public static void main (String [] args) {36 37 Animal a = new Dog (); // The Animal Pointer Points to Dog38. name = "Xiao Huang"; 39. run (); 40 41. eat (); 42 System. out. println (. color); 43 44 // a = new Fish (); // The Animal Pointer Points to Fish45 //. name = "Xiao Huang"; 46 //. run (); 47} 48}

Scenarios with Polymorphism:

1. polymorphism can be used for form parameters, allowing methods to receive more types.

2. polymorphism is used to return more data types.

Problem:

I return a circle object and cannot use the reference variable of the circle to receive it: returned graph-> the parent class of the circle

Conversion of basic data types: Big Data-> small reception (forced conversion)

1 package study; 2 3 // requirement 1: Define a method to receive any type of graphic objects, and calculate the area and perimeter. 4 // requirement 2: A method can return any type of image. 5 abstract class MyShape {6 // defines two methods to calculate the area and perimeter. 7 public abstract void getArea (); 8 public abstract void getLength (); 9} 10 11 class Circle extends MyShape {12 int r; 13 final double PI = 3.14; 14 15 // override method 16 public void getArea () {17 System. out. println ("Area of the circle"); 18} 19 public void getLength () {20 System. out. println ("circle perimeter"); 21} 22} 23 24 class Rect extends MyShape {25 26 public void getArea () {27 System. out. println ("rectangular area"); 28} 29 public void getLen Failed () {30 System. out. println ("rectangular perimeter"); 31} 32} 33 34 public class star {35 public static void main (String [] args) {36 37 // requirement 1: define a method to receive any type of graphic objects, and calculate the area and perimeter. 38 getAreaAndLength (new Circle (); 39 getAreaAndLength (new Rect (); 40 41 // requirement 2: set a method to return any type of image 42 MyShape a = getMyShape (0); // circle 43/* getAreaAndLength (); // calculate the area and perimeter of the type obtained here. */44 45 Circle c = (Circle) a; // forcibly convert the referenced data type 46 getAreaAndLength (c ); // calculate the area and perimeter of the type obtained here. 47} 48 49 public static void getAreaAndLength (MyShape MS) {// This parameter gets the object of the parent class, which can be used if it belongs to this parent class. If it is a circle, only 50 ms of the circle can be received. getArea (); 51 ms. getLength (); 52} 53 54 public static MyShape getMyShape (int I) {55 if (I = 0) {56 return new Circle (); // round 57} else if (I = 1) {58 return new Rect (); // rectangle 59} 60 return null; 61} 62}

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.