JAVA Programming Thought Notes: Reuse Classes
Reusing code is one of Java's many compelling features. But for a language to be revolutionary, it's not enough to just be able to copy code and change it. It must be able to do more.
Composition syntax
Just place the object reference in the new class.
Initialize the reference Position
Where objects are defined, they can always be initialized before the constructor is called.
In the constructor of a class
Just before the objects are used, --- Lazy initialization
Instance initialization
Inheritance syntax
Unless it is explicitly stated that you want to inherit from other classes, you are implicitly inheriting from Java's standard root class Object is inherited.
Initialize base class
Proxy
The Java language does not directly support proxies. Many development tools do. .
Choose between collections and inheritance
Both composition and inheritance allow placing sub-objects in a new class. Composition is done explicitly, inheritance is Do it implicitly.
protected keyword
is private as far as the class user is concerned, for any exported class that inherits this class or any other class in the same class. For the classes in the package, he is accessible.
Upward transformation
The exported class you exported is converted to accumulation and moves upward on the inheritance graph, becoming Upcasting.
Upcasting is a conversion from a more specialized type to a more general type, so it is always safe.
final keyword
Cannot be changed
final data
A domain that is both static and final only occupies a storage space that cannot be changed.
For basic types, final makes the value constant. For object references, final makes the reference constant.
Once a reference is initialized to point to an object, it cannot be changed to point to another object. However, the object itself can be modified.
Java does not provide a way to make any object immutable.
This restriction also applies to arrays, which are also objects.
By convention, fields that are both static and final will be capitalized, with underscores separating the words.
Blank final
The so-called blank final means that it is declared A field that is final but has no initial value given.
In any case, the compiler ensures that a blank final must be initialized before use.
final parameters
Allows the parameter to be declared final in the parameter list. This means that you cannot change the object pointed to by the parameter reference in the method.
final method
1. Lock the method to prevent any inherited class from modifying its meaning.
2. Efficiency, in early implementation, if you specify a method as final, you agree that the compiler will target the method. All calls to methods are converted to inline calls.
final and private keywords
All private methods in a class are implicitly designated as final.
Since the private method cannot be used, it cannot be overridden.
You can add the final modifier to the private method, but it does not add any additional meaning to the method.
"Override" will only appear when a method is part of the interface of the base class.
Must upcast an object to her basic type and call the same method.
final class
is prohibited from being inherited and has no subclasses.
All methods in all final classes are implicitly designated as final and cannot be overridden.
Initialization and class loading
class Insect { private int i = 9 ; protected int j ; Insect(){ System.out.println("Insect constructor."); System.out.println("i= "+i+", j="+j); j = 39 ; } static int printInit(String s){ System.out.println(s); return 47 ; } } public class Beetle extends Insect { private int k = printInit("Beetle.k initialized . "); public Beetle(){ System.out.println("k = "+ k); System.out.println("j = " + j); } private static int x2 = printInit("static Beetle.x2 initialized ") ; public static void main(String[] args) { System.out.println("Beetle constructor."); Beetle b = new Beetle(); } } 输出: static Beetle.x2 initialized Beetle constructor. Insect constructor. i= 9, j=0 Beetle.k initialized . k = 47 j = 39
The above is the detailed content of JAVA Programming Thought Notes: Reuse Classes. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
