0% found this document useful (0 votes)
70 views

Classes: A Deeper Look: Dr. Zara Hamid

The document discusses object-oriented programming concepts in Java including method overloading, passing objects as parameters, returning objects from methods, static and dynamic variables and methods, and when to use static variables and methods. It provides examples of declaring classes with overloaded methods, passing objects between methods, and using static and non-static variables and methods appropriately. Key topics covered are distinguishing static and dynamic elements, rules for accessing each from static contexts like the main method, and guidelines for when elements should be static or dynamic.

Uploaded by

Umer Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Classes: A Deeper Look: Dr. Zara Hamid

The document discusses object-oriented programming concepts in Java including method overloading, passing objects as parameters, returning objects from methods, static and dynamic variables and methods, and when to use static variables and methods. It provides examples of declaring classes with overloaded methods, passing objects between methods, and using static and non-static variables and methods appropriately. Key topics covered are distinguishing static and dynamic elements, rules for accessing each from static contexts like the main method, and guidelines for when elements should be static or dynamic.

Uploaded by

Umer Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Dr.

Zara Hami d
CLASSES: A DEEPER LOOK
cl ass Over l oadDemo {
voi d t est ( ) {

Syst em. out . pr i nt l n( "No par amet er s") ;
/ / Over l oad t est f or one i nt eger par amet er.
voi d t est ( i nt a) {

Syst em. out . pr i nt l n( "a: " + a) ;
}
/ / Over l oad t est f or t wo i nt eger par amet er s.
voi d t est ( i nt a, i nt b) {
Syst em. out . pr i nt l n( "a and b: " + a + " " + b) ;
}
/ / over l oad t est f or a doubl e par amet er
doubl e t est ( doubl e a) {
Syst em. out . pr i nt l n( "doubl e a: " + a) ;
r et ur n a*a;
} }
cl ass Over l oad {
publ i c st at i c voi d mai n( St r i ng ar gs[ ] ) {
Over l oadDemo ob = new Over l oadDemo( ) ;
doubl e r esul t ;
/ / cal l al l ver si ons of t est ( )
ob. t est ( ) ;
ob. t est ( 10) ;
ob. t est ( 10, 20) ;
r esul t = ob. t est ( 123. 25) ;
Syst em. out . pr i nt l n( "Resul t of
ob. t est ( 123. 25) :
+ r esul t ) ;
}
}
OVERLOADING METHODS
cl ass Test {
i nt a, b;
Test ( i nt i , i nt j ) {

a = i ;
b = j ;
}
/ / r et ur n t r ue i f o i s equal t o t he i nvoki ng
obj ect
bool ean equal s( Test o) {
i f ( o. a == a && o. b == b) r et ur n t r ue;
el se r et ur n f al se;
}
}
cl ass PassOb {
publ i c st at i c voi d mai n(St ri ng args[ ] ) {
Test ob1 = new Test (100, 22);
Test ob2 = new Test (100, 22);
Test ob3 = new Test ( -1, -1);
Syst em. out . pri nt l n("ob1 == ob2: " +
ob1. equal s(ob2));

Syst em. out . pri nt l n("ob1 == ob3: " +
ob1. equal s(ob3));
}
}
USING OBJECTS AS PARAMETERS
What happens when one object is put equal to other
cl ass Test {
i nt a;
Test( i nt i ) {
a = i ;
}
Test i ncrByTen( ) {
Test temp = new Test( a+10) ;
return temp;
}
}
cl ass RetOb {
publ i c stati c voi d mai n( Stri ng args[ ] ) {

Test ob1 = new Test( 2) ;
Test ob2;
ob2 = ob1. i ncrByTen( ) ;

System. out . pri ntl n( "ob1. a: " + ob1. a) ;
System. out . pri ntl n( "ob2. a: " + ob2. a) ;
ob2 = ob2. i ncrByTen( ) ;

System. out . pri ntl n( "ob2. a af ter second
i ncrease: "
+ ob2. a) ;
}
}
RETURNING OBJECTS
Decl are a cl ass date. It shoul d have three i nt vari ables day,
month, year. Decl are a constructr that sets the default date to
14/08/1947. Decl are another constructor that sets the date
according to user requi rements.

Decl are a functi on addDate that adds two date obj ects.
CLASS ACTIVITY
DYNAMIC VARIABLES AND METHODS
All instance variables and methods weve created
so far have been dynamic
Note: There is no dynamic keyword in Java
Dynamic by default

In general, dynamic refers to things created at
run time i.e. when the program is running

Every object gets its own (dynamic) instance variables.

Every object effectively gets its own copy
of each dynamic method

The 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 stati c method that i s public can be accessed
ClassName.methodName(args)
double result = Math.sqrt(25.0);
int numSold = Ticket.getNumberSold();

10
EXAMPLE: TICKET
public 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 written
It must be static because, before your program starts,
there arent 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
publ i c cl ass JustAdd {
i nt x;
i nt y;
i nt z;

publ i c stati c voi d mai n(Stri ng args[ ]) {
JustAdd myAdd = new JustAdd()
myAdd.doItAll()
}

voi d doI tAl l () {
x = 5;
y = 10;
z = x + y;
}
}
15
WHEN TO USE STATIC
A vari able shoul d be stati c i f:
It logically describes the class as a whole
There should be only one copy of it

A method shoul d be stati c i f:
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)

You might also like