Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 2 Shane Paul. Contact Details Shane Paul Rm: 378 Ext: 82766

Similar presentations


Presentation on theme: "Tutorial 2 Shane Paul. Contact Details Shane Paul Rm: 378 Ext: 82766"— Presentation transcript:

1 Tutorial 2 Shane Paul

2 Contact Details Shane Paul Rm: 378 Ext: 82766 Email: shane@cs.auckland.ac.nz

3 Extending Classes Extending classes is how java promotes reuse of code When one class extends another, that class is called its parent and it the child You may only have one parent, but as many children as you like We can draw these parent-child relationships as a tree diagram, just as for family trees or animal taxonomy

4 Abstract Classes Abstract Classes cannot be used directly They contain one or more methods that have not been completed Their sole purpose is to be extended from

5 class Dog Extending Methods Variables String furColour String type int numOfFleas void bark() Ball getTheBall() void chewStuff() class BullDog extends Dog Methods Variables int uglyFactor void drool() The class Bulldog now has everything its parent has, plus what it has The methods from the parent are not abstract (i.e. empty) This means the BullDog only has to write code for the new method drool() – everything else is already written

6 Note about Abstract Classes Extending abstract classes is much the same The only difference is that an abstract class has one method that is empty So, in addition to adding new methods, you would have to “fill in” the abstract class also by overriding that method

7 Interfaces Interfaces are Java’s way of not having multiple inheritance You can only extend one class, but implement many Interfaces Implementing an interface is very different from extending In essence, it is simply a list of methods your class must have

8 class Dog Interfaces Methods void bark() Ball getTheBall() void chewStuff() The above classes are not related in any meaningful way Let’s say we wanted to introduce the method attack() to all of them class Cat Methods void meow() void markStuff() class Dolphin Methods void play() void eat()

9 Interfaces There are four approaches we could use: 1.Add the method to each class individually 2.Use a common parent class 3.Use an Abstract parent class 4.Use an interface

10 class Dog extends AttackAnimal Add the method Methods void bark() Ball getTheBall() void chewStuff() class Cat extends AttackAnimal Methods void meow() void markStuff() class Dolphin extends AttackAnimal Methods void play() void eat() void attack()

11 class Dog extends AttackAnimal Using a Common Parent Methods void bark() Ball getTheBall() void chewStuff() class Cat extends AttackAnimal Methods void meow() void markStuff() class Dolphin extends AttackAnimal Methods void play() void eat() class AttackAnimal Methods void attack()

12 class Dog extends AttackAnimal Using an Abstract Parent Methods void bark() Ball getTheBall() void chewStuff() class Cat extends AttackAnimal Methods void meow() void markStuff() class Dolphin extends AttackAnimal Methods void play() void eat() class AttackAnimal Methods abstract void attack() void attack()

13 class Dog implements AttackAnimal Using an Interface Methods void bark() Ball getTheBall() void chewStuff() class Cat implements AttackAnimal Methods void meow() void markStuff() class Dolphin implements AttackAnimal Methods void play() void eat() interface AttackAnimal Methods void attack(); void attack()

14 Exceptions Exceptions are also organised in a hierarchy also They are grouped according to the type of error they embody

15 Object Throwable Exception RuntimeException ArithmeticExceptio n IllegalArgumentExc eption NumberFormatExcept ion NullPointerExcepti on IndexOutOfBoundsEx ception ArrayIndexOutOfBou nds- Exception IOException FileNotFoundExcept ion Error OutOfMemoryErrorStackOverflowError

16 Exercise Answer? try { String a = "0"; String b = "10"; String c = "abcde"; int r1 = Integer.parseInt(c); int r2 = Integer.parseInt(b) / Integer.parseInt(a); } catch (ArithmeticException e) { System.out.println("Arithmetic Error!"); } catch (NumberFormatException e) { System.out.println("Incorrect Format"); } System.out.println("Finished!");

17 Exercise Answer? try { System.out.println("main+"); int i = 0; if (i <= 0) throw new Exception("Throw an error"); System.out.println("main-"); } catch (Exception e) { System.out.println(e); } finally { System.out.println("Finally block!"); } System.out.println("Finished!");

18 Exercise Answer? try { String a = " abcde"; int r2=Integer.parseInt(a) / Integer.parseInt(a); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error"); } catch (ArithmeticException e) { System.out.println("Calculation Error!"); } finally { System.out.println("Finally block"); } System.out.println("Finished");

19 Exercise Answer? try { String a = "0"; int r2 = Integer.parseInt(a) / Integer.parseInt(a); } catch (ArithmeticException e) { System.out.println("Calculation Error"); return; } catch (Exception e) { System.out.println("General Exception"); } finally { System.out.println("Finally"); } System.out.println("Finished");


Download ppt "Tutorial 2 Shane Paul. Contact Details Shane Paul Rm: 378 Ext: 82766"

Similar presentations


Ads by Google