search
HomeJavajavaTutorialJava 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);
	}
	
}


##Serialize Serialize

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


Simple serialization can be learned based on the following code.

Note: For some areas that we do not want to serialize, we can use the keyword

transient

to 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor