Inheritance Lakshmish Ramaswamy Exception Hierarchy Exceptions in Java

  • Slides: 14
Download presentation
Inheritance Lakshmish Ramaswamy

Inheritance Lakshmish Ramaswamy

Exception Hierarchy • Exceptions in Java are organized as a hierarchy • Root is

Exception Hierarchy • Exceptions in Java are organized as a hierarchy • Root is Throwable – Set of Print. Stack. Trace methods, pair of constructor and to. String method • First level children Error and Exception • Any exception that is not Run. Time. Exception is checked exception • Generally, exception extends another exception but provides constructors

Nested Classes • A class declaration within another class declaration (the outer class) –

Nested Classes • A class declaration within another class declaration (the outer class) – The keyword “static” is used before inner classname • The inner class is considered as a member of outer class • Can be public, private, package or protected • Typically private • Inner class methods can access private static members of the outer class and private instance members when given a reference

Recursion Lakshmish Ramaswamy

Recursion Lakshmish Ramaswamy

Basics of Recursion • A recursive method makes a call to itself directly or

Basics of Recursion • A recursive method makes a call to itself directly or indirectly • Is this not endless circular logic? – Call is for a simpler instance • Several real-world applications are recursive – Dictionary lookup for definitions – Scanning files on a computer – Computer languages

A Simple Example • Calculate sum of first N integers (S(N)) • We know

A Simple Example • Calculate sum of first N integers (S(N)) • We know S(1) = 1 • S(N) can be expressed as S(N) = S(N-1) + N – S(2) = 2 + S(1) – S(3) = S(2) + 3 – S(N) = N(N+1)/2 <---- Closed form • Closed form are often complex • Recursive expressions might be simpler

Recursive Method for S(N)

Recursive Method for S(N)

Points to Note • Function calls a clone of itself (note the difference in

Points to Note • Function calls a clone of itself (note the difference in parameters) • Only one clone is active at any instance – Computer does all the book keeping for you • Base case – Instance that can be solved without recursion – S(1) is the base case • Recursive call should make progress towards base case

Two Rules • Always have at least one base case solved without recursion •

Two Rules • Always have at least one base case solved without recursion • Any recursive call should make progress towards base case

Second Example • Printing numbers in any base • Simpler case – Printing number

Second Example • Printing numbers in any base • Simpler case – Printing number in decimal form – No method that can print an integer – Method that can print a single character – Digits are treated as characters • print(2457) can be expressed as print(245) followed by printing 7 (via character printing method)

Decimal Printing Method • Identify the base condition? • How is the progress done?

Decimal Printing Method • Identify the base condition? • How is the progress done?

Printing Number in any Base • Exception if base > 16 • Infinite loop

Printing Number in any Base • Exception if base > 16 • Infinite loop if base is 1 – Notice identical call to the method (No progress)

Robust Implementation

Robust Implementation