The three basic characteristics of java object-oriented: encapsulation, inheritance, and polymorphism
java It is an OOP language(ObjectOrientedProgramming)Object-oriented programming language,So what is an object?What is an object?There is no doubt that this question has many answers&# xff0c;Everyone’s understanding of objects is different. Encapsulation of the three basic characteristics of java object-oriented : Why should we encapsulate our code? When we define a human body weight , require user input , if The user inputs 1000kg, which is unrealistic for us people – so how do we enable us to output exceptions in time when the user input is unreasonable – this requires the use of encapsulation in java. The advantage of encapsulation is that it hides class implementation details and we execute them through the get and set methods of attributes. This makes it easier for us to modify the code later. So how do we encapsulate attributes?Example public class Dog {private String name;private int love;private int health;private String strain;/* ***Get the value of name* @return the value of name*/public String getName() {return name;}/**** Assign the value then* @param name The name of the dog returned */public void setName(String name) {this.name = name;}/**** Get the dog’s intimacy* @return The dog’s intimacy*/public int getLove() { return love;}/**** Assign the dog’s intimacy* @param love…
[Mechanism] JavaScript prototype, prototype chain, inheritance
1. The concept of prototype and prototype chain When js creates an object, such as calling it obj, They will secretly add a reference to him. This reference points to an object, such as yuanxing. This object can provide attribute sharing for the object that refers to it. , for example: yuanxing has an attribute name, which can be accessed by obj.name. This can provide an object for attribute sharing. , is called the prototype of the previous object And the prototype itself is also an object, so it will also have its own prototype. This continues layer by layer until it finally points to null, which forms Prototype chain How is the prototype mechanism of js implemented? 2. Prototype implementation Let’s look at an example first: // code-01let obj = new Object({name:’xiaomin’})console.log(obj.name)console.log(obj.toString())// xiaomin // [object Object] We first create an object obj, which has an attribute name The attribute name was created by us, but when creating obj, the toString attribute was not created for it. Why can obj.toString() be accessed? prototype attribute Let’s first take a look at the Object.prototype attribute We found that there is toString here attributes, so in fact Object.prototype is the prototype of obj.…
Java language encapsulation, inheritance, abstraction, polymorphism, interface
Table of contents Foreword 1. Encapsulation 1.1 Definition of encapsulation 1.2 Access modification Use of symbols 2. Inheritance 2.1 Definition of inheritance 2.2 Methods of inheritance 2.3 Points to note when using inheritance 3. Polymorphism 3.1 Definition of polymorphism 3.2 Dynamic binding 3.3 Method rewriting 3.4 Upward (Downward)Transformation 4. Abstraction 4.1 Overview and definition of abstraction 4.2 Use of abstraction p> 5. Interface 5.1 Meaning of interface 5.2 Definition of interface Summary 😽Personal Homepage: tq02’s Blog_CSDN Blog-C Language, Java Blogger 🌈 Ideal goal: study hard and move towards Java There are regrets. 🎁Welcome → Like👍 + Collection⭐ + Comment📝+Follow✨ Contents of this chapter&# xff1a;Java encapsulation, inheritance, abstraction, polymorphism, interface Use compiler :IDEA Preface Before talking about encapsulation, inheritance, abstraction, polymorphism, and interfaces, we must first understand what classes and objects are. Detailed explanation of classes and objects:tq02’s explanation of classes and objects 1 .Package 1.1 Definition of package Encapsulation ,Operation in the class,Encapsulate the member variables and member methods of the class,Meaning: Can be thought of as a protective barrier,preventing the code and data of this class from being randomly accessed by code defined by external classes. To encapsulate the class we need to use access modifiers, which are…
Java object-oriented, abstraction, encapsulation, inheritance, polymorphism
What is object-oriented? Object-oriented is a programming idea and a programming model. To deeply understand the concept of object-oriented, you must first understand the responsibilities of software in the real world. We all know that software exists to allow computers to directly simulate the real environment, help humans solve problems and improve efficiency in the virtual world. When developing software, we must face a problem, that is, how to represent problem elements in the real world in software. We call the elements in the problem space and their representations in the solution space “objects” ( Object). The main idea of object-oriented programming is to decompose the various things that constitute the problem into individual objects. The purpose of establishing an object is not to complete a step, but to describe the steps and behaviors that a thing goes through in the process of solving the problem. As the basic unit of a program, objects encapsulate programs and data to improve program reusability, flexibility and scalability. Classes are templates for creating objects, and a class can create multiple objects. Objects are instantiations of classes. What is abstraction? We mentioned above that when using the object-oriented programming model, the concept of class…
Java object-oriented programming: encapsulation, inheritance, polymorphism
Three major characteristics of Java object-oriented: Encapsulation: Encapsulation of data and operation methods is achieved through Java classes. Each Java class can be regarded as a black box to the outside world, and it can be completed by simply calling the methods provided by the black box. the operation you want. Inheritance: Through class inheritance, unified functions are concentrated in the parent class to achieve code reuse and maintainability. Polymorphism: Realize different morphological characteristics of different classes through overloading, rewriting and overwriting. A class member defined as private is private to the class and cannot be accessed by all code outside the class, including subclasses. If the subclass does not explicitly call the parent class’s constructor, the parent class’s default constructor (if any) will be called. Mutual conversion between parent class and subclass In Java, we can assign the reference of the subclass to the object of the parent class. Then those members in the subclass that are not inherited from the parent class will no longer be visible. We can convert the parent class to the parent class by casting the type. After converting to a subclass type, those members become visible again, so what happened during the conversion?…
Java uses interfaces, polymorphism, inheritance, and classes to calculate the perimeter and area of triangles and rectangles
Java|java tutorial java interface polymorphic inheritance class Java-java tutorial This article describes the example of java using interface, polymorphism, Inherited, class methods for calculating the perimeter and area of triangles and rectangles. Share it with everyone for your reference. The details are as follows: Dreamweaver Financial adaptive mobile phone source code, how to use vscode for Apple, ubuntu suddenly stuck, tomcat so location, sqlite setting field auto-increment, js upload image screenshot plug-in, p2p suitable front-end framework, software for writing crawler code, php File name suffix, seo telemarketing speech, asp website moving, delphi web page save as, comic web page template lzw definition interface specification: college student part-time website source code, vscode menu bar missing, Ubuntu, tomcat binding project name, explosion-proof Crawler box, current path of php file, how to carry out Taiyuan network seo promotion, website access statistics js code lzw /** * @author vvv * @date 2013-8-10 AM 08:56:48 */package com. duotai; /** * * */public interface Shape { public double area(); public double longer(); } /** * @author vvv * @date 2013-8-10 09:10:06 AM */package com.duotai; /** * * */public class Triangle implements Shape { double s1; double s2; double s3; // Initialize a triangle object and give…
Three major features in Java: encapsulation, inheritance, and polymorphism
Encapsulation: Concept: Encapsulation can be considered as a protective barrier to prevent the code and data of this class from being accessed at will by other classes. Proper encapsulation can make the code easier to understand and maintain, and also enhance the security of the code. Principle: Hide properties. If you need to access a property, provide public methods to access it. The main application is JavaBean Code: Create JavaBean 1 //Create a Person class 2 public class Person { 3 private String name; //Private private methods modify variables and encapsulate variables 4 private int age; 5 //No-parameter construction method 6 public Person() { 7 } 8 //Construction method with parameters 9 public Person(String name, int age) { 10 this.name = name; 11 this.age =age; 12 } 13 //set, get methods 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age =age; 28 } 29 //Rewrite the toString method: to make the output format more beautiful 30 @Override 31 public String toString() { 32 return “Person{” +…
Java encapsulation, inheritance, polymorphic feature example code analysis
This article mainly introduces the relevant knowledge of Java encapsulation, inheritance, and polymorphic feature example code analysis. The content is detailed and easy to understand, the operation is simple and fast, and has certain reference value. I believe that everyone has read this Java The articles on encapsulation, inheritance, and polymorphic feature example code analysis will all be useful. Let’s take a look at them below. 1. Encapsulation What is encapsulation? Talk about your understanding of encapsulation. Encapsulation is to hide class information (such as class attributes) inside the class. It is not allowed Direct access from external programs. At this time, we need to mention the keyword private, which is a permission modifier that can be used to modify members (variables and methods) to protect members from being used by other classes. If they need to be used by other classes, then you need to Provide corresponding operations: a. Provide the get variable name () method, used to obtain the value of the member variable; b. Provide the set variable name (parameter), used to set the value of the member variable. It is also the same as the get method, both are used public to modify (also remember that…
Java basics object-oriented (encapsulation, inheritance, polymorphism)
Everything is an object. In object-oriented thinking, we may not particularly pay attention to encapsulation, inheritance, and polymorphism. What is object-oriented? What is encapsulation and what is inheritance? What is polymorphism? What interface? What is abstraction? What is the relationship between interface and abstraction? During the interview process, I hesitated in my answers, and I deeply realized that I did not have a systematic and profound understanding of object-oriented. Everything only stayed in the textbook and only had a superficial understanding. During the conversation with the interviewer, the interviewer explained to me some knowledge that I did not understand deeply enough. The interviewer I have met so far is very nice, just like a big brother, and the conversation is very comfortable and relaxing. Now let me re-understand object-oriented! The three major characteristics of Java object-oriented: encapsulation, inheritance, polymorphism 1. Encapsulation: Encapsulation in Java refers to a class hiding its internal implementation details. Only the external interface (getter and setter) methods are exposed, and encapsulation is divided into attribute encapsulation and method encapsulation. Encapsulation is divided into attribute encapsulation and method encapsulation. Attributes are defined as private. They set and obtain the value of the attribute through setter and getter…
Implementation code of Java abstract classes, inheritance, polymorphism and adapters
Java inheritance Method rewriting is a polymorphic feature of the Java language and must meet the following conditions In the subclass, the method name is exactly the same as the parent class method name The number and type of parameters of the method are exactly the same, and the return type is exactly the same The access modifier access level of the method is not lower than the access level of the method with the same name in the parent class Add the @override annotation on the method. If an error is reported, it means it is not an override Method overriding restrictions The parent class method modified by final cannot be overridden in the subclass Static-modified parent class methods cannot be overridden in subclasses, but can only be overridden super keyword The super keyword is similar to this. super modifies the object of the parent class, such as super(); it calls the default parameterless constructor of the parent class Java abstract class Abstract class characteristics Abstract classes should usually contain abstract methods and can also contain non-abstract methods Abstract classes cannot be modified with the final keyword Abstract classes themselves cannot be instantiated Abstract classes are meant to be…