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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Class Hierarchy (Inheritance)
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
For use of Cleveland State's IST410 Students only 1 Exception.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance Reserved word protected Reserved word super
(c) University of Washington11-1 CSC 143 Java More on Exceptions Reading: Ch. 15.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
March 2004Object Oriented Design1 Object-Oriented Design.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Abstract Classes and Interfaces
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Polymorphism & Interfaces
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
OVERRIDING/OVERLOADING Srinivas. EXAM OBJECTIVES  Given a code example, determine if a method is correctly overriding or overloading another method,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Peyman Dodangeh Sharif University of Technology Fall 2014.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Interfaces and Polymorphism CS 162 (Summer 2009).
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
1 CSC111H Exceptions 2 Dennis Burford
Today’s lecture Review of chapter 8 Go over examples.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
© 2001 by Ashby M. Woolf Revision 2 Exceptions for Exceptional Circumstances Passing a Problem up the Chain of Command.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Modern Programming Tools And Techniques-I
Java Exceptions a quick review….
Lecture 12 Inheritance.
Interfaces.
Inheritance and Polymorphism
CS102 – Exceptions David Davenport Latest: May 2015
COMPSCI 230 S Programming Techniques
Review for the Final Exam
ATS Application Programming: Java Programming
Interface.
Advanced Programming Behnam Hatami Fall 2017.
Fundamental Error Handling
Inheritance, Superclasses, Subclasses
Inheritance Inheritance is a fundamental Object Oriented concept
COMPUTER 2430 Object Oriented Programming and Data Structures I
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Topics OOP Review Inheritance Review Abstract Classes
Presentation transcript:

Tutorial 2 Shane Paul

Contact Details Shane Paul Rm: 378 Ext:

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

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

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

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

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

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

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

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

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

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

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

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

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

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!");

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!");

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

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