introducing classes 2
TRANSCRIPT
Dr. Zara Hamid
CLASSES: A DEEPER LOOK
c lass Over loadDemo {void tes t () {
System.out .pr in t ln("No parameters") ;/ / Over load tes t for one in teger parameter.vo id tes t ( in t a) {
System.out .pr in t ln("a: " + a) ;}/ / Over load tes t for two in teger parameters.void tes t ( in t a , in t b) {System.out .pr in t ln("a and b: " + a + " " + b) ;}/ / over load tes t for a double parameterdouble tes t (double a) {System.out .pr in t ln("double a: " + a) ;return a*a;} }
c lass Over load {publ ic s tat ic vo id main(St r ing args[ ] ) {Over loadDemo ob = new Over loadDemo() ;double resul t ;/ / ca l l a l l vers ions of tes t ( )ob. tes t () ;ob. tes t (10);ob. tes t (10, 20) ;resul t = ob. test (123.25);System.out .pr in t ln("Resul t o f ob. tes t (123.25) : “ + resul t ) ;}}
OVERLOADING METHODS
c lass Test {in t a , b ;Test( in t i , in t j ) {
a = i ;b = j ;}/ / re turn t rue i f o is equal to the invok ing objectboolean equals(Test o) {i f (o .a == a && o.b == b) return t rue;e lse return fa lse;}}
class PassOb {publ ic stat ic void main(Str ing args[]) {Test ob1 = new Test(100, 22);Test ob2 = new Test(100, 22);Test ob3 = new Test(-1, -1);System.out.print ln("ob1 == ob2: " + ob1.equals(ob2));
System.out.print ln("ob1 == ob3: " + ob1.equals(ob3)); } }
USING OBJECTS AS PARAMETERS
What happens when one object is put equal to other
class Test {int a;Test(int i) {
a = i;}
Test incrByTen() {Test temp = new Test(a+10);return temp;}}
class RetOb {public static void main(String args[]) {
Test ob1 = new Test(2);Test ob2;ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);System.out.println("ob2.a: " + ob2.a);ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "+ ob2.a); }}
RETURNING OBJECTS
Declare a class date. It should have three int variables day, month, year. Declare a constructr that sets the default date to 14/08/1947. Declare another constructor that sets the date according to user requirements.
Declare a function addDate that adds two date objects.
CLASS ACTIVITY
DYNAMIC VARIABLES AND METHODS
All instance variables and methods we’ve created so far have been dynamic
Note: There is no “dynamic” keyword in JavaDynamic by default
In general, dynamic refers to things created at “run time” i.e. when the program is runningEvery object gets its own (dynamic) instance variables.Every object effectively gets its own copy of each dynamic methodThe absence of the keyword static before non-local variables and
methods means dynamic (one per object/instance)
STATIC VARIABLES
Static means “pertaining to the class in general”, not to an individual object
A variable may be declared (outside of a method) with the static keyword:
E.g. static int numTicketsSold;There is one variable numTickets for the class not one
per object!!! A static variable is shared by all instances (if any).
All instances may be able read/write it
A static variable that is public may be accessed by:ClassName.variableName
STATIC METHODS
A method may be declared with the static keyword
Static methods live at class level, not at object level
Static methods may access static variables and
methods, but not dynamic ones
public static int getNumSold(){return numTicketsSold;
}
9
STATIC METHODS (CONTD..)
A static method that is public can be accessed ClassName.methodName(args) double result = Math.sqrt(25.0); int numSold = Ticket.getNumberSold();
10
EXAMPLE: TICKETpublic class Ticket{ private static int numTicketsSold = 0; // shared private int ticketNum; // one per object public Ticket(){ numTicketsSold++; ticketNum = numTicketsSold; } public static int getNumberSold() { return numTicketsSold; } public int getTicketNumber() { return ticketNum; } public String getInfo(){ return "ticket # " + ticketNum + "; " + numTicketsSold + " ticket(s) sold."; }}
EXAMPLE: TICKET
Ticket.getNumberSold()
Ticket t1 = new Ticket(); t1.getTicketNum()
t1.getInfo()"ticket # 1; 1 ticket(s) sold." t1.getNumberSold()
Ticket t2 = new Ticket(); t2.getTicketNum()
t2.getInfo()"ticket # 2; 2 ticket(s) sold." t2.getInfo()"ticket # 2; 2 ticket(s) sold." t1.getInfo()"ticket # 1; 2 ticket(s) sold." Ticket.getNumberSold()
12
STATIC CONTEXT
To have standalone Java Application we need a public static void main(String args[]) method
The main method belongs to the class in which it is writtenIt must be static because, before your program
starts, there aren’t any objects to send messages to
This is a static context (a class method) You can send messages to objects, if you have some
objects: d1.bark(); You cannot send a message to yourself, or use any
instance variables - this is a static context, not an object
Non-static variable cannot be referenced from a static context
EXAMPLE
public class JustAdd { int x; int y; int z;
public static void main(String args[]) { x = 5; y = 10; z = x + y; }}
all are wrong
14
SOLUTION
public class JustAdd { int x; int y; int z;
public static void main(String args[]) { JustAdd myAdd = new JustAdd()
myAdd.doItAll()}
void doItAll() { x = 5; y = 10; z = x + y; }
}
15
WHEN TO USE STATIC
A variable should be static if: It logically describes the class as a whole There should be only one copy of it
A method should be static if: It does not use or affect the object that receives the
message (it uses only its parameters)
16
STATIC RULES
static variables and methods belong to the class in general, not to individual objects
The absence of the keyword static before non-local variables and methods means dynamic (one per object/instance)
A dynamic method can access all dynamic and static variables and methods in the same class
A static method can not access a dynamic variable (How could it choose or which one?)
A static method can not call a dynamic method (because it might access an instance variable)