The difference between overloading, inheritance, overriding and polymorphism in Java

The difference between overloading, inheritance, overriding and polymorphism: 1) Inheritance is when a subclass obtains members of the parent class. 2) Overriding is a method of reimplementing the parent class after inheritance. 3) Overloading is a series of methods with different parameters and the same name in a class. 4) Polymorphism is to avoid a large number of overloading in the parent class, which will cause the code to be bloated and difficult to maintain. An interesting statement I saw on the Internet is: inheritance is a method for subclasses to use parent classes, while polymorphism is a method for parent classes to use subclasses. The following examples include these four implementations: class Triangle extends Shape { public int getSides() { Return 3; } } class Rectangle extends Shape { public int getSides(int i) { return i; } } public class Shape { public boolean isSharp(){ return true; } public int getSides(){ return 0 ; } public int getSides(Triangle tri){ return 3 ; } public int getSides(Rectangle rec){ return 4 ; } public static void main(String[] args) { Triangle tri = new Triangle(); System.out.println(“Triangle is a type of sharp? ” + tri.isSharp()); Shape shape = new Triangle(); System.out.println(“My shape has…

Javascript is based on the three major characteristics of objects (encapsulation, inheritance, polymorphism)

The three major object-based characteristics of Javascript are the same as the three major object-oriented characteristics of C++ and Java, which are encapsulation, inheritance and polymorphism. It’s just that the implementation methods are different, but the basic concept is almost the same. In fact, in addition to the three major characteristics, there is another common characteristic called abstract, which is why we sometimes see the four major characteristics of object-oriented in some books. 1. Encapsulation Encapsulation is to encapsulate abstracted data and operations on the data together. The data is protected internally, and other parts of the program can only operate on the data through authorized operations (member methods). Case: PS: JS encapsulation has only two states, one is public and the other is private. The difference between adding member methods through constructors and adding member methods through prototype methods 1. Functions allocated through the prototype method are shared by all objects. 2. The attributes assigned through the prototype method are independent. (If you do not modify the attributes, they are shared) 3. It is recommended that if we want all objects to use the same function, it is best to use the prototype method to add the function, which…

Understanding the three major characteristics of Java encapsulation, inheritance, and polymorphism

First of all, let’s briefly talk about the definitions of its three major characteristics: Encapsulation: Hide the properties and implementation details of the object, only expose the interface to the outside world, and control the access level for reading and modifying properties in the program. Combining the abstracted data and behaviors (or functions) to form an organic whole, that is, organically combining the data with the source code that operates the data to form a “class”, in which data and functions are both members of the class. The purpose of encapsulation is to enhance security and simplify programming. Users do not need to understand the specific implementation details, but only need to use the members of the class through the external interface and a specific access permission. The basic requirements for encapsulation are: privatize all properties and provide getter and setter methods for each property. If there is a constructor with parameters, you must write a constructor without parameters. During development, it is often necessary to test the classes that have been written, so sometimes the toString method is overridden, but this is not necessary. Inheritance: Code reuse is achieved through inheritance. All classes in Java are obtained by directly…

In-depth understanding of Java encapsulation, inheritance, and polymorphism

Please indicate the original address for reprinting: https://www.cnblogs.com/ygj0930/p/10830957.html 1: Encapsulation Hide some information of the class inside the class and do not allow direct access by external programs. Instead,the operations and access to the hidden information are implemented through the methods provided by the class. Common implementation methods are: getter, setter. Encapsulation follows the “open-close principle” and prohibits external direct access and modification of class information. 1. Class variables, member variables and local variables Class variables (static variables): Variables modified with static are called static variables, and their contents are shared by all objects of the class. The values ​​of this class variable in all objects point to the same memory, any object that modifies the value of the memory here will affect other objects. Member variables: Variables declared when the class is defined are established with the creation of the object and disappear with the disappearance of the object. They exist in the heap memory where the object is located. Local variables: Variables declared in a function are only defined in the local scope and are only valid in the area to which they belong. Exists in stack memory, the scope of effect ends, the stack frame is released,…

How to understand encapsulation, inheritance, and polymorphism in JAVA?

How to understand encapsulation, inheritance, and polymorphism in JAVA? Encapsulation, inheritance, and polymorphism are relatively abstract and are just concepts. You don’t need to delve too deeply into it when you first start learning. It’s a bit far-fetched to understand these things just by learning JAVA. All you have to do is at least see the code to know what is used. The concept of packaging is like a car. When you learn to drive, you only need to learn to step on the accelerator, brake, and turn the steering wheel. There is no need to understand how the engine starts. Inheritance, let me first talk about my understanding of classes. The functions of classes are: classification (the objects you create using a certain class are actually cases of that class) and the functions of templates. Then inheritance is It plays the role of reclassifying classes. For example, there is a class “Animal”, “Mammal” inherits “Animal”, and then “Horse” inherits the class “Mammal”. Here, we talk from bottom to top. First, we divide something into something called “horse” (and of course “cow”, “fish”, etc.), and then we find , “horse”, “sheep”, etc. also have many common characteristics, so we once…

Three major characteristics of Java object-oriented: encapsulation, inheritance, polymorphism

1. Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism 2. Packaging: 1. Encapsulation has two major characteristics: reasonably hiding data and reasonably exposing data. 2. Modify the data with private. You can expose the data by defining a public method. 3. Inheritance 1. Java is single inheritance, but it can be inherited indirectly, extends 2. In java, any class, except Object, has a parent class 3. What attributes and methods can a subclass inherit from the parent class? Under the same package: public, default, protected Under different packages: public, protected Visit: Under the same package: public, default, protected Under different packages: public 4. Override/override: In the inheritance relationship, the method name of the subclass is the same as the method name of the parent class, the parameter type is the same, the return type is the same, and the access permissions are the same Subclasses cannot be more strict than parent classes: private > default > protected > public @override 5. Overloading: In a class, the method name is the same, the parameter type is different, and there is no requirement for the return value. 6. The construction method cannot be inherited, but the construction method of the subclass will…

5ffa5919c4de10e4df8d00460883b1fa.bmp

java type conversion and rewriting_java notes encapsulation, inheritance, rewriting, polymorphism and type conversion

Foreword Today, let’s talk about the main specific manifestations of object-oriented in Java, namely encapsulation, inheritance and polymorphism. This is the point of the basics. Text Encapsulation Hide implementation details 1) Encapsulation steps: 1 )) Privatize properties , Use the private modifier, to modify the methods and properties that need to be hidden 2)) Provide common methods, to access private properties (getter, setter ) Note: The purpose of encapsulation is to reduce the correlation between classes 2) Encapsulation specifications: 1)) Modify the visibility of the attribute to restrict access to the attribute 2)) Create a pair of assignment and value methods for each attribute 3)) Add restrictions on attributes in the setter and getter methods 3) Benefits of encapsulation: 1)) Increased data access restrictions, Enhances the security of the program 2)) Provides a series of rules for attributes, thereby protecting the attributes 3)) From a broad perspective Implementation details hidden on )) Overloading has nothing to do with the return value Inheritance Things have the same characteristics and there is an inclusion relationship,One thing has the characteristics of another, And have its own independence. 1) Syntax: extends,The table name of the class being constructed,is derived from an existing class Note…

[Java] The difference between overloading, rewriting, inheritance, and polymorphism

Overload Simply put, it allows multiple function methods with the same name but different numbers or types of formal parameters to exist in a class. The class uses a unified calling method, and the specific method is determined by the formal parameters! Overloaded methods are distinguished by the type, number, and order of formal parameters, not by the names of formal parameters, nor by return value! That is, the return values ​​can be different or the same. Overloading involving basic types Refer to the example in thinking in java (in the attachment). The basic type can be automatically promoted from a “smaller” type to a “larger” type. Once this process is involved Overloading, can cause some confusion. The PrimitiveOverloading class shows what happens when you pass a primitive type to an overloaded method: Through the program output, you will find that the constant value 5 is treated as an int value. So if there is an overloaded method that accepts int type parameter, it will be called. As for other cases, if the actual parameter type passed in is “less than” the formal parameter type declared in the method, the type of the actual parameter will be “promoted”. The char…

(2) Centralized relationships in Java, isa, hasa, inheritance, focus on inheritance

Common class relationships in java (also introduced on javacore) 1. is a relationship ()  2.has a The relationship between the whole and the part 3. Inheritance relationship exists in the real world and the above two relationships cannot be described Of course, the most talked about is the inheritance relationship, and here comes the parent class and subclass. There are some stories below between father and son. General considerations in parent-child classes  The subclass displays the constructor of the parent class Use the keywordsuper(), which must be placed first in the subclass constructor OK, otherwise an error will be reported. The difference between super and this Super is mainly used in subclasses, calling the super() construction method of the parent class and calling the attribute method of the parent classsuper.x(), super.fun(); This is mainly used in this class to only think about the current object. Rewrite (override) Subclasses rewrite the methods inherited from the parent class according to actual needs. The definitions of the methods are exactly the same, but the method bodies are different. If the definitions of two methods in the same class are exactly the same, an error will be reported. The difference between overloading and rewriting…

Three major elements of Java objects_Three major characteristics of Java (encapsulation, inheritance, polymorphism)

There are three major features in Java, namely encapsulation, inheritance and polymorphism. The concept is very abstract and in-depth. 1. Encapsulation Concept: Encapsulation & #xff0c; That is, hiding the properties and implementation details of the object , only exposing the interface to the outside world , controlling the access level for reading and modifying properties in the program ; abstracting the obtained data and behavior (or function) Combined to form an organic whole, that is, to organically combine data with the source code for operating data to form a “class”, in which data and functions are both members of the class. . In electronics, “packaging” refers to connecting the circuit pins on the silicon chip to external connectors with wires to connect them to other devices. (from Baidu) After encapsulating the member attributes, we should provide corresponding get/set methods to access this member attribute. The advantage of encapsulation is that the attributes are hidden. It is no longer exposed data, making the data more secure and reliable. 1 classA{2 private int a;//This variable is encapsulated 3 //The interface provided for external access realizes only Read and write only 4 public intgetA(){5 returna;6 }7 public void setA(inta){8 this.a=a;9 }10 } When…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索