Summary of encapsulation inheritance Polymorphism

Source: Internet
Author: User
Encapsulation

Encapsulation according to my personal understanding,

  • Privatize attributes,
  • Provides the getters and setters methods,
  • Tostring Method
  • Constructor based on related business overload

Advantages:

  • Shield details
  • Improve Security

Constructor is involved in inheritance. There is no return value type for Constructor. If no parameter is specified by default, the JVM adds a constructor by default. If a constructor with a parameter is used, make sure to write the non-argument constructor, which involves method overloading.

Method Overloading

In the same class, the method name is the same and the parameter type is different (number or type ),It has nothing to do with the return value type and permission modifier;

Inheritance

  Inheritance: A class can inherit the attributes and methods of another class, And the subclass can choose to override the parent class's methods. Based on this extension, it can inherit a common class abstract class or interface; the inheritance class is implemented through extend through implements

 Abstract class

An abstract class is a class that uses Abstract modification. It contains abstract methods. Abstract METHODS Use abstract methods. Only method names have no method bodies;

Between abstract classes and abstract methods

Abstract classes may have abstract methods. If an abstract method exists in a class, the class must be an abstract class. abstract methods exist with abstract classes or interfaces.

An abstract class is also a class that has a constructor, but cannot be instantiated. There are also instance variables and instance methods.

If the subclass of an abstract class does not completely override the abstract method of the parent class, this subclass is still an abstract method,

An abstract class can only be inherited. That is to say, a subclass has only one parent class.

The final parent class (superclass, base class) of all classes is object,

  Interface:

You can use implement to implement multiple interfaces and override the abstract methods of interfaces.

  Interface features

  • The interface permission modifier is only public and default.
  • An interface can inherit multiple interfaces, but cannot inherit classes.
  • The interface cannot be a variable. Only constants can be defined. By default, public static is added.
  • The method in the interface cannot be modified using private protect static final. An error is returned.
  • In the interface after jdk1.8, you can define the instance method and use the default modification method.

Rewrite

Under the Inheritance condition, the subclass overrides the method of the parent class as required.

Rewrite requirements:

  • The override method name is consistent with that of the parent class.
  • The parameter tables are the same.
  • The return parameter type is the same or its subclass
  • The permission modifier cannot be reduced.

Modifier Problems

Abstract: attributes and constructor methods cannot be modified, and a method or class cannot be modified simultaneously with Private Static final.

The final-modified class cannot be inherited, the modifier cannot be overwritten, the modifier attribute cannot be changed because the constant cannot be changed, and the constructor cannot be modified.

Static modification methods cannot be rewritten, but can be inherited,

Private Any attribute or method modified by private can be inherited

The default permission modifier attributes or methods can be inherited, but must be within the same package range. abstract classes and interface interfaces cannot be written as public abstract objects.

Protected cannot modify abstract classes and interfaces. It can be abstract methods in abstract classes. The permission modifier in interfaces can only be public abstract

Constructor methods cannot be inherited. They can only be called by default using subclass constructor methods or using super

Polymorphism

A set of features with multiple behavior manifestations under the Inheritance Condition

Upward Transformation

  Parent Class Object = new subclass (); assign the subclass object to the parent class object in the inherited condition, and point the reference of the parent class to the subclass object,

When calling: The property calls the attributes of the parent class. If the subclass overrides the method of the parent class, it calls the subclass method. Otherwise, the method of the parent class is called, the new method of subclass cannot be called.

Downward Transformation

The premise for downward transformation is that the parent class has been transformed up; otherwise, compilation errors may occur,

Parent Class Object = new subclass ()

Subobj = object;

 

I am still studying. I try to write an article every week during my study. The above sections are my personal notes and online search. Due to the large number of learning tasks and insufficient personal language organization skills, if there is an error, please point it out, and forgive me !!

  

 

Summary of 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.