Dr. Philip Cannata 1. Dr. Philip Cannata 2 Dr. Philip Cannata 3.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
1 Algorithms and Problem Solving. 2 Outline  Problem Solving  Problem Solving Strategy  Algorithms  Sequential Statements  Examples.
Checked/Uncheck Exception And finally Block. Catching Any Exception It is possible to create a handler that catches any kind of exception. (This is possible.
Introduction to Methods
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Shorthand operators.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 5 Loops.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Problem of the Day  Why are manhole covers round?
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
Working with arrays (we will use an array of double as example)
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
Mixing integer and floating point numbers in an arithmetic operation.
Dr. Philip Cannata 1 mgr Semantic Data Management.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces and Polymorphism CS 162 (Summer 2009).
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
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:
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Lecture 10: More on Methods and Scope
Building Java Programs
Comp Sci 200 Programming I Jim Williams, PhD.
Comp 249 Programming Methodology
Something about Java Introduction to Problem Solving and Programming 1.
Handling Exceptions.
Writing Methods.
March 29th Odds & Ends CS 239.
More inheritance, Abstract Classes and Interfaces
TO COMPLETE THE FOLLOWING:
Java Language Basics.
References, Objects, and Parameter Passing
Algorithm Correctness
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
ICT Programming Lesson 5:
Sampath Kumar S Assistant Professor, SECE
Java’s Central Casting
Presentation transcript:

Dr. Philip Cannata 1

Dr. Philip Cannata 2

Dr. Philip Cannata 3

Dr. Philip Cannata 4 Java Upcasting and Downcasting Click here for example html or see the “Java Upcasting and Downcasting” link on the class webpagehere Click here for example code or see the Test.java link on the class webpage or see the next page.here

Dr. Philip Cannata 5 Java Upcasting and Downcasting – Test.java class Animal { static int value = 100; } class Cat extends Animal { int value = Animal.value + 1; } class Dog extends Animal { int value = Animal.value + 2; } public class Test { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.value); Dog d = new Dog(); System.out.println(d.value); Animal a = c; System.out.println(a.value); Object o = c; // System.out.println(o.value); a = d; type(c); type(d); type(a); // type(a.value); Dog d2 = (Dog) a; a = c; Dog d3 = (Dog) a; } public static void type(Animal a){ System.out.println("I am a " + a); }

Dr. Philip Cannata 6 Java Upcasting and Downcasting – Test.java class Animal { static int value = 100; } class Cat extends Animal { int value = Animal.value + 1; } class Dog extends Animal { int value = Animal.value + 2; } public class Test { public static void main(String[] args) { Cat c = new Cat(); System.out.println(c.value); Dog d = new Dog(); System.out.println(d.value); Animal a = c; System.out.println(a.value); Object o = c; // System.out.println(o.value); a = d; type(c); type(d); type(a); // type(a.value); Dog d2 = (Dog) a; a = c; Dog d3 = (Dog) a; } public static void type(Animal a){ System.out.println("I am a " + a); } Textbook Chapter 24 In languages like Java, programmers think they have the benefit of a type system, but in fact many common programming patterns force programmers to employ casts instead. Casts intentionally subvert the type system and leave checking for execution time. This indicates that Java’s evolution is far from complete. In contrast, most of the type problems of Java are not manifest in a language like ML, but its type systems still holds a few (subtler) lurking problems. In short, there is still much to do before we can consider type system design a solved problem.... Types form a very valuable first line of defense against program errors. Of course, a poorly-designed type system can be quite frustrating: Java programming sometimes has this flavor. A powerful type system such as that of ML, however, is a pleasure to use. ML programmers, for instance, claim that programs that type correctly often work correctly within very few development iterations. Types that have not been subverted (by, for instance, casts in Java) perform several valuable roles: When type systems detect legitimate program errors, they help reduce the time spent debugging. Type systems catch errors in code that is not executed by the programmer. This matters because if a programmer constructs a weak test suite, many parts of the system may receive no testing. The system may thus fail after deployment rather than during the testing stage. (Dually, however, passing a type checker makes many programmers construct poorer test suites—a most undesirable and unfortunate consequence!)