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!