Class Definition. Inheritance
Abstract Class & Interfaces
Class Definition
•
•
•
• Scanner keyboard;
•
• Keyboard = new String();
•
• keyboard.nextInt();
• class data
• variables. methods
int a;
int b;
float c;
a = 1;
b = 2;
c = 4.5;
a = 6;
b = 4;
c = 8.6;
Object 1
Object 2
new
new
Class
variables
method(s)
members
•
• Date object1;
• new :
• object1 = new Date();
•
• Date object1 = new Date();
• Date object2 = new Date();
String month;
int day;
int year;
writeOutput()
month = May;
day = 6;
year = 1994;
month = October;
day = 25;
year = 1990;
new
new
Date
Demo.java
•
•
•
Heading
body
最重要的是這個回傳值宣告
當method動作最後有
return ___;
取決於回傳了什麼東西
回傳 int 就要寫 int
回傳 String 就要寫 String
如沒有回傳 也要寫 void
•
• object1.month;
• object2.day;
•
• object1.writeOutput();
String month;
int day;
int year;
writeOutput();
month = May;
day = 6;
year = 1994;
month = October;
day = 25;
year = 1990;
object1
object2
new
new
Demo
•
•
•
•
•
•
•
•
•
• main void
• main
public static void main (String[] args)
{
……
}
•
•
•
public void method_a()
{
int a;
…
}
public void method_b()
{
int a;
…
}
•
•
public class Date
{
String month;
int day;
int year;
public void setDate(String month, int day, int year)
{
this.month = month;
this.day = day;
this.year = year;
}
}
Q & A ?
Information Hiding & Encapsulation
&
•
•
private
private
DateTry date = new Demo( );
(1) date.month = “January”;
(2) date.day = 1;
(3) date.year = 2008;
(4) date.writeOutput( );
(5) date.getDay( );
Another class
private
public
, private
methods
•
•
•
•
•
•
int getA( ) { return a; }
// Accessor method
void setA(int p) { a = p; }
// Mutator method
int getB( ) { return b; }
// Accessor method
void setB(int p) { b = p; }
// Mutator method
float getC( ) { return c; }
// Accessor method
void setC(float p) { c = p; }
// Mutator method
private int a;
private int b;
private float c;
class
•
•
•
•
public int getDate( )
{
}
public String getDate( )
{
}
Constructors
•
•
•
•
•
•
•
•
•
•
•
•
• false
• null
• zero
Overloaded
•
• Date object1 = new Date(5, 6, 1990);
• Date object2 = new Date(May, 6, 1990);
• Date object3 = new Date(1994);
•
• object1.Date()
Q & A ?
Static Type
•
•
(1) Round.area(radius)
(2) Round.volume(radius)
Round r = new Round();
(3) r.volume(radius)
•
•
void method1( )
{
B obj = new B( );
myobj.method2( );
}
void static method2( )
{
B.method1( );
}
void static method3( )
{
this.method1( )
}
void static method1( )
{
}
void method2( )
{
}
•
•
•
•
• Private static int myStaticVariable;
•
static int a;
int b;
Class myClass
b = 10; b = 5; b = 3; b = 1;
obj1 obj2 obj3 obj4
• private
•
• final
• public
• public static final int BIRTH_YEAR = 1994;
•
• int year = ClassName.BIRTH_YEAR;
Q & A ?
Inheritance
•
•
•
•
•
•
•
•
•
•
•
• public class HourlyEmployee extends Employee
String name;
int hireDate;
int hours;
int Salaryrate;
Employee HourlyEmployee
Inherit
•
•
•
•
•
•
Override
•
•
•
String name;
int hireDate;
public int getSalary()
{
definition
}
int hours;
int Salaryrate;
public int getSalary()
{
definition
}
Employee HourlyEmployee
Inherit
•
•
•
•
•
•
•
•
•
•
•
•
•
• private void doSomething()
• public void doSomething()
• public void doSomething()
• private void doSomething()
Override OK
Override WRONG!
• final
•
• final
•
•
•
•
• super ( )
public derivedClass(int p1, int p2, double p3)
{
super(p1, p2);
instanceVariable = p3;
}
Constructor method();
Method1();
Constructor method();
super()
Base Class
Derived Class
•
•
• this
public ClassName(type1 param1, type2 param2)
{
…
}
public ClassName()
{
this(argument1, argument2);
}
public class Employee
{
private String name;
private Date hireDate;
public Employee( )
{
}
public Employee(String theName, Date theDate)
{
}
public Employee(Employee originalObject)
{
}
}
public class HourlyEmployee extends Employee
{
private double wageRate;
private double hours;
public HourlyEmployee( )
{
this(“No name”, new Date(“January”, 1, 1000), 0, 0);
}
public HourlyEmployee(String theName, Date theDate,
double theWageRate, double theHours)
{
}
}
•
•
A class obj1
B class obj2
C class obj3
A obj1 = new A( );
B obj2 = new B( );
C obj3 = new C( );
obj1 = obj2; (1)
obj1 = obj3; (2)
obj2 = obj3; (3)
obj3 = obj1; (4)
•
private methodA()
{
…
}
public methodB()
{
methodA();
}
In Base Class
Invoke in derived class
•
•
•
• getClass()
• object1.getClass() == object2.getClass()
Q & A ?
Abstract Class
•
•
•
•
•
•
•
• Late Binding
• Dynamic Binding
•
•
•
•
• final. private. static
•
•
•
•
Employee obj1 = new Employee( );
HourlyEmployee obj2 = new HourlyEmployee( );
obj1 = obj2;
obj1.m1();
Employee obj1 = new Employee( );
HourlyEmployee obj2 = new HourlyEmployee( );
obj1.m1();
Employee & HourlyEmployee m1()
obj1.m1() m1 method
•
•
•
•
•
• private
public abstract class Employee
{
private String name;
private Date hireDate;
public abstract double getPay( );
}
public class HourlyEmployee extends Employee
{
private double payRate;
private double hours;
public double getPay( )
{
return payRate * hours;
}
}
public static void main(String args[])
{
Employee tony = new Employee( ); ERROR!!
HourlyEmployee joe = new HourlyEmployee( );
}
public class SalaryEmployee extends Employee
{
private double salary;
private double hours;
public double getPay( )
{
return salary / 12;
}
}
Q & A ?
Interface
•
•
•
• public
•
• class className extends baseclass implements Interface1, Interface2
•
•
• , methods (Override)
• , methods,
• , methods
•
•
• public. static & final
Q & A ?

Java & OOP Core Concept