The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
COMP 121 Week 5: Exceptions and Exception Handling.
Class Hierarchy (Inheritance)
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Object Oriented Programming
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Java Programming: Guided Learning with Early Objects
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Exception Handling Part 2: Creating and Throwing Exceptions CSIS 3701: Advanced Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Inheritance and Polymorphism CSIS 3701: Advanced Object Oriented Programming.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Object-Oriented Programming: Polymorphism Chapter 10.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming: Guided Learning with Early Objects
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Inheritance and Encapsulation
Java Programming: From Problem Analysis to Program Design,
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Chapter 12 Exception Handling and Text IO Part 1
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

The Java Inheritance Hierarchy CSIS 3701: Advanced Object Oriented Programming

Object Class All Java classes part of single inheritance hierarchy “Root” superclass: Object class Object String JComponent …

Object Class Developer-created classes part of hierarchy –If no superclass defined, Object used as superclass public class Clock extends Object { Object NameListClock Employee Implicitly added SecondClock Salaried Manager Hourly InternConsultant

Object Class Defines variables and methods common to all Java classes –May be overridden in subclasses Object this member variable toString method clone method equals method … By default, compares based on memory location (like ==) String equals method … Overrides inherited definition to do character- based comparison

Visual Applications All visual applications must extend JFrame –Inherits ability to be drawn on screen as independent window JFrame contentPane member variable getContentPane() method setSize(width, height) method setVisible(boolean) method … Main … Methods inherited and called by your application

JComponent Hierarchy Defines x, y position, width and height Defines state of “clickable” components Defines components with editable text Note that must look at documentation for all classes above in hierarchy for description of available methods

JComponent Hierarchy Can add new classes to the hierarchy –Must invoke superclass constructor, passing required information –Must use inherited methods to change what/how information displayed Example: JRotateLabel class –Extends JLabel to display one of a list of labels

JComponent Hierarchy public class JRotateLabel extends JLabel { private String[] labels; private int which; public JRotateLabel(String[] labs) { super(l[0]); labels = l; which = 0; } public void rotate() { which++; if (which >= labels.length) {which = 0;} setText(labels[which]); } Pass first label in list to JLabel constructor as initial label to display Call JLabel setText method to change which label is displayed

Type Wrapper Classes Simple types not part of Java inheritance hierarchy “Type wrappers”: –Objects that store instances of simple types as member variable 137 Instance of int simple type value: Object that stores an int as member variable

Type Wrapper Classes Java has “wrapper” class for each simple type: Can use to store/retrieve simple values: Integer i = new Integer(137); int x = i.intValue(); intInteger doubleDouble charChar booleanBoolean …… Store 137 in `new Integer object i Retrieve value stored in i as an integer

Exception Class Hierarchy Java defines two types of exception –Exception –RuntimeException (which extends Exception ) Most commonly used exceptions extend RuntimeException

Exception Class Hierarchy Subclasses of Exception must be handled –Any statement that might cause one of these exceptions must be inside try/catch block –Code will not compile otherwise Example: File commands might cause IOException try { File f = new File(“stuff.txt”); } catch (IOException ex) { … } Opening a file might cause IOExeception if file does not exist, etc. Must be caught

Exception Class Hierarchy Theory vs. Practice –Theory: Any statement that might cause exception should be in try/catch block –Practice: Would need to put almost every statement in try/catch block Any use of object could cause NullPointerException, etc.

Exception Class Hierarchy Compromise: –Only require handling for exceptions that might affect memory external to program Files/Networking: IOException Databases: SQLException Client/Server: ServletException –Problems that cannot be restricted to JVM Design note: –Your exception classes should extend RuntimeException

Exceptions and Polymorphism Can use superclasses to write code that handles any exception try { int x = Integer.parseInt(“Fred”); } catch(Exception ex) { … } Problem: How should it be handled? –No longer have information on the type of problem Also matches any subclass of Exception (such as NumberFormatException ) due to polymorphism

Designing Exception Hierarchies Can create hierarchy of exceptions –Can be as informative as possible about problem –User can still catch superclass if they don’t care catch (ClockException ex) {…} ClockException ClockHourException ClockMinuteException ClockHourHighException ClockHourLowException ClockMinuteHighException ClockMinuteLowException