03-Classes and Objects
03-Classes and Objects
(http://docs.oracle.com/javase/tutorial/java/javaOO/index.html)
• Encapsulation
• Inheritance
• Polymorphism
ID_Num ID_Num
Name Name
YearOfBirth inherited YearOfBirth
Address Address
getID_Num() getID_Num()
setID_Num(newID) setID_Num(newID)
...... ......
“is a”
relationship
RollNum
class STUDENT
Score
extensions
RollNum getScore()
Score setSore(newScore)
getScore() ......
setSore(newScore)
......
Son = Father + extensions
Product
code
name
make
price
ElectricProduct Food
guaranty Ceramic
date
voltage type expiredDate
power
Session 03 - Classes and Objects
OOP Concepts: Polymorphism
Access level
Applied to
Free-accessing public
interface
package/ subclass
protected
class
outside cannot access
private
members of
interface/class
package
no specifier
(default)
Interface is a group of prototyped
methods and they will be
Order: implemented in a class afterward.
public > protected > default > private It will be introduced later.
Session 03 - Classes and Objects
Access Level
Subclass-
Same
Modifier Class Outside World
Package
package
private Y N N N
No (default) Y Y N N
protected Y Y Y N
public Y Y Y Y
y-=7
1000 x=5
y=6
800 x=4
obj1 1000
obj2 800
N 10 → 9999
• Constants:
• The static modifier, in combination with
the final modifier, is also used to define
constants. The final modifier indicates
that the value of this field cannot
change.
static final double PI =
3.141592653589793;
Professor Student
- String department - String studentId, majorField
+ String getDepartment(); teach - String degreeSought
+ void setDepartment(String d); + String getStudentId();
+ void setStudentID(String id)
….
The class Professor has the The class Student has
field Student[] students the field Professor pr
Session 03 - Classes and Objects
Inheritance
• There are some sub-classes from one super class ➔ An
inheritance is a relationship where objects share a
common structure: the structure of one object is a sub-
structure of another object.
• The extends keyword is used to create sub-class.
• A class can be directly derived from only one class (
Java is a single-inherited OOP language).
• If a class does not have any super class, then it is
implicitly derived from Object class.
• Unlike other members, constructor cannot be inherited
(constructor of super class can not initialize sub-class
objects)
Product
code
name
make
price
- String name;
+ void print();
GraduateStudent UndergraduateStudent
m3(), m4() in A
cannot implement
m3(), m4() in
InterfaceDemo,
attempting to assign
weaker access
privileges, were
public
Modified
Error.
Why?
class Main {
static void fun(Season x)
{switch(x)
{case SPRING: System.out.println("It is spring"); break;
case SUMMER: System.out.println("It is summer"); break;
case AUTUMN: System.out.println("It is autumn"); break;
case WINTER: System.out.println("It is winter");
}
}
public static void main(String[] args) {
Season x = Season.WINTER; fun(x);
for(Season y: Season.values()) {
System.out.print(y + ": "); fun(y);
}
System.out.println();
}
}
class Main {
public static void main(String[] args) {
Season x = Season.WINTER;
x.display();
System.out.println();
}
}