[go: up one dir, main page]

0% found this document useful (0 votes)
10 views4 pages

Review

Uploaded by

dihoc77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Review

Uploaded by

dihoc77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

REVIEW

 A Java class can only extend one parent class. Multiple inheritance using classes is
not allowed.
An interface can extend more than one parent interface. Multiple inheritance using
interface is supported

class A extends B implements C, D, E { }


interface A extends B, C, D { }

 Static block static { … }

excute once when the class is first loaded into the Java virtual machine, before
main method is called.

 String contains methods that don’t change string, StringBuffer contains methods
that change string

 Like object of String classes, objects from wrapper classes are immutable (can’t
change)

 matches() method of String class: checks that a string is valid according to a


Regular Expression

public boolean matches(String regex)

Or Pattern.matches(regex, str);

String str = “aaaab”;

str.matches(regex);

str.matches(“a*b”);

 this() method: call constructor of current class

 native keyword: specify code of other language

 volatile keyword: is used for variable, so can modify the value of a variable by
different threads

 transient fields are also not serialized => no write an object


 synchronized keyword: show that only one object or class can access variables,
or methods at a time, often used in multithreading

 pass parameters to method: pass by value

 Algorithms of class Collections are applied to List and subclasses of List.

 object method: can access both object/instance and static/class member


 static method: only access static/class field and static method, can’t access object
field and object method
so non-static variable “super” cannot be referenced form a static context (such as
static method)

 Outer/ Enclosing class (Lớp bao): can not access members of nested class, but
can access through object
Nested class (Lớp lồng): Only known in outer class.

o static nested class (lớp lồng tĩnh): the same as static method

can access static nested class by outer classname

can access directly static members, can not access directly object members
in outer class but can access through object.

o nonstatic nested class/ inner class (lớp lồng phi tĩnh/ lớp nội bộ): the
same as object method

access non-static nested class by outer class object

can access directly object and class/static members in outer class


=> static/class method is similar to static nested class
=> object/instance method is similar to non-static nested class/ inner class

 clone() method of the java.lang.Object class:


creates and returns a shallow copy of the object/ instance of the same class and
copies all the fields and their values to the new instance.
clone() method of the java.lang.Object class supports Shallow copy.
Compare Shallow copy and Deep copy:

Shallow copy => Shallow copies duplicate as little as possible, a shallow


copy of a collection is a copy of the collection structure, not the elements.
Deep copy => duplicate everything, a deep copy of a collection is two
collections with all of the elements in the original collection duplicated.

 Compare Shallow Comparision and Deep comparison:


Shallow comparision => reference comparision: == checks if both objects
point to the same memory location
Deep comparison => content comparision: equals, compareTo compares
values in the objects

 A x=new B();

B is subclass of class A, or implements interface A

Employee e = new Technician();

IEmployee e = new Manager();

• Superclass at top of exception inheritan tree: Throwable

Checked exception: at compile time.

• Must be handled by either the try-catch mechanism or the throws-


declaration mechanism, so code can be compiled

• Ex:
Accessing data in a file (FileNotFoundException)
Read a text line from standard input device (IOException)
Accessing a remote resource.
Accessing data in a database.

Runtime/Unchecked exception: at runtime

• Runtime exception: code is compiled with no error without try… catch or


throws

• Ex:
Accessing an element of an array (ArrayIndexOutOfBound)
Access members of object (NullPointerException)
Divide by zero (ArithmeticException)
Method has been passed an illegal argument (IllegalArgumentException)
Parses the string as a number (NumberFormatException)

Polymorphism
• Static polymorphism is polymorphism that occurs at compile time, such as
method overloading
• Dynamic polymorphism is polymorphism that occurs at runtime, such as method
overriding (in class that implements interface or extends abstract superclass/
superclass)

Overriding method vs. hidding method


when a subclass method is declared the same as superclass method
• Override object method:
superclass method is hidden
subclass method overrides superclass method.
• Hide static method:
subclass method is hidden, subclass object calls superclass method.
Because:
The static method is resolved at compile time cannot be overridden by a subclass.
An instance method is resolved at runtime can be overridden.

You might also like