Keywords in Java

Source: Internet
Author: User

Inheritance Keywords

Inheritance can use the two keywords extends and implements to implement inheritance, and all classes inherit from Java.lang.Object, and when a class does not inherit two keywords, the default inherits Object (this class is in the Java.lang in the package, so no importis required) ancestor classes.

Extends keywords

In Java, the inheritance of a class is a single inheritance, that is, a subclass can only have one parent class, so extends can inherit only one class.

Public Class Animal {  "

< Span class= "Hl-code" >< Span class= "hl-comment" > public class Span class= "Hl-identifier" >penguin extends animal{ }

Implements keywords

The use of the Implements keyword can be disguised to make Java has multiple inheritance, using the scope of the class to inherit the interface, you can inherit multiple interfaces (interfaces and interfaces separated by commas).

Public Interface A { }

< Span class= "hl-brackets" > public interface b {  "

< Span class= "hl-reserved" > public class c implements Ab { "

Super and this keyword

Super Keyword: We can use the Super keyword to implement access to a parent class member to refer to the parent class of the current object.

This keyword: point to your own reference.

Final keyword

The final keyword declares that a class can be defined as a class that cannot be inherited, that is, a final class, or for a decorated method that cannot be overridden by a quilt class, that an instance variable can be defined as final, and that a variable defined as final cannot be modified. A method declared as a final class is automatically declared final, but the instance variable is not final

    • Declaring a class:

      Final class class name {//class body}   
    • Declaration method:

      Modifier (Public/private/default/protected)final return value type method name () { //Method body}           
Constructors

A subclass cannot inherit the constructor (constructor or constructor) of the parent class, but the constructor of the parent class has parameters, and the constructor of the parent class must be explicitly called through the Super keyword in the constructor of the child class with the appropriate argument list. If the parent class has a parameterless constructor, it is not necessary to call the parent class constructor with super in the constructor of the subclass, and if the Super keyword is not used, the system automatically calls the parent class's parameterless constructor

Keywords in Java

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.