40
Advanced Programming Rabie A. Ramadan [email protected] http://www.rabieramadan.org/classe s/2011/Advpro/ Lecture 4

Advanced Programming Rabie A. Ramadan [email protected] vpro/ Lecture 4

Embed Size (px)

Citation preview

Advanced Programming

Rabie A. [email protected]

http://www.rabieramadan.org/classes/2011/Advpro/

Lecture 4

Chapter 6Interfaces and Inner Classes

2

Q1

3

In java , A class can implement more than one interface (True/False)?

Q2

4

All methods in “interface” are automatically “protected” (True/False)?

Q3

5

Can I define constants in an interface?

Q4

6

Wwhat is wrong with the following code: public interface Comparable

{

int compareTo(Object other);

}

int compareTo(Object otherObject)

{

Employee other = (Employee) otherObject;

if (salary < other.salary) return -1;

if (salary > other.salary) return 1;

return 0;

}

Q5

7

if “Comparable” is an interface, what is the meaning of the following line of code :

if (anObject instanceof Comparable) { . . . }

Q6

8

What is wrong with the following code: public interface Moveable

{

void move(double x, double y);

}

A. public interface Powered extends Moveable

{

double milesPerGallon();

}

B.public interface Powered extends Moveable

{

double milesPerGallon();

double SPEED_LIMIT = 95; // a public static final constant

}

Q7

9

In interfaces, fields are always public and final only (true/false)?

Q8

10

Why do we need interfaces while we have abstracts?

Q9

11

Describe what actually happen in:

Employee original = new Employee("John Public", 50000);

Employee copy = original;

copy.raiseSalary(10);

Q10

12

How can we solve the previous problem?

Q11

13

clone () method initially is “protected” method and returns an object , can I implement it as follows? If No , how can I implement it? class Employee implements Cloneable

{

 

public Employee clone() throws CloneNotSupportedException

{

return (Employee) super.clone();

}

. . .

}

Q12

14

clone () method is used to copy the whole class methods and fields. Why do I need to clone the hireDay field in the following code separately ?  class Employee implements Cloneable

{

. . .

public Employee clone() throws CloneNotSupportedException

{

// call Object.clone()

Employee cloned = (Employee) super.clone();

cloned.hireDay = (Date) hireDay.clone()

return cloned;

}

}

Q13

15

Which of the following implementation to the clone () method is better than the other :

1. public Employee clone() throws CloneNotSupportedException

2.  public Employee clone()

{

try

{

return super.clone();

}

catch (CloneNotSupportedException e) { return null; }

}

Q14

16

what is the main idea behind “inner class”? why it is important ?

Q15

17

Is there anything wrong with this code : if nothing wrong , describe how the inner class accesses the fields of the outer class during the run time.

 public class TalkingClock

{

public TalkingClock(int interval, boolean beep) { . . . }

public void start() { . . . }

private int interval;

private boolean beep;

private class TimePrinter implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

Date now = new Date();

System.out.println("At the tone, the time is " + now);

if (beep) Toolkit.getDefaultToolkit().beep(); }}}

Q16

18

Which of the following statements will be passed by the compiler without any error message considering the previous implementation:

 

1. if (outer.beep) Toolkit.getDefaultToolkit().beep();

2.  

3. if (beep) Toolkit.getDefaultToolkit().beep();

4.  

Q17

19

Inner classes are handled by the virtual machine ? (True/False) Explain?

Q18

20

What is the output of the following commands :

 

 

java ReflectionTest TalkingClock\$TimePrinter

or

javap -private TalkingClock\$TimePrinter

21

Q19

22

If the following is the output of the previous instruction , what is the meaning of

final TalkingClock this$0 ;

Q20

23

What does it mean to create “Local Inner Class”?

Q21

24

what is missing in the following code to start a timer that is listening on TimePrinter:

 public void start()

{

class TimePrinter implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

Date now = new Date();

System.out.println("At the tone, the time is " + now);

if (beep) Toolkit.getDefaultToolkit().beep();

}

}

}

Q22

25

Local inner classes are always declared as “private” (True/False) ?

Q23

26

Why the following code does not compile:  public void start(int interval, boolean beep)

{

class TimePrinter implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

Date now = new Date();

System.out.println("At the tone, the time is " + now);

if (beep) Toolkit.getDefaultToolkit().beep();

}

}

ActionListener listener = new TimePrinter();

Timer t = new Timer(interval, listener);

t.start();

}

Q24

27

The following is the output of javap -private TalkingClock\$TimePrinter:

   

What was the original class structure ?

28

Q25

29

Reconstruct the original code from the following output of the following javap command

 

30

Q26

31

What is a “blank final variable”?

Q27

32

What is the difference between the following two statements:

  public static final double SPEED_LIMIT = 55;

 

  public final double SPEED_LIMIT = 55;

Q28

33

What does it mean to create Anonymous Inner Classes?

Q29

34

It is obvious that the constructors of Anonymous inner classes must be declared at the beginning of the class ? (True/False)

Q30

35

Define the concept of Proxies in java ?

Q31

36

what is wrong with this code :

Q32

37

Do you see a problem with the following assignments:

 

1- a[i] = a[j];

2- *px = *py;

Q33

38

What might go wrong with the following code:

Q34

39

What are the possible solutions to the previous problem ?

Q35

40

what is the summary of item 12 recommendations?