Java keyword final, abstract class, interface Introduction

Source: Internet
Author: User
Tags define abstract

1. Final keywords

Final keywords can be used to modify classes, methods, and member variables.

(1) classes marked by final cannot be inherited;

(2) The final flag method cannot be subscribe;

(3) variables marked by final become constants and can only be assigned once.

All words of the variable name marked by final must be capitalized.

2. abstract class

2.1 The definition of abstract classes must first define abstract methods. the so-called abstract method refers to a method that is declared but not implemented (that is, the method body surrounded. classes that contain one or more abstract methods are called abstract classes.

Abstract class = Common class + abstract Method

Abstract classes cannot be directly instantiated. To use an abstract class, you must have a subclass of this abstract class. if the subclass of an abstract class is not an abstract class, all abstract methods of the abstract class must be rewritten.

Abstract class person

{

// The name of a global constant must be capitalized.

Public static final string name = "newsainton ";

// The print () method has a method body, so it is not an abstract method.

Public void print ()

{

System. Out. println ("non-abstract method, name =" + name );

}

// The Fun () method does not contain the method body. It is an abstract method.

Public abstract void fun ();

}

// Class B inherits from Class A, but class B is not declared as an abstract class. You must rewrite all the abstract methods in Class.

Class student extends person

{

Public void fun ()

{

System. Out. println ("abstract method, name =" + super. Name );

}

}

Public class demo01

{

Public static void main (string ARGs [])

{

Student s = new student ();

S. Fun ();

S. Print ();

}

}

Abstract class person

{

// The name of a global constant must be capitalized.

Public static final string name = "newsainton ";

// The print () method has a method body, so it is not an abstract method.

Public void print ()

{

System. Out. println ("non-abstract method, name =" + name );

}

// The Fun () method does not contain the method body. It is an abstract method.

Public abstract void fun ();

}

// Class B inherits from Class A, but class B is not declared as an abstract class. You must rewrite all the abstract methods in Class.

Class student extends person

{

Public void fun ()

{

System. Out. println ("abstract method, name =" + super. Name );

}

}

Public class demo01

{

Public static void main (string ARGs [])

{

Student s = new student ();

S. Fun ();

S. Print ();

}

}

2.2 The abstract class can have its own constructor, but the constructor cannot directly instantiate its own object. if a constructor exists in an abstract class, you must explicitly use super ([parameter list]) in the subclass to specify which constructor of the parent class to call.

Abstract class person

{

// The name and age attributes should exist.

Private string name;

Private int age;

Public Person (){}

// If it is no longer a parameter, you must explicitly call the parameter-free construction in the subclass.

Public Person (string name, int age)

{

This. Name = Name;

This. Age = age;

}

Public String getname ()

{

Return this. Name;

}

Public int getage ()

{

Return this. Age;

}

// Define an output method, but this method is an abstract Method

Public abstract string getinfo ();

}

Class student extends person

{

Public student (string name, int age)

{

// Call the construction method with two parameters in the person class

Super (name, age );

}

Public String getinfo ()

{

Return "name =" + super. getname () + ", age =" + super. getage ();

}

}

Public class demo05

{

Public static void main (string ARGs [])

{

Student s = new student ("James", 30 );

System. Out. println (S. getinfo ());

}

}

Abstract class person

{

// The name and age attributes should exist.

Private string name;

Private int age;

Public Person (){}

// If it is no longer a parameter, you must explicitly call the parameter-free construction in the subclass.

Public Person (string name, int age)

{

This. Name = Name;

This. Age = age;

}

Public String getname ()

{

Return this. Name;

}

Public int getage ()

{

Return this. Age;

}

// Define an output method, but this method is an abstract Method

Public abstract string getinfo ();

}

Class student extends person

{

Public student (string name, int age)

{

// Call the construction method with two parameters in the person class

Super (name, age );

}

Public String getinfo ()

{

Return "name =" + super. getname () + ", age =" + super. getage ();

}

}

Public class demo05

{

Public static void main (string ARGs [])

{

Student s = new student ("James", 30 );

System. Out. println (S. getinfo ());

}

}

3. interface:

3.1 interfaces are the combination of Abstract methods and Constants

Interface definition method: interface name {data type constant name = constant value; Return Value Type method name ();.......}

In Java, a class can inherit only one class, but multiple interfaces can be implemented. if the class implementing the interface is not an abstract class, the subclass must rewrite all the abstract methods of the interface.

Interface person

{

// The Interface contains abstract classes and abstract methods.

Public static final string name = "newsainton ";

Public abstract void fun ();

}

// A class can inherit multiple interfaces. If the class is not an abstract class, all abstract methods in the abstract class must be implemented.

Class student implements person

{

Public void fun ()

{

System. Out. println ("name =" + name );

}

}

Public class demo02

{

Public static void main (string ARGs [])

{

Student s = new student ();

S. Fun ();

}

}

// The Interface contains abstract classes and abstract methods, so the code can also be written like this

Interface person

{

// The Interface contains abstract classes and abstract methods.

// Public static final string name = "newsainton ";

Public string name = "newsainton ";

// Public abstract void fun ();

Public void fun ();

}

Class student implements person

{

Public void fun ()

{

System. Out. println ("name =" + name );

}

}

Public class demo03

{

Public static void main (string ARGs [])

{

Student s = new student ();

S. Fun ();

}

}

An interface can use the extends keyword to inherit one or more existing interfaces. However, in the implementation of subclass, all abstract methods of all interfaces must be implemented.

Interface

{

Public void printa ();

}

Interface B

{

Public void printb ();

}

Interface C extends a, B

{

Public void printc ();

}

Class X implements C

{

// If the (inherited) C interface is implemented, all abstract methods must be overwritten in the subclass.

Public void printa ()

{

System. Out. println ("A --> hello ");

}

Public void printb ()

{

System. Out. println ("B --> hello ");

}

Public void printc ()

{

System. Out. println ("c --> hello ");

}

}

Public class demo04

{

Public static void main (string ARGs [])

{

X = new x ();

X. printa ();

X. printb ();

X. printc ();

}

}

3.2 If a subclass inherits an abstract class and implements an interface, how can it be written?

The syntax format is as follows: Class class name extends abstract class implements interface.

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.