(3) Java Language Constructs

(3) Java Language Constructs - Reading CMSC 132:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 132: Object-Oriented Programming II Java Language Constructs Department of Computer Science University of Maryland, College Park Reading Read Appendix A and Sections 4.1-4.3 in the Koffman text (Appendix A and Sections 2.1-2.3 in the second edition) 2 3 Review of Java language constructs Basic elements Primitive types, variables, constants, operators If-else, switch, while, for Classes Object instances and creating objects with new Object references and the null reference Instance data, class (static) data Methods: parameters, return values, polymorphism 4 Review of Java language constructs Inheritance Base class, derived class, super Method overriding (vs. overloading) Abstract methods Upcasting and downcasting, getClass() , instanceof avoid overuse of these, since they lead to bad designs Interfaces One-dimensional arrays: creating, indexing Exceptions Try-catch blocks
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5 Constructs to be covered now Generics The Iterator interface The enhanced for loop Enumerated types Autoboxing The Comparable interface The Comparator interface 6 Generics – motivating example Problem Before Java 1.5, utility classes only handled data as Objects Objects must be cast back to actual class Example: class A { … } class B { … } ArrayList myList= new ArrayList(); myList.add(new A()); // add an object of // type A A a= (A) myList.get(0); B b= (B) myList.get(0); // problem??? 7 Solution – generic types Generic types Provide abstraction over types Classes, interfaces, and methods can be parameterized Type parameters are defined using the notation < X > Examples: public class SomeClass <X, Y> { } SomeClass <String, Integer> myObject=
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

(3) Java Language Constructs - Reading CMSC 132:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online