


JAVA object-oriented inheritance, construction methods, overwriting and overloading (summary sharing)
This article brings you relevant knowledge about java, which mainly introduces related issues about object-oriented, including parameterized construction, parameterless construction, class inheritance, and method Let’s take a look at rewriting, reloading, etc., I hope it will be helpful to everyone.
java tutorial"
Construction method
The construction method of a class is a special method. The name of this method must be consistent with the class name. The construction cannot have a return value, does not use void, and cannot be called directly. It is automatically called when the class object is instantiated, and can be called when new. General constructors are used for initialization when instantiating class objects. If a class does not have a constructor written, the system automatically adds a no-argument constructor to this class during compilation. If the declaring class has written a constructor, the system will no longer add a parameterless constructor.It is recommended that after writing the constructor, it is best to write a parameterless constructor.
No-parameter construction
To put it bluntly, it is a construction method without parametersIf you don’t understand the concept, it will be clear with an example .
Declare a class Apublic class A { }
Create a constructor method A() in class A, and print out a sentenceThe constructor must be the same as the class name
public class A { public A(){ System.out.println("构造方法A()"); }}
Create a new test class and nuw an object of class A in the classpublic class Test { public static void main(String[] args) { A a = new A(); }}
Execute the main method and try it
The construction method is equivalent to the method that is automatically used in the new objectConstruction with parameters
Construction with parameters is Constructor with parametersDeclare a constructor with parameters in class A and pass in two String type parameters, a and b
public class A { public A() { } public A(String a, String b) { System.out.println(a + b); }}
New one in the test class Pass in the parameters when object apublic class Test { public static void main(String[] args) { A a = new A("aaa", "bbb"); }}
Execute the main methodNote
There is no constructor When , there is a parameterless construct hidden in the class. But when you create a parameterized constructor, the hidden parameterless constructor disappears. Then the new object can only take parameters in the future. Therefore, when building a parameterized constructor, you must create a parameterless constructor and put it there.Inheritance of classes
In the Java language, a class class has a single inheritance and multiple implementation interfaces. Interface interface is multiple inheritance.
Why should we inherit? Because the subclass wants to add new features based on the parent class.Subclasses inherit the methods and attributes of the parent class.
The following is an example analysis: The parent class is a person, and the subclass is an experienced driver.
Human characteristics include: two hands, two eyes, two feet, the ability to eat and drink
The old driver has inherited the characteristics of humans, and has the added function of being able to drive.
The code is as follows:
This is a human being with the following attributes and functions.public class Ren { public final String shou = "两只手"; public final String jiao = "两只脚"; public final String yan = "两只眼"; public void chi() { System.out.println("会吃"); } public void he() { System.out.println("会喝"); }}
This is the veteran class, which inherits from humans. And there is an additional function that can drivepublic class Siji extends Ren { public void kai() { System.out.println("会开车"); }}
Test: Create the output attribute of the old driver object and call the method.public class Test { public static void main(String[] args) { Siji b = new Siji(); System.out.println(b.jiao); System.out.println(b.shou); System.out.println(b.yan); b.chi(); b.he(); b.kai(); }}
Rewriting Override: The method name, return type, and formal parameters are all the same. If this is the case, it must be an inheritance relationship.RewriteOverloading: Methods with the same method name, return type, number of formal parameters, and different types do not necessarily have to be inheritance relationships. They can be in the same class, such as constructor method overloading
Because the subclass is not satisfied with the method of the parent class and wants to change it, this is how the subclass overrides the method of the parent class.The code is as follows:For example, a person can eat and drink, but an experienced driver doesn’t like the feature of eating and wants to change it to eat shit.
Old driver rewrites the human chi() methodThe method name must be the same
public class Siji extends Ren { public void kai() { System.out.println("会开车"); } public void chi() { System.out.println("会吃屎"); }}
Now rewrite the running test class
Overloading
Construction with parameters and construction without parameters are method overloading. There are two methods with the same name in a class, but their return value types and parameter types are The number of parameters is different.
Only one of the return value type, parameter type, and number of parameters of the two methods is different. But their method names are the same, so the two methods are overloaded.
Recommended study: "java tutorial"
The above is the detailed content of JAVA object-oriented inheritance, construction methods, overwriting and overloading (summary sharing). For more information, please follow other related articles on the PHP Chinese website!

The class loader ensures the consistency and compatibility of Java programs on different platforms through unified class file format, dynamic loading, parent delegation model and platform-independent bytecode, and achieves platform independence.

The code generated by the Java compiler is platform-independent, but the code that is ultimately executed is platform-specific. 1. Java source code is compiled into platform-independent bytecode. 2. The JVM converts bytecode into machine code for a specific platform, ensuring cross-platform operation but performance may be different.

Multithreading is important in modern programming because it can improve program responsiveness and resource utilization and handle complex concurrent tasks. JVM ensures the consistency and efficiency of multithreads on different operating systems through thread mapping, scheduling mechanism and synchronization lock mechanism.

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.