Brief introduction to Java object-oriented three features: encapsulation, inheritance, polymorphism

Source: Internet
Author: User

Package:
    • The whole attribute and service of the object are combined to form an indivisible independent unit;
    • Hide internal details as much as possible. (data hiding, hiding details of specific implementations)
Public, private, protected, default
    • Public: can be accessed across classes or across packages (package)
    • Private: can only be accessed by such objects, subclasses cannot be accessed, and cannot be accessed across packages
    • Protected: can only be accessed by the class itself and subclasses, even if the subclass is not in the package
    • Default: No modifiers are added, only one package is allowed to be visited
Inheritance:1. is a relationship (eg: Manager is a staff)
    • The initialization subclass must first initialize the parent class (virtual machine AutoComplete) because the constructor method of the parent class is called in the constructor of the child class
    • Single inheritance: A subclass can inherit only one father
    • Keyword super: Used to refer to your own parent class, such as the parent class's methods and properties are accessible
2. Method overrides
    • The return type, method name, and argument list must be the same as the method it overrides
    • Access permissions cannot be reduced
    • Cannot throw more exceptions
3. Method overloading
    • Same method (including parent class), method name is the same, parameter list is different, return type can be different
Polymorphic :
    • Simply speaking, a type conversion in a child parent class (interface or implementation class)
1. Attributes: Attributes are not polymorphic, and when the same attribute is present in a subclass, the reference to the left will prevail. class a{int num = 3;} Class B extends a{int num = 5;}  main{a = new B ();  SOP (A.num);  Output 3 b b = new B ();  SOP (B.num); Output 5} 2. Non-static private function features:
    • Compile time: The reference variable belongs to the class (parent class) whether there is a calling method, if there is a compilation pass, if there is no compilation failure.
    • Run time: If the subclass is a parent method, run the method in the subclass, if the parent class method is not running
3. Static function Features:
    • As with properties, the static method is not polymorphic, regardless of whether compiling or running is referred to the left.
4.instanceof operators:
    • The preceding is a reference type variable that is followed by a class that determines whether the preceding object is an instance of the following class, and if it returns True, ClassCastException is avoided unless it returns false.

Public interface Animal {
public abstract void Eat ();
}

public class Cat implements animal{
public void Eat () {
System.out.println ("Chi Yu");
}
public void Catchmouse () {
System.out.println ("Zhu Lao Shu");
}
}

public class Dog implements animal{
public void Eat () {
System.out.println ("Chi gu tou");
}
public void Kanhouse () {
System.out.println ("Kan men");
}
}

public class Test {
public static void Main (string[] args) {
Animal a = new Cat ();
function (a);
}

public static void function (Animal a) {
A.eat ();
if (a instanceof Cat) {
Cat C = (cat) A;
C.catchmouse ();
}else if (a instanceof Dog) {
Dog d = (dog) A;
D.kanhouse ();
}
}
}

Brief introduction to Java object-oriented three features: encapsulation, inheritance, polymorphism

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.