Unformatted text preview:

CMSC 132 Object Oriented Programming II Java Support for OOP Department of Computer Science University of Maryland College Park Object Oriented Programming OOP OO Principles Abstraction Encapsulation Abstract Data Type ADT Implementation independent interfaces Data and operations on data Java Many language features supporting OOP Overview Objects class References alias levels of copying this super reference Constructor initialization block Garbage collection destructor Package scope Modifiers Public Private Protected Static Final Abstract Object Class Object Abstracts away data algorithm details Encapsulates data Instances exist at run time Class Blueprint for objects of same type Exists at compile time References Aliases Reference A way to get to an object not the object itself All variables in Java are references to objects Alias Multiple references to same object x y operator tests for alias x equals y tests contents of object potentially Reference x Object z Reference y Three Levels of Copying Objects Assume y refers to object z 1 Reference copy Makes copy of reference x y 2 Shallow copy Makes copy of object x y clone 3 Deep copy x y x z z y z Makes copy of object z x and all objects directly or indirectly referred to by z y z z this Reference Description Reserved keyword Refers to object through which method was invoked Allows object to refer to itself Use to refer to instance variables of object this Reference Example class Node value val1 value val2 void foo value val2 val1 same as this val1 implicit this val2 parameter to method this val2 instance variable for object bar this passes reference to object Inheritance Definition Relationship between classes when state and behavior of one class is a subset of another class Terminology Superclass parent More general class Subclass More specialized class Forms a class hierarchy Helps promote code reuse super Reference Description Reserved keyword Refers to superclass Allows object to refer to methods variables in superclass Examples super x super super foo accesses variable x in superclass invokes constructor in superclass invokes method foo in superclass Constructor Description Method invoked when object is instantiated Helps initialize object Method with same name as class w o return type Default parameterless constructor If no other constructor specified Initializes all fields to 0 or null Implicitly invokes constructor for superclass If not explicitly included Constructor Example class Foo Foo constructor for Foo class Bar extends Foo Bar constructor for Bar implicitly invokes Foo here class Bar2 extends Foo Bar2 constructor for bar super explicitly invokes Foo here Initialization Block Definition Block of code used to initialize static instance variables for class Motivation Enable complex initializations for static variables Control flow Exceptions Share code between multiple constructors for same class Initialization Block Types Static initialization block Code executed when class loaded Initialization block Code executed when each object created at beginning of call to constructor Example class Foo static A 1 static initialization block A 2 initialization block Variable Initialization Variables may be initialized At time of declaration In initialization block In constructor Order of initialization 1 Declaration initialization block in the same order as in the class definition 2 Constructor Variable Initialization Example class Foo static A 1 static initialization block static int A 2 static variable declaration static A 3 static initialization block B 4 initialization block private int B 5 instance variable declaration B 6 initialization block Foo constructor A 7 B 8 now A 7 B 8 initializations executed in order of number Garbage Collection Concepts All interactions with objects occur through reference variables If no reference to object exists object becomes garbage useless no longer affects program Garbage collection Reclaiming memory used by unreferenced objects Periodically performed by Java Not guaranteed to occur Only needed if running low on memory Destructor Description Method with name finalize Returns void Contains action performed when object is freed Invoked automatically by garbage collector Not invoked if garbage collection does not occur Usually needed only for non Java methods Example class Foo void finalize destructor for foo Method Overloading Description Same name refers to multiple methods Sources of overloading Multiple methods with different parameters Constructors frequently overloaded Redefine method in subclass Example class Foo Foo Foo int n 1st constructor for Foo 2nd constructor for Foo Package Definition Group related classes under one name Helps manage software complexity Separate namespace for each package Package name added in front of actual name Put generic utility classes in packages Avoid code duplication Example package edu umd cs name of package Package Import Import Make classes from package available for use Java API java core javax optional Example import java util Random import single class import java util all classes in package class definitions Scope Scope Part of program where a variable may be referenced Determined by location of variable declaration Boundary usually demarcated by Example public MyMethod1 int myVar myVar accessible in method between Scope Example Class Package Method Method package edu umd cs public class MyClass1 public void MyMethod1 public void MyMethod2 public class MyClass2 Class Scopes Example Modifier Description Java keyword added to definition Specifies characteristics of a language construct Partial list of modifiers Public private protected Static Final Abstract Modifier Examples public class Foo private static int count private final int increment 5 protected void finalize public abstract class Bar abstract int go Visibility Modifier Properties Controls access to class members Applied to instance variables methods Four types of access in Java Public Protected Package Default if no modifier specified Private Most visible Least visible Visibility Modifier Where Visible public Referenced anywhere i e outside package protected Referenced within package or by subclasses outside package None specified package Referenced only within package private Referenced only within class definition Applicable to class fields methods Visibility Modifier For instance variables Should usually be private to enforce encapsulation Sometimes may be protected for


View Full Document

UMD CMSC 132 - Java Support for OOP

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 Java Support for OOP 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 Java Support for OOP 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?