Three main features of Java object-oriented

Source: Internet
Author: User
Tags modifiers

Three main features of Java object-oriented: inheritance, encapsulation, polymorphism

Inherited

Origin of the inheritance

Using an is a relationship to determine inheritance

Implementations of inheritance in Java

Keyword: extends

Instantiating child objects

Single inheritance and multiple inheritance

Subclasses inherit the parent class, and the parent class can also inherit from the previous class, known as transitive inheritance.

Dog extends Animal;

Animal extends Shengwu;

Root class of all classes object

That is, the object class is the parent class of all classes, and all methods in object can be called directly by other classes.

Method overrides

Parent class method public void Jiao () {system.out.print ("called")}

Subclass method public void Jiao () {system.out.print ("barking")}

Super keyword

Super. Parent Class Property

this keyword

This. Current Class properties

Final keyword

The class that is modified by final cannot be inherited; The modified method cannot be overridden

Instantiation process

Calling the constructor of the parent class

Packaging

Access modifiers

Public: can be called in any place

protected Protected: can be accessed in the consent package, or can be invoked in subclasses of a non-sibling class

Default defaults: Can be used in the same package, do not write out modifiers

Private: Only the current class can be used

Encapsulation can improve the reusability of code, improve the portability of code, can hide implementation details

The decorated class can only be public or default

Understanding static Members

static statically.

Static methods cannot access instance members

Static initialization blocks

static{

Statement body;

}

Instance initialization block

A static block is executed only once when the object is instantiated, so it can be used to give the initial value of the object

Javabean Principle: Entity class properties typically use private adornments to set and get data through set and get functions.

Polymorphic

Automatic conversion upward transformation For example: Animal dog=new dog ();

The reference to the parent class object points to an instance of the subclass object, and the properties and methods that are more specific to the subclass are invalid.

If the child class overrides a method of the parent class, the method of the subclass is called.

Downward transformation, type restore for example: Animal animal=new Dog ();

Subclass-specific properties and methods restore the Dog dog= (dog) animal;

instanceof Animal instanceof Dog

In polymorphism, subclasses can invoke all methods of the parent class (except for the parent class private methods).

Abstract abstraction, does not have the actual function implementation, to standardize the subclass, can only modify the method, the abstract method can only be defined in the abstract class, the abstract class cannot be instantiated. Subclasses must implement this method, that is, subclasses must override the abstract methods of the parent class.

Public abstract class animal{code body}

Interface

Interfaces are abstract methods, and interfaces must be abstract classes

Interface declaration

Public Interface interface name {}

Interface implementation

Interfaces need to redefine a class to implement the method inside

public class class name implements interface name

To implement an interface if it is a subclass:

public class subclass name extends parent class name implements interface name {}

Interface applications

Interfaces can be used to expose the behavior of a class, allowing the user of a class to use the methods in the interface through the implementation of the interface.

An interface can be used to force a class to have some behavior.

Declaring member variables in an interface

An interface can contain member variables, but member variables can only be decorated with static and final keywords. Because the interface cannot be instantiated, the member variable must be declared static, and final is declared for the program's reliability.

The final property must be assigned when declared.

Interface inheritance

The inheritance of the interface is also extends by the keyword

Interfaces can have multiple at the same time. Implements interface 1, interface 2 ... Interface N

Interface and polymorphism

The difference between an interface and an abstract class

Neither of them can be instantiated for implementation or inheritance by other classes.

Both can contain abstract methods, and subclasses that implement interfaces or inherit abstract classes must implement these abstract methods.

The difference: An interface can contain only abstract methods, whereas an abstract class can contain ordinary methods.

Interfaces cannot define static methods, and abstract classes can be defined.

An interface can only define static constant properties and cannot define normal properties. Abstract classes can define normal properties, or they can define static constant properties.

The interface does not contain constructors. The constructors inside the abstract class do the initialization work.

The initialization block is not included in the interface. Abstract classes can contain initialization blocks.

A class has at most one direct parent class, including abstract classes, but a class can implement multiple interfaces directly by implementing multiple interfaces to compensate for the lack of Java single inheritance.

Three main features of Java object-oriented

Related Article

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.