Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Programming with Java. Problem Solving The purpose of writing a program is to solve a problem The general steps in problem solving are: –Understand the.
Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java.
COMP201 Topic 8 / Slide 1 COMP201 Java Programming Topic 6: Interfaces and Inner Classes Readings: Chapter 6.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
COMP201 Java Programming Topic 6: Interfaces and Inner Classes Readings: Chapter 6.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
Java: Chapter 1 Computer Systems Computer Programming II.
Abstract and Nested Classes
The Java Programming Language
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
Chapter 8 Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Interfaces.
User-Defined Classes and ADTs
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Topic 5: Interfaces and Inner Classes
Lecture 4: Interface Design
null, true, and false are also reserved.
Java Programming Language
Inner Classes 29-Nov-18.
Chapter 1: Computer Systems
Focus of the Course Object-Oriented Software Development
Interfaces.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Chapter 8 Class Inheritance and Interfaces
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
Corresponds with Chapter 5
Inner Classes 25-Oct-19.
Presentation transcript:

Advanced Programming Rabie A. Ramadan vpro/ Lecture 4

Chapter 6 Interfaces 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(); 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?