Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

Similar documents
INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

CONTAİNERS COLLECTİONS

USAL1J: Java Collections. S. Rosmorduc

JAVA MOCK TEST JAVA MOCK TEST II

CS-202 Introduction to Object Oriented Programming

Type Hierarchy. Lecture 6: OOP, autumn 2003

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Sets and Maps. Part of the Collections Framework

CS Programming Language Java. Fall 2004 Sept. 29

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Chapter 5 Object-Oriented Programming

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Today. Book-keeping. Exceptions. Subscribe to sipb-iap-java-students. Collections. Play with problem set 1

25. Generic Programming

Java Object Oriented Design. CSC207 Fall 2014

More Relationships Between Classes

Inheritance -- Introduction

Chapter 2: Java OO II X I A N G Z H A N G

What is Inheritance?

CS 251 Intermediate Programming Inheritance

Example: Count of Points

Java Fundamentals (II)

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Practice for Chapter 11

VIRTUAL FUNCTIONS Chapter 10

5/23/2015. Core Java Syllabus. VikRam ShaRma

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

15CS45 : OBJECT ORIENTED CONCEPTS

JAVA MOCK TEST JAVA MOCK TEST III

Compaq Interview Questions And Answers

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Homework #10 due Monday, April 16, 10:00 PM

Example: Count of Points

Core Java Contents. Duration: 25 Hours (1 Month)

Exercise: Singleton 1

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CGS 2405 Advanced Programming with C++ Course Justification

Java SE 8 Programming

CSC 1214: Object-Oriented Programming

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Inheritance and object compatibility

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Designing Classes. Design for Reuse School of Computer Science

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Collections (Java) Collections Framework

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Java SE 8 Programming

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Programmieren II. Polymorphism. Alexander Fraser. June 4, (Based on material from T. Bögel)

Inheritance and Polymorphism

Motivations. Chapter 13 Abstract Classes and Interfaces

PIC 20A Collections and Data Structures

PROGRAMMING LANGUAGE 2

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Java SE 8 Programming

1 Shyam sir JAVA Notes

Collections, Maps and Generics

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Learn Java/J2EE Basic to Advance level by Swadeep Mohanty

The Collections API. Lecture Objectives. The Collections API. Mark Allen Weiss

Core Java Syllabus. Overview

Java Collections Framework reloaded

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Java Map and Set collections

+ Abstract Data Types

Advanced Programming Languages Effective Java Item 1. Spring 2015 Chungnam National Univ Eun-Sun Cho

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

OBJECT ORİENTATİON ENCAPSULATİON

Java Magistère BFA

תוכנה 1 מבני נתונים גנריים

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

JAVA. Duration: 2 Months

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Java HashMap Interview Questions

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Java Collections Framework. 24 April 2013 OSU CSE 1

C++ Important Questions with Answers

First IS-A Relationship: Inheritance

27/04/2012. Objectives. Collection. Collections Framework. "Collection" Interface. Collection algorithm. Legacy collection

Programming Languages and Techniques (CIS120)

Software 1 with Java. Java Collections Framework. Collection Interfaces. Online Resources. The Collection Interface

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

COURSE 2 DESIGN PATTERNS

COMP 250 Fall inheritance Nov. 17, 2017

Programming Exercise 14: Inheritance and Polymorphism

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Implements vs. Extends When Defining a Class

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

Transcription:

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6 Prepared for course 160410/411 Michael C. Kunkel m.kunkel@fz-juelich.de Materials taken from; docs.oracle.com Teach Yourself Java ISBN 9780078825705

Polymorphism Next Class Polymorphism The ability of an object to have many forms. provides developers the ability to create and reuse code Object can be run differently when they seem the same

Polymorphism Next Class Polymorphism is accomplished via (Static binding) Compile time binding which is achieved by method overloading in a class (Dynamic binding) Run time binding which is achieved by overriding methods of a super class

Polymorphism Next Class Static binding achieved by method overloading in a class known as compile time binding because Java knows which method will be invoked in the program by checking the methods parameters remember method overloading?

Method Overloading

Polymorphism Next Class Dynamic binding achieved by overriding methods of a super class method overriding is having the same name and parameters but returning different results known as run time binding because Java knows which method will be invoked during run-time, i.e. as the program runs

Polymorphism Next Class

Polymorphism Next Class Same method from different objects, different results Java distinguished these methods at run time

Polymorphism Next Class One more example of run-time binding Java distinguishes these methods at run time Tell me why

Polymorphism Next Class Upcasting when a subtype (subclass) object is cast to a super type (superclass) object. Its upward on the inheritance tree Here we cast the Car to the Vehicle Since Vehicle is the supertype of Car, this is upcasting

Polymorphism Next Class Upcasting cont. The actual object does not change because of casting. The Car object is still a Car object, only the reference type get changed The above code will produce??

Polymorphism Next Class Upcasting cont. The actual object does not change because of casting. The Car object is still a Car object, only the reference type get changed The above code will produce??

Polymorphism Next Class Upcasting cont. Upcasting is always safe since we treat a type to a more general one. In this example, a Vehicle has all the behaviors of a Car

Polymorphism Next Class Downcasting is casting to a subtype (subclass). It is downward the inheritance tree Here we cast the Vehicle to the Car Since Car is the subtype of Vehicle, this is downcasting

Polymorphism Next Class Downcasting cont. unlike upcasting, downcasting can fail if the object type is not the target object type This will throw the exception ClassCastException because the actual object type is Car, and a Car is not a MotorCycle!! So we cannot cast to MotorCycle..make sense?

Polymorphism Next Class Downcasting cont. in Java there is a keyword called instanceof to check the type of an object before casting

Polymorphism Next Class Downcasting cont. more frequently used than upcasting use when access to to specific subtype behavior is needed

Polymorphism Next Class Downcasting cont.

Inheritance Next Class Inheritance The process where classes can be derived from other existing classes when a new class is derived from an existing class, then the new class can have all states and behaviors of the existing (parent) class Recap, tell me what the state and behavior mean

Inheritance Next Class Inheritance In Java, each class can have only one parent class to inherit from However the child class may also be a parent class to another set of children classes

Inheritance Next Class Classes that inherit from parent classes can extend the properties of the parent class ChildClass can have all the states/behaviors of ParentClass ChildClass can also have its own states/behaviors in addition to those inherited from ParentClass

Inheritance Next Class Why do we need Inheritance Reusability of codes increases use public methods of parent classes without having to write them again Overriding We can override the methods of parent classes in child classes

Inheritance Next Class Why do we need Inheritance Encapsulation of states/behaviors Child classes can hide data by making them private Reduced maintenance Making a change in the parent class trickles to all children

Next Interfaces Class What Is an Interface? Commonly an interface is a group of related methods with empty bodies. Implementing an interface allows a class to become more formal about the behavior it promises to provide.

Next Interfaces Class What Is an Interface? Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile

Next Interfaces Class

Next Interfaces Class

Next Interfaces Class

Class Next Work Class Remember that binding energy homework? This time use Polymorphism to plot the binding energy for elements A<10

Homework Next Class Remember that binding energy homework? This time use Polymorphism to plot the binding energy for elements A<25

Homework Next Class Here are some data points Plot these points Calculate Hubble s constant Object Distance (MPc) Velocity(km/s) Virgo 15 1200 Ursa Major 190 15400 Corona Borealis 280 22000 Bootes 490 39400 Hydra 760 60600 Quasar PC 1247+3406 4035 280000

Homework Next Class What is the current age of our universe using your found value?

Collections Next Class I have not prepared a lecture part for collections. I thought it best to demonstrate them Map TreeSet List Pairs

Collections Next Class Map An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface. The Map interface includes methods for basic operations (such as put, get, remove, containskey, containsvalue, size, and empty), bulk operations (such as putall and clear), and collection views (such as keyset, entryset, and values).

Collections Next Class Map The Java platform contains three generalpurpose Map implementations: HashMap, TreeMap, and LinkedHashMap. Their behavior and performance are precisely analogous to HashSet, TreeSet, and LinkedHashSet, as described in The Set Interface section.

Collections Next Class Caveats to Maps HashMap:This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. TreeMap: This class sorts by ascending order LinkedHashMap: This class preserves insertion order

Collections Next Class If you're only interested in the keys, you can iterate through the keyset() of the map: Map<String, Object> map = ; for (String key : map.keyset()) { } If you only need the values, use values(): for (Object value : map.values()) {// }

Collections Next Class Finally, if you want both the key and value, use entryset(): for (Map.Entry<String, Object> entry : map.entryset()) { String key = entry.getkey(); Object value = entry.getvalue(); }

Collections Next Class Not Ordered Ordered

Collections Next Class Key order is preserved, but value changed

Collections Next Class TreeSet A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used. This implementation provides guaranteed log(n) time complexity for the basic operations (add, remove and contains).

Collections Next Class Not Ordered Ordered

Collections Next Class List An ordered collection (also known as a sequence). precise control over where in the list each element is inserted. elements accessible by their integer index (position in the list) Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements

Collections Next Class List The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashcode methods. The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example).

Collections Next Class

Collections Next Class Pairs There are many Pairs. The one referred here is located at org.apache.commons.lang3.tuple.pair A pair consisting of two elements. This class is an abstract implementation defining the basic API. It refers to the elements as 'left' and 'right'. It also implements the Map.Entry interface where the key is 'left' and the value is right'.

Collections Next Class Pairs Subclass implementations may be mutable or immutable. However, there is no restriction on the type of the stored objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

Collections Next Class

Collections Next Class