


How to use inheritance, constructor methods, overriding and overloading methods in JAVA?
Constructor method
The constructor method of the class is a special method, this method name It 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 parameters
If you don’t understand the concept, it will be clear with an example .
Declare a class A
public class A { }
Create a constructor method A() in class A, and print out a sentence
The 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 class
public 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 object
Construction with parameters
Construction with parameters is Constructor with parameters
Declare 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 a
public class Test { public static void main(String[] args) { A a = new A("aaa", "bbb"); }}
Execute the main method
Note
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 drive
public 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(); }}
Method rewriting, overloading
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.
Overloading: 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
Rewrite
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.
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.
The code is as follows:
Old driver rewrites the human chi() method
The 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. In a class, there are Two methods with the same name, but their return value types, parameter types, and number of parameters are 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.
The above is the detailed content of How to use inheritance, constructor methods, overriding and overloading methods in JAVA?. 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

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

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











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

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
