Java object-oriented basics and advanced knowledge summary
Controlling Access to Members of a Class.
private: Can only be used in your own class
public: Can be used anywhere
protected: package-private can be used under the same package, and can also be used in subclasses that inherit it under other packages.
no-modifier: package-private can only be used under the same package (the same as protected for the same package). It doesn't work in other packages.
Overriding and Overloadding Overriding
Overriding: is used for subclasses Between it and the parent class, except for the change in the function body, everything else remains unchanged. And the access controller of the subclass overriding cannot be higher than the parent class.
Overloadding: It is between methods in the same class. The parameter list must change, and the return type may or may not change.
##Polymorphism Polymorphism
can generally be understood as
ParentClass obj = new ChindClass(); //A
ChildClass obj2 = new ChildClass(); //B
ChildClass obj3 = new ParentClass(); //C error example
I will give you a tip as to why there is this setting. For A, we declare ParentClass, then we are equivalent to opening separate rooms in memory for the variables and methods in ParentClass. When we create new, it is like someone actually comes to ChildClass, because the variables and methods in ChildClass The number must be greater than or equal to the number in ParentClass, so the rooms we opened before are all full, so the hotel is very happy and stays as much as we booked. For C, we have booked a room using the variables and methods in ChildClass, but the actual number of people coming is from ParentClass, and the number is obviously smaller than the number of people in ChildParent. If the reservation is not satisfactory, the hotel will be very angry and report an error to you.
Overriding and overloading are important manifestations of polymorphism.
Abstract Class Abstract class
abstract class can contain or not contain abstract functions, abstract functions must be qualified with the abstract keyword. As long as a class contains an abstract function, the class must be modified with the abstract keyword. Same in its subclasses.Interface Interface
Interface is something more extreme than abstract class. All methods in it must be abstract methods and cannot contain instance fields. All constants such as int must be static and final. implement It must be to implement all abstract methods just like abstract.Java Advanced Knowledge
Generic Class
Very easy to pass The following example is obvious. For example, arraylist or hashmap are all generic .
package Generic; //generic class public class GenericMethodTest< A,Z > { //generic variable public A a; public void setA( A a) { this.a = a; } //generic methods public A getA () { return this.a; } public void printArray ( Z[] inArray) { for ( Z temp : inArray) { System.out.println(temp); } } public static < B > void printArray_2 (B[] inArray) { for (B temp : inArray) { System.out.println(temp); } } }
package Implement; import Generic.GenericMethodTest; public class a{ public static void main (String[] args) { GenericMethodTest<Integer,String> gm = new GenericMethodTest<Integer,String>() ; gm.setA(10); String[] ss = {"aaa", "bbb", "ccc"}; Double [] bb = {1.0, 2.0, 3.0}; gm.printArray(ss); gm.printArray_2(ss); gm.printArray_2(bb); } }
Serialization is simply a technology or process that converts existing instantiated objects into byte arrays. It has many benefits, please take a look.
http://stackoverflow.com/questions/2232759/what-is-the-purpose-of-serialization-in-java
Note: For some areas that we do not want to serialize, we can use the keyword
transientto modify it.
package Serialize;
import java.io.*;
public class Employee implements Serializable{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck (){
System.out.println("Mailing a check to "+ name + " " + address);
}
}
package Serialize; import java.io.*; public class SerializeDemo { public static void main (String[] args) { Employee e = new Employee(); e.name = "Reyan df"; e.address = "New York, ManhaThan"; e.SSN = 1234433224; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream ("/Users/huazhe/Desktop/demo.ser"); ObjectOutputStream out = new ObjectOutputStream (fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.println("Serialization done..."); } catch (IOException i){ System.out.println(i); } } }
package Serialize; import java.io.*; public class DeserializaDemo { public static void main (String[] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("/Users/huazhe/Desktop/demo.ser"); ObjectInputStream in = new ObjectInputStream (fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { System.out.println(i); } catch (ClassNotFoundException c) { System.out.println(c); } System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } }
The above is the detailed content of Java object-oriented basics and advanced knowledge summary. 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

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

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

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

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

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

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.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
