Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology/ George Koutsogiannakis 1.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
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.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
OOP Languages: Java vs C++
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
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.
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Java and C++, The Difference An introduction Unit - 00.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
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.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
The Java Programming Language
1 Documenting with Javadoc. 2 Motivation  Why document programs? To make it easy to understand, e.g., for reuse and maintenance  What to document? Interface:
Utilities (Part 2) Implementing static features 1.
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Chapter 5 Introduction to Defining Classes
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
1  lecture slides online
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
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.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Accessing Files in Java
ATS Application Programming: Java Programming
Object Oriented Programming
Java Programming Language
Applying OO Concepts Using Java
Chapter 8 Class Inheritance and Interfaces
Java Basics Exception Handling.
Presentation transcript:

Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern features GUI exceptions multiple threads networking suitable for multiple courses

Scott Grissom, copyright 2004Ch 3: Java Features Slide 2 State (3.2.1) Instance Variables avoid public generally use private Class Variables values associated with the entire class static int count Symbolic Constants final int SIZE = 100; make them static?

Scott Grissom, copyright 2004Ch 3: Java Features Slide 3 Behavior (3.2.2) method signatures overloaded methods have unique signature but identical name Instance Methods invoked by an object generally public Class Methods invoked using class name public static int getCount( ) answer = Math.sqrt(16.0);

Scott Grissom, copyright 2004Ch 3: Java Features Slide 4 Main methods the first method called by the Java Runtime System specific signature public static void main(String args[]) Command Line Arguments System.out.println(args[I]) Sample execution java program arg1 arg2 Sample result arg1 arg2

Scott Grissom, copyright 2004Ch 3: Java Features Slide 5 Identity (3.2.3) Objects are instantiated and associated with a reference Memory is allocated Constructor is invoked Reference is returned Scenarios multiple references no references garbage collection Parameters passed by value copies are made

Scott Grissom, copyright 2004Ch 3: Java Features Slide 6 Inheritance (3.3) most state and behavior is available to the subclass not inherited private members constructors overloading vs. overriding keyword final prevents a class from being extended prevents a method from being overridden prevents a variable from being changed

Scott Grissom, copyright 2004Ch 3: Java Features Slide 7 abstract (3.3.2) abstract methods must be overridden and contain no implementation abstract classes contain one or more methods and cannot be instantiated interfaces allows specification form of inheritance all methods are abstract no instance variables constants are OK a class implements an interface can implement multiple interfaces similar to an abstract class?

Scott Grissom, copyright 2004Ch 3: Java Features Slide 8 Polymorphism (3.3.4) Polymorphic references are resolved at run-time, not during compilation called “late binding” References can point to classes and subclasses Mammal m = new Dog( ); Example with mammals Dog, Pig, Cat, Tiger speak method story: working for toy company Sample Code Mammals ark[SIZE]; for (index=0; index<ark.length; index++) ark[index].speak( );

Scott Grissom, copyright 2004Ch 3: Java Features Slide 9 Class Object Every class is a subclass of Object Several methods are provided It is useful to override them String toString( ) boolean equals(Object) Object clone( )

Scott Grissom, copyright 2004Ch 3: Java Features Slide 10 Compiling 3.4 Source code is converted to machine code for some languages such as C Instead, Java is interpreted Source code (java files) is compiled into bytecode (class files) JVM runs the program on multiple platforms

Scott Grissom, copyright 2004Ch 3: Java Features Slide 11 Exceptions ( ) handles rare and unexpected events we must detect and recover some must be handled (checked) others are optional (unchecked) Each exception has a name Arithmetic Exception Index Out of Bounds Null Pointer File Not Found Illegal Argument

Scott Grissom, copyright 2004Ch 3: Java Features Slide 12 Throwing Exceptions A simple way to handle error messages Sample Code if (value < 0) throw new IllegalArgumentException (“value must be positive”); method terminates It is often more appropriate to ‘catch’ the exception and handle it appropriately

Scott Grissom, copyright 2004Ch 3: Java Features Slide 13 Catching Exceptions Sample Code try{ // possibility of exception x = x / y; } catch (ArithmeticException e){ System.out.println (“error: “ + e.toString()); e.printStackTrace(); } multiple catch clauses can be provided but only one will be executed Flow of control continues after last catch clause

Scott Grissom, copyright 2004Ch 3: Java Features Slide 14 What can go wrong? European Space Agency Ariane rocket in 1996 embedded software written in Ada exception not caught rocket self destructed

Scott Grissom, copyright 2004Ch 3: Java Features Slide 15 Object Serialization (not in book) Java provides support to save and load objects from external files Information is stored in binary For each class you must: import java.io.*; implement Serializable No additional methods must be written!

Scott Grissom, copyright 2004Ch 3: Java Features Slide 16 Saving to a file This sample code is often embedded in a try/catch block Sample Code SomeObject obj; FileOutputStream outFile = new FileOutputStream (“filename”); ObjectOutputStream objectOut = new ObjectOutputStream (outFile); objectOut.writeObject (obj); outFile.flush(); outFile.close();

Scott Grissom, copyright 2004Ch 3: Java Features Slide 17 Loading from a file This sample code is often embedded in a try/catch block Sample Code SomeObject obj; FileInputStream inFile = new FileInStream (“filename”); ObjectInputStream objectIn = new ObjectInputStream (inFile); obj = (SomeObject) objectIn.readObject (); inFile.close();

Scott Grissom, copyright 2004Ch 3: Java Features Slide 18 Javadoc Documentation generates API documentation in HTML parses ‘special’ comments within source code /** */ javadoc *.java -author pulldown menu in BlueJ does this automatically

Scott Grissom, copyright 2004Ch 3: Java Features Slide 19 Doc comments Should precede class declarations variable declarations method headers The first sentence should be a concise summary optionally followed by additional information Followed by zero or more special tags

Scott Grissom, copyright 2004Ch 3: Java Features Slide 20 Special parameter-name description

Scott Grissom, copyright 2004Ch 3: Java Features Slide 21 Sample Refer to our online Style Guide /** Calculate the sqare Scott value to find square square root of value */ double squareRoot (double value){