Java learning notes-15. Object-Oriented Programming 10-Final keywords in Java, abstract classes and interfaces

Source: Internet
Author: User
Tags define abstract


Abstract classes and interfaces are one of the most important parts of Java. Here we use a large amount of space for this part of notes.

1. Final keywords

In Java, you can use the final keyword to modify classes, methods, and member variables.

(1) the class marked by final cannot be inherited;

(2) The final marking method cannot be subscribe;

(3) The final variable becomes a constant and can only be assigned once.

Note: If you use final to declare constants, follow the general rules below: variable names marked by final, all words and letters must be capitalized.

2. abstract class

 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

 For abstract classes, remember that 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.

 As shown below:CodeExample: 

Abstract class person {// The name of a global constant must be capitalized. Public static final string name = "newsainton"; // print () has a method body, so it is not the abstract method public void print () {system. out. println ("non-abstract method, name =" + name);} // The Fun () method does not contain the method body. It is the abstract method public abstract void fun ();} // Class B inherits from Class A, but class B is not declared as an abstract class. You must re-write all the abstract methods in Class A, 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 ();}}

Another question to consider is: can an abstract class have its own constructor?

 The answer is: an abstract class can have its own constructor. However, this 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.

Here is an example: 

 
Abstract class person {// The name and age attributes should be private string name; private int age; Public Person () {}// if no parameter is specified, you must explicitly call the non-argument construction public person (string name, int age) {This. name = Name; this. age = age;} Public String getname () {return this. name;} public int getage () {return this. age ;}// defines an output method, but this method is an abstract method public abstract string getinfo ();} class student extends person {public student (string name, int age) {// There are two constructors for calling 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 concepts of interfaces

 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.

 Example code: 

 
Interface person {// The Interface contains the abstract class and the abstract method public static final string name = "newsainton"; public abstract void fun ();} // a class can inherit multiple interfaces. If the class is not an abstract class, you must implement all the abstract methods in the abstract 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 ();}}

 3.2 two points of attention for interfaces

 (1 ). Abstract classes use the extends keyword, indicating that a class can only inherit one parent class, but the interface uses implements. A class can implement multiple interfaces at the same time, however, the subclass must overwrite the abstract methods in multiple interfaces at the same time.

 (2). Since the definition clearly states that the interface is a set of abstract methods and global variables, we can simplify the Code as follows: 

 
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 ();}}

3.3 relationship between interfaces

 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.

 

An example of how to inherit an interface and implement multiple interfaces is as follows: 

 
Interface A {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 public void printa () {system must be overwritten in the subclass. 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.4 Here, if a subclass inherits an abstract class and implements an interface, how can it be written?

 What we adopt is: Class class name extends abstract class implements Interface This syntax format.

The following is an example that inherits an abstract class and implements an interface: 

Interface X {public void printx ();} interface Z {public void Printz ();} // an abstract class y implements Z {public abstract void printy ();} // Class D inherits both class Y and Class D extends y implements X {public void printx () {system. out. println ("X --> hello. ");} public void printy () {system. out. println ("y --> hello. ");} public void Printz () {system. out. println ("z --> hello. ") ;}} public class demo13 {public static void main (string ARGs []) {d = new D (); D. printx (); D. printy (); D. printz ();}}

 

 

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.