Java Notes (3) - Class & Interface & Modifier
Course: Java Programming (Coursera)
Instructor: Dashi Tang, Peking University
Class
Field (variable) + Method (function)
- Constructor
- No return type!
- If not defined, system will generate a default constructor.
- Overload - Polymorphism
- Functions with different signatures can be recognized when compling.
- Signature: # and type of variables.
"this"
- Tell the difference from fields and local variables.
Person (int age, string name){ this.age = age; this.name = name; }- In a constructor, call another constructor. MUST put at first!
Person(){ this(0, ""); ... }
- Constructor
Inheritance
- Subclass & Superclass
- A subclass could have only one direct superclass.
- Subclasses could modify and add fields & methods of their superclasses.
"extends"
- All classes inherit from java.lang.Object
class Student extends Person { //A Student "is a" Person ... }Field
- Inherite & Re-define (Hide) & Add
Method
- Inherite
- @Override (Modify)
- Re-define a method with same name and signature in the superclass.
- Add
- Overload
- Define a method with same name and different signature in the superclass.
- Acually a NEW fuction.
"super"
- Access fields and methods in the superclass
- Override and use the superclass
void sayHello(){ super.sayHello(); ... }- Call the supre class's constructor
Student(String name, int age, String school){ super(name, age); //MUST put at first! this.school = school; }Casting
- Subclass & Superclass
Package
- Classes in the same package could access each other.
Modifiers
Access Modifier: public, private, protected
Other Modifier: abstract, static, final
Modifiers could modify classes and members (fields & methods) in classes
Access Modifiers
Modify Fields & Methods
- Accessibility:
Within Class Within Packet Subclass out of Packet Non-subclass out of Packet privateY N N N Default Y Y N N protectedY Y Y N publicY Y Y Y - Setter & Getter -- Encapsulation
class Person{ private int age; public void setAge(int age){ if(age>0 && age<200) this.age = age; } public int getAge(){ return age; } }Modify Classes
- Accessibility:
Within Packet Out of Packet Default Y N publicY Y
Other Modifiers
| Modify Classes | Modify Members | Modify Local Variables | |
|---|---|---|---|
static |
Inner Class | Y | N |
final |
Y | Y | Y |
abstract |
Y | Y | N |
- static
staticField- Not belong to any instance, stored in the memory block of class
- Could be accessed by class name or an instance, same result
- "Global variable"
staticMethod- Not belong to any instance
- Can't use
superorthis
- final
finalClass: Can't be inheritedfinalMethod: Can't be overrided by subclassesfinalField & Local Variable- Read-only
static finalfields represent constants. If not be initialized, the default values are 0 for numbers, false for boolean values, null for references.finalfields and local variables MUST be and could only be assigned once.
- abstract
abstractClass: Can't be instantiatedabstractMethod- Only declared, not implemented:
abstract returnType abstractMethod([paramlist]); - An
abstractclass may or may not containabtractmethods; but if a class contains an abtract methods, it is anabstractclass. abstractmethods MUST be implemented (overrided) in the subclass, otherwise the subclass is still abstract.
- Only declared, not implemented:
Interface
Define an interface: all methods are public abstract.
Implement an interface: multi-inherite; have nothing to do with class inheritence.
- Constant in Interface
(public static final) type NAME = value
Enumeration
enum Light{Red, Yellow, Green} Light light = Light.Red; switch(light){case Red:......Break;}staticMethod (above Java8): not only declared, also implemented; no need to be overrided in subclasses.
Definition Summary
- Class
[public][abstract|final] class ClassName [extends SuperclassName][implements InterfaceNameList]{
[public|protected|private][static][final][transient][colatile] type variableName; //Fields
[public|protected|private][static][final|abstract][native][synchronized] returnType methodName ([paramList]) //Methods
[throws exceptionList]{
statements
}
}
- Interface
[public] interface InterfaceName [extends SuperInterfaceList]{
type constantName = value; //Constants
returnType methodName([paramList]); //Methods
}
Method
- Constructor
className([paramList]){ ...... }- main()
public static void main (String arg[]){ ...... }- finalize()
protected void finalize() throws throwable{ ...... }.java File
package packageName; //0 or 1
import packageName.[className|*]; //0 or many
public classDefinition //0 or 1, className = fileName
interfaceDefinition and classDefinition //0 or many
OO Design: Course Enrollment System
In this project, I used all elements metioned above, including class, interface, various modifiers, inheritence, and etc. View the whole project here.
