Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

Size: px
Start display at page:

Download "Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski"

Transcription

1 CS Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University February 11, 2008 / Lecture 4

2 Outline Course Status Course Information & Schedule Assignments Questions Polymorphism Polymorphism Re-visisted Pitfalls Constructors & Polymorphism Other Containers Overview ArrayList HashMap Generics Exceptions Concepts Catching Exceptions Throwing Exceptions Runtime Exceptions Restrictions and Pitfalls Midterm Review

3 Course Information Instructor Andy Mroczkowski Office Location: UC 147 Office Hours: Monday 6-7 & evenings by appointment Course CS Java Web: uamroczk/cs190 Book: Thinking in Java, 4th Edition Previous edition available for free at wwww.mindview.net/books/tij/ Course materials adapted from Nadya Belov

4 Schedule 1 Jan 7 Jan 14 Jan 21 Jan 28 Feb 4 Feb 11 Feb 18 Mar 3 Feb 25 Mar 10 Mar 17 Course Overview, Introduction to Java No Class No Class (University Holiday) Object Oriented Concepts, Control Statements Access Control, Intialization & Cleanup, Reuse, Interfaces, Containers, Exceptions, Polymorphism, Midterm Revie Midterm, Inner Classes, Strings File I/O, Threading Graphical User Interfaces (AWT, Swing) Networking, Special Topics (Encryption, Reflection) Final 1 Also subject to change

5 Grades Assignment 3 is graded Grades reported in comments in BB Vista See me about grading issues

6 Assignment 4 Questions? Push back due date to 22 Feb

7 Assignment 4 Questions? Push back due date to 22 Feb

8 Midterm Monday Feb 18th

9 Midterm Monday Feb 18th

10 Questions Difference between: 2 String[] args String args[] String[] args is preferred String args[] is discouraged 2

11 Questions Difference between: 2 String[] args String args[] String[] args is preferred String args[] is discouraged 2

12 Questions Do super calls have to occur first in the constructor? Yes.

13 Questions Do super calls have to occur first in the constructor? Yes.

14 Questions Why would you want several things to point to the same object? Singleton Only one instance for the entire VM

15 Questions Why would you want several things to point to the same object? Singleton Only one instance for the entire VM

16 Questions Why would you want several things to point to the same object? Singleton Only one instance for the entire VM

17 Class Participation! Let s write a Singleton! Hooray!

18 Class Participation! Let s write a Singleton! Hooray!

19 Singleton - Implementation p u b l i c class Singleton { protected Singleton ( ) { } s t a t i c p r i v a t e Singleton _instance = n u l l ; s t a t i c p u b l i c Singleton getinstance ( ) { i f (! _instance ) { _instance = new Singleton ( ) ; } r e t u r n _instance ; }

20 Polymorphism

21 Upcasting Cast an object to something "up" the inheritance tree, to make it more generic Why?

22 Upcasting Cast an object to something "up" the inheritance tree, to make it more generic Why?

23 Upcasting Cast an object to something "up" the inheritance tree, to make it more generic Why?

24 Instruments (again) interface Instrument void play() String what() void adjust() Wind void play() String what() void adjust() implements implements Percussion void play() String what() void adjust() implements Stringed void play() String what() void adjust() extends Woodwind void play() String what() void adjust() extends Brass void play() String what() void adjust()

25 Instruments (again) public class Music2 { public s t a t i c void tune ( Wind i ) { i. play ( Note. MIDDLE_C ) ; } public s t a t i c void tune ( Stringed i ) { i. play ( Note. MIDDLE_C ) ; } public s t a t i c void tune ( Brass i ) { i. play ( Note. MIDDLE_C ) ; } } public s t a t i c void main ( String [ ] args args ) { Wind f l u t e = new Wind ( ) ; Stringed v i o l i n = new Stringed ( ) ; Brass frenchhorn = new Brass ( ) ; tune ( f l u t e ) ; tune ( v i o l i n ) ; tune ( frenchhorn ) ; }

26 Instruments (again) (improved) public class Music2 { public s t a t i c void tune ( Instrument i ) { i. play ( Note. MIDDLE_C ) ; } } public s t a t i c void main ( String [ ] args args ) { Wind f l u t e = new Wind ( ) ; Stringed v i o l i n = new Stringed ( ) ; Brass frenchhorn = new Brass ( ) ; tune ( f l u t e ) ; tune ( v i o l i n ) ; tune ( frenchhorn ) ; }

27 How does this work? Magic! Actually, late binding Java doesn t decide which method to call untill run time.

28 How does this work? Magic! Actually, late binding Java doesn t decide which method to call untill run time.

29 How does this work? Magic! Actually, late binding Java doesn t decide which method to call untill run time.

30 How does this work? Magic! Actually, late binding Java doesn t decide which method to call untill run time.

31 Calling Overridden Methods What if I want to call a method that has been overridden directly? You can t, sorry.

32 Calling Overridden Methods What if I want to call a method that has been overridden directly? You can t, sorry.

33 Pitfalls Overriding private methods private methods are automatically final static methods don t behave polymorphically must explicitly say Class.method

34 Constructors & Polymorphism Order of Initialization 1. Parent classes Constructors are called, recursively Default Constructor if no super() call 2. Members are initialized in order of declaration 3. Constructor s body is executed

35 Inheritance - Constructors class C i r c l e { private double r a d i u s ; public C i r c l e ( double r ) { r a d ius = r ; } private C i r c l e ( ) { } } class Sphere extends C i r c l e { Sphere ( double r ) { super ( r ) ; } } public double getvolume ( ) ;

36 Polymorphism inside Constructors What happens when we call a polymorphic method inside a constructor? The overridden version is used. Is this dangerous? It can be. Lesson: Be careful in your constructors.

37 Polymorphism inside Constructors What happens when we call a polymorphic method inside a constructor? The overridden version is used. Is this dangerous? It can be. Lesson: Be careful in your constructors.

38 Polymorphism inside Constructors What happens when we call a polymorphic method inside a constructor? The overridden version is used. Is this dangerous? It can be. Lesson: Be careful in your constructors.

39 Polymorphism inside Constructors What happens when we call a polymorphic method inside a constructor? The overridden version is used. Is this dangerous? It can be. Lesson: Be careful in your constructors.

40 Downcasting interface Instrument void play() String what() void adjust() Wind void play() String what() void adjust() implements implements Percussion void play() String what() void adjust() implements Stringed void play() String what() void adjust() extends Woodwind void play() String what() void adjust() extends Brass void play() String what() void adjust() What happens we we cast down the inheritance tree?

41 Downcasting All casts in Java are checked There are no unsafe casts

42 New in Java 5 - Covariant Return Types Overridden method may return a different type, as long as it is derived from the return type of the method it is overriding

43 Containers Collections & Maps Iterators Common Examples Generics

44 Containers Collections & Maps Iterators Common Examples Generics

45 Collections & Maps Collection Sequence of elements with one or more restrictions List, Array, Stack, Queue Map Look up elements by key aka. Dictionary, Hash Table

46 Iterators Moves through a list and select each object in correct order Again, information hiding Provided by the list object

47 Iterator Example n0 n1 n2 n3 n4 n5

48 ArrayList An ArrayList holds a list of ordered Object objects. Basic Operations Add elements Retrieve elements by index Remove elements by index Get the number of elements Get all elements

49 ArrayList An ArrayList holds a list of ordered Object objects. Basic Operations Add elements Retrieve elements by index Remove elements by index Get the number of elements Get all elements

50 ArrayList Creating and Adding Elements import java. u t i l. A r r a y L i s t ; class A r r a y L i s t T e s t { public s t a t i c void main ( S t r i n g [ ] args ) { A r r a y L i s t autobots = new A r r a y L i s t ( ) ; } } autobots. add ( " Jazz " ) ; autobots. add ( " Bumblebee " ) ; autobots. add ( " Wheeljack " ) ;

51 ArrayList Accessing an Element { } S t r i n g a1 = autobots. get ( 0 ) ; S t r i n g a2 = autobots. get ( 1 ) ; Is there a problem?

52 ArrayList Accessing an Element { } S t r i n g a1 = autobots. get ( 0 ) ; S t r i n g a2 = autobots. get ( 1 ) ; Is there a problem?

53 ArrayList Accessing an Element - Fixed { } S t r i n g a1 = ( S t r i n g ) autobots. get ( 0 ) ; S t r i n g a2 = ( S t r i n g ) autobots. get ( 1 ) ;

54 ArrayList Iterating Over All Elements import java. u t i l. I t e r a t o r ; { } I t e r a t o r i = autobots. i t e r a t o r ( ) ; while ( i. hasnext ( ) ) { System. out. p r i n t l n ( i. next ( ) ) ; }

55 HashMap An HashMap holds a number of Object objects that are associated with an Object key. Basic Operations Add elements Retrieve elements by key Remove elements by key Get the number of elements Get all the keys Get all the values

56 HashMap An HashMap holds a number of Object objects that are associated with an Object key. Basic Operations Add elements Retrieve elements by key Remove elements by key Get the number of elements Get all the keys Get all the values

57 HashMap Creating and Adding Elements import java. u t i l. HashMap ; import java. u t i l. A r r a y L i s t ; class HashMapTest { public s t a t i c void main ( S t r i n g [ ] args ) { } } HashMap transformers = new HashMap ( ) ; transformers. put ( new A r r a y L i s t ( ), " autobots " ) ; transformers. put ( new A r r a y L i s t ( ), " decepticons " ) ;

58 HashMap Accessing an Element A r r a y L i s t goodguys = ( A r r a y L i s t ) transformers. get ( " autobots " ) ; A r r a y L i s t badguys = ( A r r a y L i s t ) transformers. get ( " decepticons " ) ;

59 HashMap Removing an Element transformers. remove ( " decepticons " ) ; transformers. remove ( " dinobots " ) ;

60 HashMap Removing an Element transformers. remove ( " decepticons " ) ; transformers. remove ( " dinobots " ) ; We didn t insert "dinobots" - is that ok?

61 Make containers type-safe New in Java 5 Example Generics A r r a y L i s t < String > autobots = new A r r a y L i s t < String > ( ) ; autobots. add ( " Jazz " ) ; autobots. add ( " Bumblebee " ) ; autobots. add ( " Wheeljack " ) ; S t r i n g a1 = autobots. get ( 0 ) ; S t r i n g a2 = autobots. get ( 1 ) ;

62 Make containers type-safe New in Java 5 Example Generics A r r a y L i s t < String > autobots = new A r r a y L i s t < String > ( ) ; autobots. add ( " Jazz " ) ; autobots. add ( " Bumblebee " ) ; autobots. add ( " Wheeljack " ) ; S t r i n g a1 = autobots. get ( 0 ) ; S t r i n g a2 = autobots. get ( 1 ) ;

63 Exceptions

64 Exceptions Concepts Catching Exceptions finally Throwing Exceptions Constructors & Interfaces Restrictions & Pitfalls

65 Exceptions - Concepts Exceptional Condition Separate error handling from normal logic Handle errors at the appropriate context

66 The Return Code Way FILE i f p, ofp ; char mode = " r " ; i f p = fopen ( " i n f i l e ", mode ) ; i f ( i f p == NULL) { f p r i n t f ( s t d e r r, "Can t open i n p u t f i l e i n f i l e! \ n " ) ; e x i t ( 1 ) ; } ofp = fopen ( " o u t f i l e ", "w" ) ; i f ( ofp == NULL) { f p r i n t f ( s t d e r r, "Can t open output f i l e %s! \ n ", o u t f i l e ) ; e x i t ( 1 ) ; }

67 The Return Exception Way t r y { BufferedReader i n = new BufferedReader (new FileReader ( " i n f i l e " ) ) ; BufferedReader i n = new B u f f e r e d W r i t e r (new F i l e W r i t e r ( " o u t f i l e " ) ) ; } catch ( IOException e ) { / / a l l error handling here e. p r i ntstacktrace ( ) ; }

68 Causing an Exception A r r a y L i s t < String > autobots = new A r r a y L i s t < String > ( ) ; autobots. add ( " Jazz " ) ; autobots. add ( " Bumblebee " ) ; autobots. add ( " Wheeljack " ) ; S t r i n g a1 = autobots. get ( 0 ) ; S t r i n g a2 = autobots. get ( 1 ) ; S t r i n g a3 = autobots. get ( 5 ) ; / /???

69 Causing an Exception - Results $ java ArrayListTest Jazz Bumblebee Wheeljack Exception in thread "main" java.lang.indexoutofboundsexception: Index: 5, Size: 3 at java.util.arraylist.rangecheck(arraylist.java:546) at java.util.arraylist.get(arraylist.java:321) at ArrayListTest.main(ArrayListTest.java:19)

70 What kinds of exceptions are out there? ClassNotFoundException CloneNotSupportedException FontFormatException IllegalAccessException InstantiationException InterruptedException IOException NoSuchFieldException NoSuchMethodException RuntimeException TimeoutException UnsupportedAudioFileException To name a few

71 What kinds of exceptions are out there? ClassNotFoundException CloneNotSupportedException FontFormatException IllegalAccessException InstantiationException InterruptedException IOException NoSuchFieldException NoSuchMethodException RuntimeException TimeoutException UnsupportedAudioFileException To name a few

72 What kinds of exceptions are out there? ClassNotFoundException CloneNotSupportedException FontFormatException IllegalAccessException InstantiationException InterruptedException IOException NoSuchFieldException NoSuchMethodException RuntimeException TimeoutException UnsupportedAudioFileException To name a few

73 Catching Exceptions

74 try and catch Code that may throw exceptions must be wrapped in a try block Exceptions are caught in catch block Basic Structure t r y { / / code t h a t may throw an exception } catch ( SomeException e1 ) { / / handle SomeException } catch ( OtherException e2 ) { / / handle OtherException }

75 Matching Exceptions Match in order First match, not necessarily best match

76 Matching Exceptions - Example t r y { S t r i n g a3 = autobots. get ( 5 ) ; / /??? } catch ( Exception e1 ) { e1. printstacktrace ( ) ; } catch ( IndexOutOfBoundsException e2 ) { System. e r r. p r i n t l n ( " Index out of bounds " ) ; }

77 finally Cleanup Always executed even after return

78 finally Cleanup Always executed even after return

79 finally Cleanup Always executed even after return

80 Stack Traces Exception in thread "main" java.lang.indexoutofboundsexception: Index: 5, Size: 3 at java.util.arraylist.rangecheck(arraylist.java:546) at java.util.arraylist.get(arraylist.java:321) at ArrayListTest.main(ArrayListTest.java:19)

81 Throwing Exceptions You can throw your own Exceptions with: thow and throws

82 Throwing Exceptions - Example class SimpleException extends Exception { } public class I n h e r i t i n g E x c e p t i o n s { public void f ( ) throws SimpleException { System. out. p r i n t l n ( " Throw SimpleException from f ( ) " ) ; throw new SimpleException ( ) ; } public s t a t i c void main ( String [ ] args ) { InheritingExceptions ie = new InheritingExceptions ( ) ; t r y { i e. f ( ) ; } catch ( SimpleException e ) { System. out. p r i n t l n ( " Caught i t! " ) ; } } }

83 Throwing Exceptions - Output Throw SimpleException form f() Caught it!

84 Exception Specification All the exceptions thrown by a method must be specified Example void f o o ( ) throws TimeoutException, MissingFileException, PurpleMonkeyDishwasherException What about specifying exceptions it doesn t actually throw?

85 Exception Specification All the exceptions thrown by a method must be specified Example void f o o ( ) throws TimeoutException, MissingFileException, PurpleMonkeyDishwasherException What about specifying exceptions it doesn t actually throw?

86 Exception Specification All the exceptions thrown by a method must be specified Example void f o o ( ) throws TimeoutException, MissingFileException, PurpleMonkeyDishwasherException What about specifying exceptions it doesn t actually throw?

87 Re-throwing Exceptions Wrapping Throw as uncaught zuh?

88 Re-throwing Exceptions Wrapping Throw as uncaught zuh?

89 Re-throwing Exceptions Wrapping Throw as uncaught zuh?

90 Constructors & Interfaces Constructors can throw exceptions Why is this cool? Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Interfaces and abstract methods can specify exceptions Concrete methods in abstract classes can throw them

91 Constructors & Interfaces Constructors can throw exceptions Why is this cool? Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Interfaces and abstract methods can specify exceptions Concrete methods in abstract classes can throw them

92 Constructors & Interfaces Constructors can throw exceptions Why is this cool? Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Interfaces and abstract methods can specify exceptions Concrete methods in abstract classes can throw them

93 Constructors & Interfaces Constructors can throw exceptions Why is this cool? Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Interfaces and abstract methods can specify exceptions Concrete methods in abstract classes can throw them

94 Constructors & Interfaces Constructors can throw exceptions Why is this cool? Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Interfaces and abstract methods can specify exceptions Concrete methods in abstract classes can throw them

95 Runtime Exceptions ArithmeticException BufferOverflowException BufferUnderflowException ClassCastException IllegalArgumentException NegativeArraySizeException NoSuchElementException NullPointerException To name a few

96 Runtime Exceptions ArithmeticException BufferOverflowException BufferUnderflowException ClassCastException IllegalArgumentException NegativeArraySizeException NoSuchElementException NullPointerException To name a few

97 Runtime Exceptions Do not need to be explicitly caught Although you can catch them if you want to Uncaught runtime exceptions print the stack trace and exit

98 Runtime Exceptions Do not need to be explicitly caught Although you can catch them if you want to Uncaught runtime exceptions print the stack trace and exit

99 Restrictions and Pitfalls When you override a method, you may only throw exceptions specified by the base class Constructors Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Exceptions in interfaces The Lost Exception

100 Restrictions and Pitfalls When you override a method, you may only throw exceptions specified by the base class Constructors Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Exceptions in interfaces The Lost Exception

101 Restrictions and Pitfalls When you override a method, you may only throw exceptions specified by the base class Constructors Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Exceptions in interfaces The Lost Exception

102 Restrictions and Pitfalls When you override a method, you may only throw exceptions specified by the base class Constructors Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Exceptions in interfaces The Lost Exception

103 Restrictions and Pitfalls When you override a method, you may only throw exceptions specified by the base class Constructors Can throw anything they want, regardless of the base class Cannot catch any exceptions thrown by the base class Exceptions in interfaces The Lost Exception

104 The Lost Exception class WorldWillExplodeException extends Exception { } class T r i v i a l E x c e p t i o n extends Exception { } public class LostMessage { void f ( ) throws WorldWillExplodeException { throw new WorldWillExplodeException ( ) ; } void dispose ( ) throws T r i v i a l E x c e p t i o n { throw new TrivialException ( ) ; } } public s t a t i c void main ( String [ ] args ) { t r y { LostMessage lm = new LostMessage ( ) ; t r y { lm. f ( ) ; } f i n a l l y { lm. dispose ( ) ; } } catch ( Exception e ) { System. out. p r i n t l n ( e ) ; } }

105 Midterm Format Similar to Quiz Find the bug Write a simple program What does this program do? True or False

106 Java Basics + Virtual Machine + Class file + Garbage collection - Java History Midterm Review OOP Concepts + Why OO? (Information hiding, Re-use, Encapsulation) + Inheritance + Polymorphism

107 Midterm Review Operators + if - else and switch + for and while loops + mathematical operators (+ - * /) + relational operators (< > ==!=) + boolean operators (&&!) + increment operators (++ ) + new operator + special string operators - bitwise operators - ternary operator - operator precedence

108 Midterm Review Loops + for + while Access Control + scope + packages + access specifiers (default, public, private, protected)

109 Initialization & Cleanup + constructors + new operator + finalize method - clone Re-use + Inheritance + Composition + final keyword - Delegation Midterm Review

110 Midterm Review Interfaces + Abstract classes + Interfaces + implements vs extends

111 Midterm Review Polymorphism Containers Exceptions

112 Class Participation 2! Let s do some True-False! Hooray!

113 Class Participation 2! Let s do some True-False! Hooray!

Introduction Course Information Object Oriented Concepts Operators & Control Statements Assignment 3. CS Java. Introduction to Java

Introduction Course Information Object Oriented Concepts Operators & Control Statements Assignment 3. CS Java. Introduction to Java CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University January 28, 2008 / Lecture 2 Introduction Course Information Basic Course Information

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University March 10, 2008 / Lecture 8 Outline Course Status Course Information & Schedule

More information

Selected Java Topics

Selected Java Topics Selected Java Topics Introduction Basic Types, Objects and Pointers Modifiers Abstract Classes and Interfaces Exceptions and Runtime Exceptions Static Variables and Static Methods Type Safe Constants Swings

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions Overview Problem: Can we detect run-time errors and take corrective action? Try-catch Test for a variety of different program situations

More information

EXCEPTION HANDLING. Summer 2018

EXCEPTION HANDLING. Summer 2018 EXCEPTION HANDLING Summer 2018 EXCEPTIONS An exception is an object that represents an error or exceptional event that has occurred. These events are usually errors that occur because the run-time environment

More information

15CS45 : OBJECT ORIENTED CONCEPTS

15CS45 : OBJECT ORIENTED CONCEPTS 15CS45 : OBJECT ORIENTED CONCEPTS QUESTION BANK: What do you know about Java? What are the supported platforms by Java Programming Language? List any five features of Java? Why is Java Architectural Neutral?

More information

CS 3 Introduction to Software Engineering. 3: Exceptions

CS 3 Introduction to Software Engineering. 3: Exceptions CS 3 Introduction to Software Engineering 3: Exceptions Questions? 2 Objectives Last Time: Procedural Abstraction This Time: Procedural Abstraction II Focus on Exceptions. Starting Next Time: Data Abstraction

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 7 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 7 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Essential

More information

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun CS 61B Data Structures and Programming Methodology July 7, 2008 David Sun Announcements You ve started (or finished) project 1, right? Package Visibility public declarations represent specifications what

More information

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance (extends) Use relationship Inheritance 1..n Unidirectional association Inheritance ("extends") Use relationship Implementation ("implements") What is inherited public, protected methods public, proteced attributes What is not inherited

More information

Lecture 19 Programming Exceptions CSE11 Fall 2013

Lecture 19 Programming Exceptions CSE11 Fall 2013 Lecture 19 Programming Exceptions CSE11 Fall 2013 When Things go Wrong We've seen a number of run time errors Array Index out of Bounds e.g., Exception in thread "main" java.lang.arrayindexoutofboundsexception:

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? QUIZ Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed? Or Foo(x), depending on how we want the initialization

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of February 23, 2013 Abstract Handling errors Declaring, creating and handling exceptions

More information

Index COPYRIGHTED MATERIAL

Index COPYRIGHTED MATERIAL Index COPYRIGHTED MATERIAL Note to the Reader: Throughout this index boldfaced page numbers indicate primary discussions of a topic. Italicized page numbers indicate illustrations. A abstract classes

More information

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11 Administration Exceptions CS 99 Summer 2000 Michael Clarkson Lecture 11 Lab 10 due tomorrow No lab tomorrow Work on final projects Remaining office hours Rick: today 2-3 Michael: Thursday 10-noon, Monday

More information

BBM 102 Introduction to Programming II Spring Exceptions

BBM 102 Introduction to Programming II Spring Exceptions BBM 102 Introduction to Programming II Spring 2018 Exceptions 1 Today What is an exception? What is exception handling? Keywords of exception handling try catch finally Throwing exceptions throw Custom

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of February 23, 2013 Abstract Handling errors Declaring, creating and handling exceptions

More information

Here is a hierarchy of classes to deal with Input and Output streams.

Here is a hierarchy of classes to deal with Input and Output streams. PART 15 15. Files and I/O 15.1 Reading and Writing Files A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data

More information

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Interfaces - An Instrument interface - Multiple Inheritance

More information

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Marenglen Biba Exception handling Exception an indication of a problem that occurs during a program s execution. The name exception implies that the problem occurs infrequently. With exception

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring 2017 BBM 102 Introduction to Programming II Spring 2017 Exceptions Instructors: Ayça Tarhan, Fuat Akal, Gönenç Ercan, Vahid Garousi Today What is an exception? What is exception handling? Keywords of exception

More information

COE318 Lecture Notes Week 10 (Nov 7, 2011)

COE318 Lecture Notes Week 10 (Nov 7, 2011) COE318 Software Systems Lecture Notes: Week 10 1 of 5 COE318 Lecture Notes Week 10 (Nov 7, 2011) Topics More about exceptions References Head First Java: Chapter 11 (Risky Behavior) The Java Tutorial:

More information

More on Exception Handling

More on Exception Handling Chapter 18 More on Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. CSE 142, Summer 2002 Computer Programming 1. Exceptions CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ 12-Aug-2002 cse142-19-exceptions 2002 University of Washington 1 Reading Readings and References»

More information

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1. Readings and References Exceptions CSE 142, Summer 2002 Computer Programming 1 http://www.cs.washington.edu/education/courses/142/02su/ Reading» Chapter 18, An Introduction to Programming and Object Oriented

More information

More on Exception Handling

More on Exception Handling Chapter 18 More on Exception Handling Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Exceptions in Java

Exceptions in Java Exceptions in Java 3-10-2005 Opening Discussion Do you have any questions about the quiz? What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment?

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Chapter 13 Exception Handling

Chapter 13 Exception Handling Chapter 13 Exception Handling 1 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the runtime error so that the program can continue to run or

More information

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

CSC207H: Software Design. Exceptions. CSC207 Winter 2018 Exceptions CSC207 Winter 2018 1 What are exceptions? Exceptions represent exceptional conditions: unusual, strange, disturbing. These conditions deserve exceptional treatment: not the usual go-tothe-next-step,

More information

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

Casting -Allows a narrowing assignment by asking the Java compiler to trust us Primitives Integral types: int, short, long, char, byte Floating point types: double, float Boolean types: boolean -passed by value (copied when returned or passed as actual parameters) Arithmetic Operators:

More information

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1 A Third Look At Java Chapter Seventeen Modern Programming Languages, 2nd ed. 1 A Little Demo public class Test { public static void main(string[] args) { int i = Integer.parseInt(args[0]); int j = Integer.parseInt(args[1]);

More information

Answer Key. 1. General Understanding (10 points) think before you decide.

Answer Key. 1. General Understanding (10 points) think before you decide. Answer Key 1. General Understanding (10 points) Answer the following questions with yes or no. think before you decide. Read the questions carefully and (a) (2 points) Does the interface java.util.sortedset

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 04: Exception Handling MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Creating Classes 2 Introduction Exception Handling Common Exceptions Exceptions with Methods Assertions and

More information

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages References Exceptions "Handling Errors with Exceptions", Java tutorial http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html CSE 413, Autumn 2005 Programming Languages http://www.cs.washington.edu/education/courses/413/05au/

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 04: Exception Handling MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Creating Classes 2 Introduction Exception Handling Common Exceptions Exceptions with Methods Assertions

More information

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2 CITS2200 Data Structures and Algorithms Topic 2 Java Primer Review of Java basics Primitive vs Reference Types Classes and Objects Class Hierarchies Interfaces Exceptions Reading: Lambert and Osborne,

More information

Implements vs. Extends When Defining a Class

Implements vs. Extends When Defining a Class Implements vs. Extends When Defining a Class implements: Keyword followed by the name of an INTERFACE Interfaces only have method PROTOTYPES You CANNOT create on object of an interface type extends: Keyword

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Review 2: Object-Oriented Programming Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Review 2: Object-Oriented Programming 1 / 14 Topics

More information

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Polymorphism, Abstract Classes, Interfaces Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 3, 2011 G. Lipari (Scuola Superiore Sant

More information

Exceptions. CSC207 Winter 2017

Exceptions. CSC207 Winter 2017 Exceptions CSC207 Winter 2017 What are exceptions? In Java, an exception is an object. Exceptions represent exceptional conditions: unusual, strange, disturbing. These conditions deserve exceptional treatment:

More information

More on Objects in JAVA TM

More on Objects in JAVA TM More on Objects in JAVA TM Inheritance : Definition: A subclass is a class that extends another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a

More information

Object Oriented Software Design

Object Oriented Software Design Object Oriented Software Design Polymorphism, Abstract Classes, Interfaces Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 28, 2010 G. Lipari (Scuola Superiore Sant

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

PESIT Bangalore South Campus

PESIT Bangalore South Campus PESIT Bangalore South Campus 15CS45 : OBJECT ORIENTED CONCEPTS Faculty : Prof. Sajeevan K, Prof. Hanumanth Pujar Course Description: No of Sessions: 56 This course introduces computer programming using

More information

Exceptions. What exceptional things might our programs run in to?

Exceptions. What exceptional things might our programs run in to? Exceptions What exceptional things might our programs run in to? Exceptions do occur Whenever we deal with programs, we deal with computers and users. Whenever we deal with computers, we know things don

More information

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017 PIC 20A Exceptions Ernest Ryu UCLA Mathematics Last edited: November 27, 2017 Introductory example Imagine trying to read from a file. import java.io.*; public class Test { public static void main ( String

More information

5/23/2015. Core Java Syllabus. VikRam ShaRma

5/23/2015. Core Java Syllabus. VikRam ShaRma 5/23/2015 Core Java Syllabus VikRam ShaRma Basic Concepts of Core Java 1 Introduction to Java 1.1 Need of java i.e. History 1.2 What is java? 1.3 Java Buzzwords 1.4 JDK JRE JVM JIT - Java Compiler 1.5

More information

CS 221 Review. Mason Vail

CS 221 Review. Mason Vail CS 221 Review Mason Vail Inheritance (1) Every class - except the Object class - directly inherits from one parent class. Object is the only class with no parent. If a class does not declare a parent using

More information

CS 11 java track: lecture 3

CS 11 java track: lecture 3 CS 11 java track: lecture 3 This week: documentation (javadoc) exception handling more on object-oriented programming (OOP) inheritance and polymorphism abstract classes and interfaces graphical user interfaces

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Data Structures. 02 Exception Handling

Data Structures. 02 Exception Handling David Drohan Data Structures 02 Exception Handling JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN 0132162709 2012 Pearson Education, Inc., Upper Saddle River, NJ.

More information

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling Course Name: Advanced Java Lecture 5 Topics to be covered Exception Handling Exception HandlingHandlingIntroduction An exception is an abnormal condition that arises in a code sequence at run time A Java

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (IS120) Lecture 30 April 4, 2016 Exceptions hapter 27 HW7: PennPals hat Due: Tuesday Announcements Simplified Example class { public void foo() {.bar(); "here in foo");

More information

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling Reminder CSE 1720 Lecture 11 Exception Handling Midterm Exam" Thursday, Feb 16, 10-11:30" CLH J Curtis Lecture Hall, Room J! will cover all material up to and including Tues Feb 14th! Tues, Feb 7 topic:

More information

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are built on top of that. CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Introduction

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Written by John Bell for CS 342, Spring 2018

Written by John Bell for CS 342, Spring 2018 Advanced OO Concepts Written by John Bell for CS 342, Spring 2018 Based on chapter 3 of The Object-Oriented Thought Process by Matt Weisfeld, with additional material from other sources. Constructors Constructors

More information

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING

QUESTION BANK. SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING QUESTION BANK DEPARTMENT:EEE SEMESTER: V SUBJECT CODE / Name: CS2311 OBJECT ORIENTED PROGRAMMING UNIT V PART - A (2 Marks) 1. What is the difference between super class and sub class? (AUC MAY 2013) The

More information

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed 1 1 COMP200 EXCEPTIONS OOP using Java, based on slides by Shayan Javed Exception Handling 2 3 Errors Syntax Errors Logic Errors Runtime Errors 4 Syntax Errors Arise because language rules weren t followed.

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Object Oriented Programming

Object Oriented Programming Object Oriented Programming Java lecture (10.2) Exception Handling 1 Outline Throw Throws Finally 2 Throw we have only been catching exceptions that are thrown by the Java run-time system. However, it

More information

CS Programming Language Java. Fall 2004 Sept. 29

CS Programming Language Java. Fall 2004 Sept. 29 CS3101-3 Programming Language Java Fall 2004 Sept. 29 Road Map today Java review Homework review Exception revisited Containers I/O What is Java A programming language A virtual machine JVM A runtime environment

More information

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the

40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the Answers 1) B 2) C 3) A 4) D 5) Non-static members 6) Static members 7) Default 8) abstract 9) Local variables 10) Data type default value 11) Data type default value 12) No 13) No 14) Yes 15) No 16) No

More information

Java Programming Training for Experienced Programmers (5 Days)

Java Programming Training for Experienced Programmers (5 Days) www.peaklearningllc.com Java Programming Training for Experienced Programmers (5 Days) This Java training course is intended for students with experience in a procedural or objectoriented language. It

More information

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University Chapter 8 Exception Handling CS 180 Sunil Prabhakar Department of Computer Science Purdue University Clarifications Auto cast from char to String does not happen. Cast between int and char happens automatically.

More information

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Lecture 36: Cloning Last time: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting Today: 1. Project #7 assigned 2. equals reconsidered 3. Copying and cloning 4. Composition 11/27/2006

More information

Exception Handling. Chapter 11. Java By Abstraction Chapter 11. Outline What Are Exceptions?

Exception Handling. Chapter 11. Java By Abstraction Chapter 11. Outline What Are Exceptions? Chapter 11 Exception Handling Java By Abstraction 11-1 Outline 11.1 What are Exceptions? 11.1.1 Exception Handling 11.1.2 The Delegation Model 11.2 Java's Exception Constructs 11.2.1 The Basic -catch Construct

More information

JAVA MOCK TEST JAVA MOCK TEST II

JAVA MOCK TEST JAVA MOCK TEST II http://www.tutorialspoint.com JAVA MOCK TEST Copyright tutorialspoint.com This section presents you various set of Mock Tests related to Java Framework. You can download these sample mock tests at your

More information

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p. Preface p. xix Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p. 5 Java Applets and Applications p. 5

More information

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11 Outline Chapter 11 Exception Handling 11.1 What are Exceptions? 11.1.1 Exception Handling 11.1.2 The Delegation Model 11.2 Java's Exception Constructs 11.2.1 The Basic -catch Construct 11.2.2 Handling

More information

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries 1 CONTENTS 1. Introduction to Java 2. Holding Data 3. Controllin g the f l o w 4. Object Oriented Programming Concepts 5. Inheritance & Packaging 6. Handling Error/Exceptions 7. Handling Strings 8. Threads

More information

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

Exceptions Handling Errors using Exceptions

Exceptions Handling Errors using Exceptions Java Programming in Java Exceptions Handling Errors using Exceptions Exceptions Exception = Exceptional Event Exceptions are: objects, derived from java.lang.throwable. Throwable Objects: Errors (Java

More information

G51PGP Programming Paradigms. Lecture OO-4 Aggregation

G51PGP Programming Paradigms. Lecture OO-4 Aggregation G51PGP Programming Paradigms Lecture OO-4 Aggregation 1 The story so far We saw that C code can be converted into Java code Note real object oriented code though Hopefully shows you how much you already

More information

National University. Faculty of Computer Since and Technology Object Oriented Programming

National University. Faculty of Computer Since and Technology Object Oriented Programming National University Faculty of Computer Since and Technology Object Oriented Programming Lec (8) Exceptions in Java Exceptions in Java What is an exception? An exception is an error condition that changes

More information

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1 CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Singleton Pattern Mar. 13, 2007 What is it? If you need to make sure that there can be one and only one instance of a class. For example,

More information

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

Exception-Handling Overview

Exception-Handling Overview م.عبد الغني أبوجبل Exception Handling No matter how good a programmer you are, you cannot control everything. Things can go wrong. Very wrong. When you write a risky method, you need code to handle the

More information

Compaq Interview Questions And Answers

Compaq Interview Questions And Answers Part A: Q1. What are the difference between java and C++? Java adopts byte code whereas C++ does not C++ supports destructor whereas java does not support. Multiple inheritance possible in C++ but not

More information

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance? CS6501 - Internet programming Unit- I Part - A 1 Define Java. Java is a programming language expressly designed for use in the distributed environment of the Internet. It was designed to have the "look

More information

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions. CS511, HANDOUT 12, 7 February 2007 Exceptions READING: Chapter 4 in [PDJ] rationale for exceptions in general. Pages 56-63 and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7 CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7 Reference information about some standard Java library classes appears on the last pages of the test. You can tear off these pages for easier reference

More information

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws Lecture 14 Summary Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws 1 By the end of this lecture, you will be able to differentiate between errors, exceptions,

More information

9: Polymorphism. OOP = abstraction + inheritance + polymorphism

9: Polymorphism. OOP = abstraction + inheritance + polymorphism 9: Polymorphism OOP = abstraction + inheritance + polymorphism 1 Substitutability A Circle Shape A Square A Line 2 Extensibility A Circle Shape A Square A Line A Triangle 3 Interface & Implementation Shape

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Introduction to Java Written by John Bell for CS 342, Spring 2018

Introduction to Java Written by John Bell for CS 342, Spring 2018 Introduction to Java Written by John Bell for CS 342, Spring 2018 Based on chapters 1 to 6 of Learning Java by Patrick Niemeyer and Daniel Leuck, with additional material from other sources. History I

More information

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse Object-Oriented Design Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented Design 1 March 2005 Object Oriented

More information

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7) Software Development & Education Center Java Platform, Standard Edition 7 (JSE 7) Detailed Curriculum Getting Started What Is the Java Technology? Primary Goals of the Java Technology The Java Virtual

More information

Exception handling in Java. J. Pöial

Exception handling in Java. J. Pöial Exception handling in Java J. Pöial Errors and exceptions Error handling without dedicated tools: return codes, global error states etc. Problem: it is not reasonable (or even possible) to handle each

More information