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

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

Dec 13, 2015

Download

Documents

Merilyn Gregory
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Advanced Programming

Rabie A. [email protected]

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

Lecture 4

Page 2: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Chapter 6Interfaces and Inner Classes

2

Page 3: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q1

3

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

Page 4: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q2

4

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

Page 5: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q3

5

Can I define constants in an interface?

Page 6: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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;

}

Page 7: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q5

7

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

if (anObject instanceof Comparable) { . . . }

Page 8: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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

}

Page 9: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q7

9

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

Page 10: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q8

10

Why do we need interfaces while we have abstracts?

Page 11: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q9

11

Describe what actually happen in:

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

Employee copy = original;

copy.raiseSalary(10);

Page 12: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q10

12

How can we solve the previous problem?

Page 13: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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();

}

. . .

}

Page 14: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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;

}

}

Page 15: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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; }

}

Page 16: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q14

16

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

Page 17: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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(); }}}

Page 18: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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.  

Page 19: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q17

19

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

Page 20: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q18

20

What is the output of the following commands :

 

 

java ReflectionTest TalkingClock\$TimePrinter

or

javap -private TalkingClock\$TimePrinter

Page 21: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

21

Page 22: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q19

22

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

final TalkingClock this$0 ;

Page 23: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q20

23

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

Page 24: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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();

}

}

}

Page 25: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q22

25

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

Page 26: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

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();

}

Page 27: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q24

27

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

   

What was the original class structure ?

Page 28: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

28

Page 29: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q25

29

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

 

Page 30: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

30

Page 31: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q26

31

What is a “blank final variable”?

Page 32: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q27

32

What is the difference between the following two statements:

  public static final double SPEED_LIMIT = 55;

 

  public final double SPEED_LIMIT = 55;

Page 33: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q28

33

What does it mean to create Anonymous Inner Classes?

Page 34: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q29

34

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

Page 35: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q30

35

Define the concept of Proxies in java ?

Page 36: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q31

36

what is wrong with this code :

Page 37: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q32

37

Do you see a problem with the following assignments:

 

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

2- *px = *py;

Page 38: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q33

38

What might go wrong with the following code:

Page 39: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q34

39

What are the possible solutions to the previous problem ?

Page 40: Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org  vpro/ Lecture 4.

Q35

40

what is the summary of item 12 recommendations?