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