CS1101: Programming Methodology Recitation 4 Class Behavior in Java - PowerPoint PPT Presentation

About This Presentation
Title:

CS1101: Programming Methodology Recitation 4 Class Behavior in Java

Description:

They share access to a class variable counter1 with value 0. Output of BDemo ? ... counter1 is a class variable ... to access any instance variable of its class ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 10
Provided by: leemo4
Category:

less

Transcript and Presenter's Notes

Title: CS1101: Programming Methodology Recitation 4 Class Behavior in Java


1
CS1101 Programming MethodologyRecitation 4
Class Behavior in Java
2
public class A private int data1 1 private
double data2 private boolean data3 private
String data4 public A() // no body
used public int getData1() return
data1 public double getData2() return
data2 public boolean getData3() return
data3 public String getData4() return
data4
Initialize instance variables
Reference type variable set to null
public class ADemo public static void
main(String args) A a new
A() System.out.println(a.getData1()) System.ou
t.println(a.getData2()) System.out.println(a.get
Data3()) System.out.println(a.getData4())
Output of ADemo ?
3
public class B private static int counter1
0 private int counter2 public B(int n)
counter2 n public void
incrementCounter1() counter1 public
void incrementCounter2() counter2 publ
ic int getCounter1() return
counter1 public int getCounter2()
return counter2
public class BDemo public static void
main(String args) B b1 new B(0) B b2
new B(0) b1.incrementCounter1() b1.incrementC
ounter2() b1.incrementCounter2() b1.incrementC
ounter2() b2.incrementCounter1() b2.increment
Counter2() System.out.println(b1.getCounter1())
System.out.println(b1.getCounter2()) System.o
ut.println(b2.getCounter1()) System.out.println(
b2.getCounter2())
Output of BDemo ?
Two B reference variable b1 and b2 are
created. These objects each have an instance
variable counter2 with value 0. They share access
to a class variable counter1 with value 0
4
public class BDemo public static void
main(String args) B b1 new B(0) B b2
new B(0) b1.incrementCounter1() b1.incrementC
ounter2() b1.incrementCounter2() b1.incrementC
ounter2() b2.incrementCounter1() b2.increment
Counter2() System.out.println(b1.getCounter1())
System.out.println(b1.getCounter2()) System.o
ut.println(b2.getCounter1()) System.out.println(
b2.getCounter2())
Output of BDemo 2 3 2 1
  • counter1 is a class variable
  • same value is manipulated regardless of whether
    it is accessed via b1 or b2
  • value of counter1 was incremented two times (0 ?
    2)
  • counter2 is an instance variable
  • increment operations of b1 and b2 affect
    different instances of counter2
  • value of b1.counter2 (0 ? 3)
  • value of b2.counter2 (0 ? 1)

5
public class C private int count
0 public C (int n) count
n public void f (int n) count
n public void g (int n) int
count n count n
  • Which methods in C are in principle allowed to
    access instance variable count ?
  • Which methods in C legally set instance variable
    count ?
  • Which methods in C illegally attempt to set
    instance variable count ?
  • Which methods in C do not modify instance
    variable count ?

6
public class C private int count
0 public C (int n) count
n public void f (int n) count
n public void g (int n) int
count n count n
Access instance variable count
Define local variable count Increment local
variable count
  • Which methods in C are in principle allowed to
    access instance variable count ?
  • ALL because they are instance methods
  • An instance method is allowed to access any
    instance variable of its class
  • Which methods in C legally set instance variable
    count ?
  • Methods C() and f()
  • Which methods in C illegally attempt to set
    instance variable count ?
  • None
  • Which methods in C do not modify instance
    variable count ?
  • g()

7
public class D private String data public
D(String s) data s public String get()
return data public Object clone()
String val get() return new
D(val) public class E private String
data public E(String s) data
s public String get() return
data public Object clone() String str
get() String val new String(str) return
new E(val)
Class D and E define String variable data and
method get() to return value of data. But they
have different overriddings of method clone()
public class DEDemo public static void
main(String args) String word
"strengths" D d1 new D(word)
E e1 new E(word) D d2 (D)
d1.clone() E e2 (E) e1.clone()
boolean flag1 d1.get() d1.get()
boolean flag2 d1.get() d2.get()
boolean flag3 e1.get() e1.get()
boolean flag4 e1.get() e2.get()
System.out.println(flag1)
System.out.println(flag2)
System.out.println(flag3)
System.out.println(flag4)
Output of DEDemo ?
8
String word "strengths" D d1 new D(word) E
e1 new E(word) D d2 (D) d1.clone() E e2
(E) e1.clone()
String val get() return new D(val)
String str get() String val new
String(str) return new E(val)
e1
9
public class DEDemo public static void
main(String args) String word
"strengths" D d1 new D(word)
E e1 new E(word) D d2 (D)
d1.clone() E e2 (E) e1.clone()
boolean flag1 d1.get() d1.get()
boolean flag2 d1.get() d2.get()
boolean flag3 e1.get() e1.get()
boolean flag4 e1.get() e2.get()
System.out.println(flag1)
System.out.println(flag2)
System.out.println(flag3)
System.out.println(flag4)
Output of DEDemo true true true false
Write a Comment
User Comments (0)
About PowerShow.com