Download presentation
Presentation is loading. Please wait.
Published byCarrie Byrd Modified over 9 years ago
1
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2.update (32, ‘B’); obj2 = obj1; obj1.output_info(); obj2.output_info(); } What is the output? information=45, moreInformation=A
2
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2 = obj1; obj2.update (32, ‘B’); obj1.output_info(); obj2.output_info(); } What is the output?
3
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2 = obj1; obj2.update (32, ‘B’); obj1.output_info(); obj2.output_info(); } What is the output? information=32, moreInformation=B Why?
4
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj1.update (45, ‘A’); obj2 = obj1; obj2.update (32, ‘B’); obj1.output_info(); obj2.output_info(); } What is the output? information=32, moreInformation=B Why? Both obj1 and obj2 point to the same object!
5
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj2.update (32, ‘B’); ABC obj3 = obj2; obj3.update (40, ‘A’); obj1.output_info(); obj2.output_info(); } What is the output?
6
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); obj2.update (32, ‘B’); ABC obj3 = obj2; obj3.update (40, ‘A’); obj1.output_info(); obj2.output_info(); } What is the output? information=42, moreInformation=A information=40, moreInformation=A
7
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); changer (obj2); obj1.output_info(); obj2.output_info(); } public void changer(ABC obj) { obj.update (100, ‘A’); obj = new ABC (); obj.update (30, ‘C’); } What is the output?
8
public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public void update(int info, char moreInfo) { information = info; moreInformation = moreInfo; } public void output_info() { System.out.println(“information=” + information + “, moreInformation=“ + moreInformation); } public class Test { public void main() { ABC obj1 = new ABC (42, 'A'); ABC obj2 = new ABC ((int) 32.99, ‘B'); changer (obj2); obj1.output_info(); obj2.output_info(); } public void changer(ABC obj) { obj.update (100, ‘A’); obj = new ABC (); obj.update (30, ‘C’); } What is the output? information=42, moreInformation=A information=100, moreInformation=A
9
public class Money { private int dollar = 0; private int cent = 0; public Money() {} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; } } public class Test { public void main() { Money m1 = new (); Money m2 = new (100, 2); System.out.println(“The balance of m1 is: “+ m1); System.out.println(“The balance of m2 is: “+ m2); if (m1 == m2) System.out.println(“equal.”); else System.out.println(“not-equal.”); } What is the issue?
10
public class Money { private int dollar = 0; private int cent = 0; public Money() {} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; } } public class Test { public void main() { Money m1 = new (); Money m2 = new (100, 2); System.out.println(“The balance of m1 is: “+ m1); System.out.println(“The balance of m2 is: “+ m2); if (m1 == m2) System.out.println(“equal.”); else System.out.println(“not-equal.”); } What is the issue? need to override the equals and toString methods!
11
public class Money { private int dollar = 0; private int cent = 0; public Money() {} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; } public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } } public class Test { public void main() { Money m1 = new (); Money m2 = new (100, 2); System.out.println(“The balance of m1 is: “+ m1); System.out.println(“The balance of m2 is: “+ m2); if (m1.equals(m2)) System.out.println(“equal.”); else System.out.println(“not-equal.”); } What is the issue? need to override the equals and toString methods!
12
public class Money { private static int num_accounts = 0; private int dollar = 0; private int cent = 0; public Money() {num_accounts ++;} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; num_accounts ++;} public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } public void setDollar(int in_d) { dollar = in_d; } public void setCent (int in_c) { cent = in_c; } public static void setMoney(int in_d, int in_c) { setDollar(in_d); setCent(in_c); } public static void getDollar() { return dollar; } public static void getCent() { return cent;} public static void output_balance() { System.out.println(“Your balance is :” + getDollar() + getCent();} public static void resetCounter() { num_accounts = 0; } } What is the issue?
13
public class Money { private static int num_accounts = 0; private int dollar = 0; private int cent = 0; public Money() {num_accounts ++;} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; num_accounts ++;} public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } public void setDollar(int in_d) { dollar = in_d; } public void setCent (int in_c) { cent = in_c; } public static void setMoney(int in_d, int in_c) { setDollar(in_d); setCent(in_c); } public static void getDollar() { return dollar; } public static void getCent() { return cent;} public static void output_balance() { System.out.println(“Your balance is :” + getDollar() + getCent();} public static void resetCounter() { num_accounts = 0; } } What is the issue? static functions cannot access non-static members!!
14
public class Money { private static int num_accounts = 0; private int dollar = 0; private int cent = 0; public Money() {num_accounts ++;} public Money(int in_d, int in_c) { dollar = in_d; cent = in_c; num_accounts ++;} public String toString() { return (dollar + “.” + cent);} public boolean equals(Money other_m) { return (dollar == other_m.dollar && cent == other_m.cent); } public void setDollar(int in_d) { dollar = in_d; } public void setCent (int in_c) { cent = in_c; } public void setMoney(int in_d, int in_c) { setDollar(in_d); setCent(in_c); } public void getDollar() { return dollar; } public void getCent() { return cent;} public void output_balance() { System.out.println(“Your balance is :” + getDollar() + getCent();} public static void resetCounter() { num_accounts = 0; } }
15
public class A { public A() {} public void method1() {} public void method2() {} private final A method3() { return A(); } } What is the issue? public class B : public A { public B() {} private void method1() {} public boolean method2(int ii) { return false; } public B method3() { return B(); } }
16
public class A { public A() {} public void method1() {} public void method2() {} private final A method3() { return A(); } } What is the issue? public class B : public A { public B() {} private void method1() {} public boolean method2(int ii) { return false; } public B method3() { return B(); } }
17
public class A { public A() {} public void method1() {} public void method2() {} private final A method3() { return new A(); } } What is the issue? public class B extends A { public B() {} public void method1() {} public boolean method2(int ii) { return false; } }
18
public class A { private int val1 = 0; public A() {} public void method1() {} public void method2() {} private A method3() { return new A(); } } What is the issue? public class B extends A { public B() {} public void method1() {} public boolean method2(int ii) { val1 ++; return false; } public B method3() { return new B(); } } public Test { public void static main() { A obj1 = new A(); B obj2 = new B(); obj1.method1(); A obj3 = obj1.method3(); obj1 = obj2.method3(); }
19
public class A { private int val1 = 0; public A() {} public void method1() {} public void method2() {} private A method3() { return new A(); } } What is the issue? public class B extends A { public B() {} public void method1() {} public boolean method2(int ii) { val1 ++; return false; } public B method3() { return new B(); } } public Test { public void static main() { A obj1 = new A(); B obj2 = new B(); obj1.method1(); A obj3 = obj1.method3(); obj1 = obj2.method3(); }
20
public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { super(0.); this (0.0, 1.0); } public Derived(double x_val, double y_val) { x = x_val; y = y_val; } …… } What is the issue?
21
public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { super(0.); this (0.0, 1.0); } public Derived(double x_val, double y_val) { x = x_val; y = y_val; } …… } What is the issue?
22
public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super (x_val, y_val); x = x_val; y = y_val; } …… } What is the issue?
23
public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super ((int) x_val, (int) y_val); x = x_val; y = y_val; } …… } What is the issue?
24
public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } public Base() {} …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super(); super ((int) x_val, (int) y_val); x = x_val; y = y_val; } …… } How about this?
25
public class Base { private int x, y = 0; public Base(int x_val, int y_val) { x = x_val; y = y_val; } public Base(int x_val) { x = x_val; } public Base() {} …… } public class Derived extends Base { private double x, y = 0; public Derived() { this (0.0, 1.0); } public Derived(double x_val, double y_val) { super(); super ((int) x_val, (int) y_val); x = x_val; y = y_val; } …… } How about this? There can have only one super!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.