DOC PREVIEW
UMD CMSC 132 - Object-Oriented Programming & Java Language Constructs

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 132 Object Oriented Programming II Object Oriented Programming Java Language Constructs Department of Computer Science University of Maryland College Park 1 Overview Object oriented programming OOP Introduction to OOP principles Java programming language Review language constructs Introduce new language constructs Many from Java 5 0 2 Object Oriented Programming OOP Approach to improving software View software as a collection of objects entities Motivated by software engineering concerns To be discussed later in the semester OOP takes advantage of two techniques Abstraction Encapsulation 3 Techniques Abstraction Abstraction Provide high level model of activity or data Procedural abstraction Specify what actions should be performed Hide algorithms Data abstraction Specify data objects for problem Hide representation 4 Techniques Encapsulation Encapsulation Confine information so it is only visible accessible through an associated external interface Approach For some entity X in program Abstract data in X Abstract actions on data in X Collect data actions on X in same location Protects and hides X Extension of abstraction 5 Abstraction Encapsulation Example Abstraction of a Roster Data List of student names Actions Create roster Add student Remove student Print roster Encapsulation ROSTER List of names create addStudent removeStudent print Only these actions can access names in roster 6 Java Programming Language Language constructs designed to support OOP Example Interface specifies a contract Class implements defines contracts supports encapsulation of implementation Class libraries designed using OOP principles Example Java Collections Framework Java Swing 7 Java Interface An Interface defines a contract Collection of Constants Abstract methods no implementations Can not be instantiated Classes can implement interfaces Must implement all methods in interface Example class Foo implements Bar Similar to abstract class But class can inherit from multiple interfaces 8 Java Collections Framework Collection Object that groups multiple elements into one unit Also called container Collection framework consists of Interfaces Abstract data type Implementations Reusable data structures Algorithms Reusable functionality 9 Overview Object oriented programming OOP Introduction to OOP principles Java programming language Review language constructs Introduce new language constructs Many from Java 5 0 10 Review of Java Language Constructs Basic elements Primitive types variables constants operators If else switch while for Classes Object instances Creating objects with new Object references The null reference Instance data class static data Methods Parameters return values polymorphism 11 Review of Java Language Constructs Inheritance Base class derived class super Method overriding vs overloading Abstract methods Up and down casting getClass instanceof avoid overuse of these leads to bad designs Interfaces 1D Arrays Creating indexing Exceptions Try catch blocks 12 New Java Language Constructs Autoboxing Enumerated types Generics Enhanced for loop Iterator interface Stream input output Scanner class Annotations BitSet class 13 Enumerated Types New type of variable with set of fixed values Establishes all possible values by listing them Supports values valueOf name compareTo Can add fields and methods to enums Example public enum Color Black White new enumeration Color myC Color Black for Color c Color values System out println c When to use enums Natural enumerated types days of week phases of the moon seasons Sets where you know all possible values 14 Enumerated Types From Taming the Tiger presentation by Joshua Bloch and Neal Gafter at Sun s 2004 Worldwide Java Developer Conference public class Card implements Serializable public enum Rank DEUCE THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN JACK QUEEN KING ACE public enum Suit CLUBS DIAMONDS HEARTS SPADES private final Rank rank private final Suit suit private Card Rank rank Suit suit this rank rank this suit suit public Rank rank return rank public Suit suit return suit public String toString return rank of suit 15 Generics Motivating Example Problem Utility classes handle arguments as Objects Objects must be cast back to actual class Casting can only be checked at runtime Example class A class B List myL new List myL add new A Add an object of type A B b B myL get 0 throws runtime exception java lang ClassCastException 16 Solution Generic Types Generic types Provides abstraction over types Can parameterize classes interfaces methods Parameters defined using X notation Examples public class foo X Y Z List String myNames Improves Readability robustness Used in Java Collections Framework 17 Generics Usage Using generic types Specify type parameter for utility class Automatically performs casts Can check class at compile time Example class A class B List A myL new List A myL add new A Add an object of type A A a myL get 0 myL element class A B b B myL get 0 causes compile time error 18 Generics Issues Generics and subtyping Even if class A extends class B List A does not extend List B Example class B class A extends B A is subtype of B B b new A A used in place of B List A aL new LinkedList A List B bL aL compile time error Why 19 Subtyping and generic types Consider what could happen if legal class B class A extends B A is subtype of B B b new A A can be used where B expected List A aL new LinkedList A List B bL aL bl add b A a al getFirst runtime exception 20 Subtyping and Arrays Subtyping works for arrays class B class A extends B A is subtype of B B b new A A can be used where B expected A aA new A 1 B bA aA aA 0 b won t compile bA 0 b get runtime exception Arguably a mistake 21 Autoboxing Unboxing Automatically convert primitive data types Data value Object of matching class Data types classes converted Boolean Byte Double Short Integer Long Float Example ArrayList Integer myL new LinkedList Integer myL add 1 previously myL add new Integer 1 int y mL getFirst previously int y mL getFirst intValue Also see example in SortValues java 22 Comparable Interface Comparable public int compareTo Object o A compareTo B returns Negative if A B 0 if A B positive if A B Properties Referred to as the class s natural ordering Can sort using Collections sort Arrays sort Example Collections sort myList Can use as keys in SortedMap SortedSet Consistency w equals strongly recommended x equals y if and only if x compareTo y 0 23 Comparator Interface


View Full Document

UMD CMSC 132 - Object-Oriented Programming & Java Language Constructs

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Loading Unlocking...
Login

Join to view Object-Oriented Programming & Java Language Constructs and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming & Java Language Constructs and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?