Java™ Tutorial (Generics, Inheritance, and Subtyping)

Java™ Tutorial (Generics, Inheritance, and Subtyping)

Generics, Inheritance, and Subtyping As you know , you can assign an object of one type to an object of another type as long as the types are compatible , for example , you Integer can be assigned to Object,because Object is one of the supertypes of Integer&#xff1a ; Object someObject = new Object();Integer someInteger = new Integer(10);someObject = someInteger; // OK In object-oriented terminology , this is called an “is-a” relationship, since Integer is a Object,therefore allows assignment,but Integer is also a Number,so the following code is also valid&#xff1a ; public void someMethod(Number n) { /* … */ }someMethod(new Integer(10)); // OKsomeMethod(new Double(10.1)) ; // OK The same goes for generics ,You can do a generic type call ,pass Number as its type parameter, xff0c;Allows any subsequent add call if the argument is compatible with Number,: Box box = new Box();box.add(new Integer(10)); // OKbox.add(new Double(10.1)); // OK Now consider the following method: public void boxTest(Box n) { /* … */ } What type of parameter does it accept? By looking at its signature, you can see that it accepts a parameter of type Box. But ,what does this mean? can you pass Box or Box as expected? the answer…

Java instantiates a subclass object and calls the parent class construction method static_javase encapsulation, static, inheritance

One of the object-oriented features of Java (encapsulation) Encapsulation : shields external direct access to members through privatization, thereby improving security, shielding specific Implementation details Implementation details of encapsulation: 1. Make members private,so that they cannot be accessed in external classes 2 . Provide public access to getter and setter methods for privatized members get is used to access data values,so it only needs to return,no parameters are required set is used For assignment ,requires parameters,no need to return this keyword can only be used in ordinary member methods and constructors of the class this expression Is the object currently being used this. Member attribute this. Member method () This() calls the constructor , This can be used in any constructor Others of the class static static keyword static can be used to modify attributes,methods,code blocks,inner classes,guide packages Above Static modified ones are loaded during compilation,Loaded first Variables modified by static keywords have the following characteristics 1. Loaded during compilation , JVM allocates space 2. Always use the same memory address,Changes to static members will affect all methods that use this variable 3. Static members exist independently of the objects of this class and belong to the class. All classes…

Java basic encapsulation, inheritance, polymorphism

Encapsulation 1. Concept Hide some information of a class inside the class and do not allow external programs to access it directly, but through the The class provides methods to operate and access hidden information. Therefore, encapsulation has two meanings: Hide what should be hidden, and expose what should be exposed. 2. Benefits a) Encapsulation can hide implementation details b) Allow users to write only through implementation access methods to access these fields. In this way, we only need to add logical controls to these methods to limit unreasonable access to data. c) Convenient data inspection and conducive to protecting object information Completeness d) Facilitate modification and improve code maintainability 3. Encapsulation implementation steps a) Modify the visibility of attributes Set to (private) b) Create getter/setter methods (for reading and writing attributes) (data is obtained and set through these two methods, and the object is implemented by calling these two sending methods Reading and writing data) c) Add attribute control statements to the getter/setter methods (to judge the legality of attribute values) 1 public class Person{ 2 private String name; 3 private int age; 4 ​ 5 public int getAge(){ 6 return age; 7 } 8 ​ 9 public String…

JavaScript creates objects – classes, inheritance

Note: a. All the codes in this article have been tested under the Chrome browser. Readers are recommended to also install a Chrome browser; b. The description in this article follows the ECMA-262 fifth edition specification c. The output of this article uses the console.log function, please press F12 to enter the debugging mode , watch the console output; d. Source code link address e. Please indicate the source for reprinting . 1. What is an object? Javascript itself has no concept of classes, only the concept of objects. Except for the basic types (string, number, boolean, null, undefined), the rest are objects. Even function is an object.So, what is an object?! An object in Javascript is a collection of pairs of keys. You can even use keys #20540;The right way to manipulate objects in Javascript is like this:var myDog= new Object();myDog[“name”] = “Odie”;myDog[“color”] = “Yellow”;console.log (myDog [“name”] );console.log(myDog [“color”] ); The effect of creating an object in this way is the same:var myDog = new Object();myDog .name = “Odie”;myDog .color = ” Yellow”;console.log(myDog.name );console.log(myDog.color ); Obviously, the second access method is more convenient. This method of access is usually only used when the attribute name of the object we…

The difference between Android basics Java polymorphism, inheritance, overloading and rewriting

[Free] Exclusive on the entire network:This is a very valuable Android knowledge system!!& #xff01; 1. Inheritance 1.1 What is inheritance? In short, inheritance is A way to add new methods or override base class methods based on existing classes. Inheritance is also one of the basic features of object-oriented languages ​​[inheritance, polymorphism and encapsulation]. In Java, the class java.lang.Object is the base class of all classes [also called parent class or super class]. The two keywords involved in inheritance are as follows: extends: For inherited classes,Each class can only inherit at most one parent class implements: for inheriting interfaces, there is no limit on the number of inherited interfaces for each class Below we just created a Person class , and did not explicitly specify a class to inherit from – but its base class defaults to Object. /*** Create class: person*/public class Person{//Name public void name(){}//Gender public void sex(){}} 1.2 How to use inheritance public class JavaPolymorphic {/*** Create an interface*/public interface IPerson{//Methods in the interface void work();}/*** Create a class& #xff1a; Man, and inherits Person*/public class Man extends Person implements IPerson{/*** Overrides the name() method of the parent class Person*/@Overridepublic void name() {super.name();}/*** Override the work() method in…

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 programmers from stupid to noobs_Java programmers from stupid to noobs (2) Object-oriented encapsulation, inheritance, polymorphism (Part 1)…

Java is an object-oriented language – everyone knows this – what makes it different from process-oriented languages ​​​​such as C language is its own object-oriented characteristics – encapsulation ,Inheritance,Polymorphism,These are the legendary three major characteristics of object-oriented 1:Start with classes and objects: Oop’Object Oriented Programming’ The description of a type of thing is an abstract conceptual definition Object: an object is every individual of the type of thing that actually exists, so it is also called an instance. Three relationships between classes:Dependency relationship (uses-a) Aggregation relationship (has-a) Inheritance relationship (is-a) In java& #xff0c; The relationship between classes and objects is like the relationship between animals and tigers – tigers belong to animals – tigers are just an instance of animals. A class represents the category of an object and is the blueprint for creating an object. Building an abstract model of a thing is essentially to express the nature and behavior of the thing. Using classes to build abstract models is achieved by defining variables and methods in classes. The attribute defined in the class is a variable that can store a value – this variable represents the specific nature of the thing. Objects of a class have rows implemented…

The benefits and disadvantages of polymorphism in Java basics (encapsulation, inheritance, polymorphism)

Benefits and disadvantages of polymorphism: 1. Benefits of polymorphism: Improves the scalability of the program Specific: When defining a method, use the parent type as a parameter, When using it in the future, use specific subtypes to participate in operations 2. Disadvantages: You cannot use the unique functions of the subclass If you want to call methods in the subclass, you need to downward transformation. /** * @author Wrry * @data 2020-08-12 * @desc Animal parent class */public class DongWu { public void eat(){ System.out.println(“Animal class: animals all eat”); }} /** * @author Wrry * @data 2020-08-12 * @desc Operation class */public class DongWuOperation { /* / /Create a cat object public void CZM(Cat c){ c.eat(); } //Create a fish object public void CZY(Fish f){ f.eat(); }*/ public void CZDongWu(DongWu d){ d.eat(); }} /** * @author Wrry * @data 2020-08-12 * @desc Cat class */public class Cat extends DongWu{ //Write the eat method from the DongWu method @Override public void eat() { System.out.println(“Cats like to eat fish”); }} /** * @author Wrry * @data 2020-08-12 * @desc 鱼 Class */public class Fish extends DongWu{ public void eat(Fish f){ System.out.println(“Fish eats shrimp”); }} /** * @author Wrry * @data 2020-08-12 *…

The three major features of Java: encapsulation, inheritance, and polymorphism

The three major characteristics of Java: encapsulation, inheritance, polymorphism 1. The concept of class: A class generally includes two Parts: Attributes (Member Variables) and Methods(Member Methods)Method components: Modifiers(Optional)Return Value Type& #xff08;Required)Method name(Required)Parameter list(Optional) package com.classTest.project;public class ClassTest { //Member variable String name;int age; //Member method public void count(){this.age+=1;}} 2. Some explanations on the definition method 1. Return value type:Basic data type (integer (int), floating point (float), string type ( String), character type (char, etc.), reference data type (array, class, interface) Example: ①Basic data type as return Value type: ⑴Integer type (int) ⑵Floating point type (float) ⑶String type (String) ⑷Character type (char) package com.returntype.project;public class BasicData {public static void main(String[] args) {BDMethod BdMethod=new BDMethod();int getInt&#61 ;BdMethod.getInt();System.out.println(“The return value is an integer: “+getInt);float getFloat=BdMethod.getFloat();System.out.println(” The return value is a floating-point type: “+getFloat);String getString=BdMethod.getString();System.out.println(“The return value is a string type: “& #43;getString);char getChar=BdMethod.getChar();System.out.println(“The return value is the returned content of characters:”+getChar);}} package com.returntype.project; public class BDMethod {public int getInt(){return 2;}public float getFloat(){return 3.2f;}public String getString(){return “Return value String Type “;}public char getChar(){return 'a';}} ②Reference data type as put back value type: ⑴Array ⑵Class ⑶Interface 2. Modification Symbol: access level (public, private, protect, default) Same category The same package Subclasses of different packages Non-subclasses…

Technical Pictures

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

Introduction In process-oriented, there is only encapsulation (encapsulation of functions, such as functions in the C language), while these three characteristics only exist in object-oriented. Inheritance –>inheritance 1. Subclasses can inherit attributes and methods from the parent class (except for the private modified methods of the parent class) 2. Subclasses can provide their own separate attributes and methods Encapsulation–>encapsulation If you don’t need outside access, I will hide it. If the outside world may use it, I will provide you with an interface 1. Hide certain attributes and methods from the outside 2. Expose certain attributes and methods to the outside world Polymorphism–>polymorphism 1. The final state of the program is determined only during execution and not during compilation The specific understanding will be discussed below Encapsulation Introducing encapsulation can hide the specific implementation inside the object, and provide interfaces for specific functions to avoid external interference in internal data. It can make the program achieve “high cohesion” and “low coupling”, making the system better stability and scalability. How to implement encapsulation: The data and methods inside the object are encapsulated through access modification permission control characters. 1. Different access modifier permission control          There are four types of member…

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
首页
微信
电话
搜索