In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is the state and abstract class of Java". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "what is the state and abstract class of Java".
Polymorphism:
We know the three main features of Java: encapsulation, inheritance, and polymorphism. The first two have been mentioned in the introduction to Java (6), and now we are going to talk about the feature of polymorphism.
What is polymorphism?
As the name implies, polymorphism means many forms.
The meaning of polymorphism in Java:
1. Send a message to an object and let the object decide which behavior to use in response to the message
two。 The reference of the subclass object is assigned to the parent class reference variable to implement dynamic method invocation
The prerequisite for the formation of polymorphism in Java:
1. Inherit
two。 Overrides of parent methods
3. Upward transformation
My explanation for polymorphism:
For example, we are people, students and young people. I can do things as people, buy student tickets as students, and do public welfare as young people. in this way, we can do different things through different forms. Isn't that easier to understand?
Note:
Polymorphic prerequisite: there must be a child-parent relationship.
When a method is called using a polymorphic parent class reference variable, the overridden method of the subclass is called.
The definition and usage format of polymorphism:
Parent class type variable name = new subclass type ()
Characteristics of members in polymorphism:
Polymorphic member variables: compile and run look to the left
Polymorphic member method: compile to the left, run to the right
The transformation of polymorphism:
The transformation of polymorphism can be divided into upward transition and downward transition.
Upward transition: polymorphism itself is a process of upward transformation.
Use format: parent class type variable name = new subclass type ()
Applicable scenario: when you do not need to face the subclass type, you can complete the corresponding operation by improving the extensibility or using the function of the parent class.
Downward transition: a subclass object that has been transformed upward can use the format of forced type conversion to change the parent class reference type to the subclass reference type.
Use format: subclass type variable name = (subclass type) variables of parent class type
Applicable scenarios: when you want to use subclass-specific features.
Code interpretation:
Public class Person {/ / Human, as a parent class, uses public void speak () {System.out.println ("We are all one person");}} public class Student extends Person {/ / inherits the parent class, which means that we are students, someone's method @ Override public void speak () {System.out.println ("I am a student of mankind") }} public class Child extends Person {/ / inherits the parent class, which is equivalent to the behavior of the child @ Override public void speak () {System.out.println ("I am the child of mankind");}} / / Test class public class TestMain {public static void main (String [] args) {/ / parent type variable name = new subclass type (); Person p = child () / / the reference to the subclass object is assigned to the parent class p.speak (); / / Polymorphism is equivalent to the Student method used here. Output I am a student of human beings / / as a human being, I use my identity as a student to say: I am a student of human beings Person p = new Child (); p.speak () / / output: I am a child of human beings / / as a human being, using my identity as a child to say that I am a child of human beings} / / this code, we use inheritance, rewriting, upward transformation, because polymorphism is a process of upward transformation.
The role of polymorphism: after introducing polymorphism, what is the use of talking about polymorphism? Why polymorphism can be one of the three characteristics of Java. There must be a reason:
Improve the reusability of code
Reduce the coupling between modules
What is the polymorphic mechanism here? How does the Java language implement polymorphism? (it may be a little difficult to understand, and I don't have a good understanding of the content, but this is also the question of the HKCEE.)
The so-called polymorphism means that the specific type pointed to by the reference variable defined in the program and the method call made through the reference variable are not determined during programming, but are determined during the run of the program, that is, a reference variable will point to the instance object of which class, and the method call issued by the reference variable must be decided during the run of the program. Because the specific class is determined only when the program is running, so that the reference variable can be bound to a variety of different class implementations without modifying the source code, resulting in a change in the specific method of the reference call, that is, the specific code bound by the program can be changed without modifying the program code, so that the program can choose multiple running states, which is polymorphism. Polymorphism is divided into compile-time polymorphism and run-time polymorphism. Among them, polymorphism is static when editing, which mainly refers to the overloading of the method. It distinguishes different functions according to the different parameter list. After editing, it will become two different functions, not to mention polymorphism at run time. Run-time polymorphism is dynamic, and it is achieved through dynamic binding, which is what we call polymorphism.
You can also refer to a piece of code for the understanding of polymorphism:
Class People {/ / parent public void eat () {System.out.println ("We will eat");} class Student extends People {/ / inherit parent @ Override public void eat () {System.out.println ("I will eat meat");} public void study () {System.out.println ("We need to study hard") }} class Teacher extends People {/ / inherit parent class @ Override public void eat () {System.out.println ("the teacher will eat vegetables");} public void teach () {System.out.println ("the teacher should teach carefully");} / / Test class: public class TestMain {public static void main (String [] args) {People p=new Stu () / / the reference of the subclass object is assigned to the parent class p.eat (); / / output: I will eat meat}} abstract class:
What is an abstract class?
The ordinary class is a perfect functional class, which can generate instantiated objects directly, and can contain constructors, common methods, static methods, constants and variables in the ordinary class. The abstract class refers to adding the components of abstract methods to the structure of the ordinary class.
Abstract method:
There is a "{}" on all common methods, which represents the method body, and the method with the method body must be used directly by the object. An abstract method refers to a method without a method body, and the abstract method must be modified with the keyword abstract. In other words, abstract methods in an abstract class can be rewritten after being inherited.
Abstract class declaration keyword: abstracat
Define an abstract class:
Public abstract class studnet {/ / defines an abstract class public void study () {/ / ordinary method System.out.println ("I'll learn");} public abstract void eat (); / / Abstract method, no method body, modified by abstract keyword} / Note: if there are abstract methods, this class must be abstract!
Example explanation: shape class Shape needs to provide methods for calculating area and perimeter, but the shape itself has not been determined, so the method of calculating perimeter and area can not be determined, so we need to use abstract classes and abstract methods.
Since the method of calculating the perimeter and area of the Shape class cannot be determined, such a method can be declared abstract so that it can be implemented in a concrete subclass.
/ / define a shape class, but there is no concrete shape, so we define the abstract class public abstract class Shape {private int a; public abstract void area () {} / / the method public abstract void perimeter () for finding the area; / / the method for finding the perimeter public Shape () {/ / no-parameter construction} public Shape (int a) {this.a = a }} / / subclass public abstract class shape {/ / A class with abstract methods must be an abstract class, which requires the keyword abstract to modify private int a; public abstract void area (); / / the method of calculating the area, but no method body, requires the keyword abstract to modify public abstract void perimeter () / / method for calculating perimeter} / / create a subclass that calculates the area and perimeter of a circle, inherit the abstract class shape, and rewrite the method public class Circle extends shape {public static double pi = 3.14; private double r; / / radius @ Override public void area () {System.out.println ("area of the circle:" + Circle.pi*this.r*this.r) in shape. } @ Override public void perimeter () {System.out.println ("the circumference of the circle is:" + 2*Circle.pi*this.r);} public Circle () {} public Circle (double r) {/ / this.r = r;} / / Test class: public class TestMain {public static void main (String [] args) {Circle c = new Circle (5) / / input radius: 5 c.area (); c.perimeter ();}} / / output result: area of the circle: 78.5pm / perimeter of the circle: 31.4000000000002
Considerations for abstract methods and abstract classes:
There can be constructors in the abstract class, and the writing of the constructor is no different from other classes, but it really runs because of the super call of the subclass constructor, after all, we can't new an abstract class object, we can only give the constructor of the abstract class to the constructor of the subclass to use.
Abstract classes do not necessarily contain abstract methods, but classes that have abstract methods must be abstract classes.
The subclass of an abstract class must rewrite all abstract methods in the parent class. If there is an abstract method that is not overwritten, a compilation error will occur. At this time, the subclass can also be declared as an abstract class, and the error will disappear.
Concise summary:
Abstract method: a method without a method body and requires the keyword abstract to modify
A class with an abstract method must be an abstract class, which needs to be modified by the keyword abstract
Abstract methods cannot be decorated with private and static
Abstract classes, classes that do not necessarily have abstract methods
Abstract classes, classes that do not necessarily have abstract methods
Abstract classes can have constructors
What is the difference between a normal class and an abstract class?
An ordinary class cannot contain abstract methods, and an abstract class can contain abstract methods
Abstract classes cannot be instantiated directly, while ordinary classes can be instantiated directly.
The above is all the content of the article "what are the states and abstract classes of Java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.