Three main object-oriented features in Java: Encapsulation, inheritance, polymorphism, and keyword instanceof

Source: Internet
Author: User

Three main object-oriented features in Java: Encapsulation, inheritance, polymorphism, and keyword instanceof

1. Package:

Using the private keyword, so that the outside world can not directly access the properties of the class;

Provide setter and Getter methods for setup and acquisition;

Benefits: Enhance the security of the program, so that the outside world can not access directly, but also to set the properties of the input restrictions;

public class Anli1 {
private String name;
Private String sex;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
}

The attributes in the class name and sex sex are encapsulated and cannot be accessed directly in other classes;

If you want to set a gender setting only male and female can restrict in the Setsex () method;

public void Setsex (String sex) {
if (Sex.equals ("male") | | Sex.equals ("female")) {
This.sex = sex;
}else{
SYSTEM.OUT.PRINTLN ("You have set a gender error");
}
}

Test in main;

public static void Main (string[] args) {
Anli1 a1=new anli1 ();
Anli1 a2=new anli1 ();
A1.setsex ("female");
A1.setname ("A1");
System.out.println (A1.getname () +a1.getsex ());

A2.setsex ("female");
A2.setname ("A2");
System.out.println (A2.getname () +a2.getsex ());
}

Results:

You set the gender error
A1null
A2 Women

From the output results can be seen A1 set sex for Female, Setup failed, while A2 set sex for NV succeeded;

This makes it easy to enter restrictions, although setting other restrictions is the same;

2. Inheritance: Extends

As the name implies: the inheritance of properties and methods in the parent class:

Function: Reduce the bloated code, improve the reusability of the Code;

Note: A, cannot inherit the constructor method in the parent class, B, cannot inherit the private property and method in the parent class;

Create a new anli2 inherit the above Case 1, and create a new unique method inside;

public class Anli2 extends anli1{

public void Look () {
System.out.println ("I'll Be the Janitor");
}

}

Test in Main:

Anli2 b1=new anli2 ();
B1.setname ("B1");
B1.setsex ("male");
System.out.println (B1.getname () +b1.getsex ());

Results:

B1 Male

Subclasses can invoke properties and methods of the parent class except private;

3. polymorphic

Multiple states of a thing are called polymorphism. For example, water has three forms of gaseous liquid and solid state;

In a class; Because the subclass inherits the parent class all also makes a thing have many States;

For example, Anli2 inherited Anli1, changed anli1 to animal animal anli2 for dog dogs.

It can be said that the dog can be a dog, can also be said to be an animal, there are two kinds of statements;

There are two types of polymorphic forms:

A, upward transformation: The object of the class to refer to the parent class reference type;

Animal A1=new Dog ();

But when a dog is an animal, it is not able to call the dog's unique method look () If you need to use a method that is unique to the parent class, you need to transform downward;

B, down transformation: Convert a reference to a parent class to a reference to a subclass

Animal A1=new Dog ();//Upward transformation
Dog b2= (dog) a1;//downward transition
B2.look ();//Sub-class unique methods

Note that the downward transition must first be transformed, and the parent's reference cannot be directed to the child class;

Function: Can decouple, reduce coupling; what is coupling? is not it, just like our mobile phone if the charger is broken, if only with the factory original charger,

Then the practicability is too bad, does not meet the demand;

Decoupling in the application of the class: if the parent class has more than one child class, if you do not know which subclass is the case, you can set the passed parameter to the parent class, then all its subclasses can be passed into the parameters,

Here to use the upward transformation, the general combination of instanceof keyword use;

4. instanceof

Function: Used to determine whether the incoming reference type belongs to a given type, generally to judge the transformation of the parent class;

Example: Create a new service class, set up a method, pass in the parameter animal

public class Service {
public void Fun (animal a) {//Parameter up transformation
if (a instanceof dog) {//Determine if the passed in parameter is a dog
(dog) a). Look ();//Downward transition
}
}
}

In the main method, test:

Animal A1=new Dog ();
Service s=new service ();
S.fun (A1);

Operation Result:

I'll be the janitor.

Three main object-oriented features in Java: Encapsulation, inheritance, polymorphism, and keyword instanceof

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.