









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Abstraction Encapsulation
Implementation independent interfaces Data and operations on data
Many language features supporting OOP
Overview
Public, Private, Protected Static, Final, Abstract
Object & Class
Abstracts away (data, algorithm) details Encapsulates data Instances exist at run time
Blueprint for objects (of same type) Exists at compile time
“this” Reference
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
Relationship between classes when state and behavior of one class is a subset of another class
Superclass / parent ⇒ More general class Subclass ⇒ More specialized class
“super” Reference
Reserved keyword Refers to superclass Allows object to refer to methods / variables in superclass
super.x // accesses variable x in superclass super() // invokes constructor in superclass super.foo() // invokes method foo() in superclass
Initialization Block
Block of code used to initialize static & instance variables for class
Enable complex initializations for static variables Control flow Exceptions Share code between multiple constructors for same class
Initialization Block Types
Code executed when class loaded
Code executed when each object created (at beginning of call to constructor)
class Foo {
static { A = 1; } // static initialization block { A = 2; } // initialization block
}
Variable Initialization
At time of declaration In initialization block In 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
Method Overloading
Same name refers to multiple methods
Multiple methods with different parameters Constructors frequently overloaded Redefine method in subclass
class Foo { Foo( ) { … } // 1st^ constructor for Foo Foo(int n) { … } // 2nd^ constructor for Foo }
Package
Group related classes under one name
Separate namespace for each package Package name added in front of actual name Put generic / utility classes in packages Avoid code duplication
package edu.umd.cs; // name of package
Package – Import
Make classes from package available for use Java API java.* (core) javax.* (optional)
import java.util.Random; // import single class import java.util.*; // all classes in package … // class definitions
Scope
Part of program where a variable may be referenced Determined by location of variable declaration Boundary usually demarcated by { }
public MyMethod1() { int myVar; myVar accessible in ... method between { } }
Modifier
public class Foo { private static int count; private final int increment = 5; protected void finalize { … } } public abstract class Bar { abstract int go( ) { … } }
Visibility Modifier
Controls access to class members Applied to instance variables & methods
Public Most visible Protected Package Default if no modifier specified Private Least visible
Visibility Modifier – Where Visible
Referenced anywhere (i.e., outside package)
Referenced within package, or by subclasses outside package
Referenced only within package
Referenced only within class definition Applicable to class fields & methods
Visibility Modifier
Should usually be private to enforce encapsulation Sometimes may be protected for subclass access
Public methods – provide services to clients Private methods – provide support other methods Protected methods – provide support for subclass
Modifier – Final
Method can not be overridden by subclass Private methods are implicitly final
Class can not be a superclass (extended) Methods in final class are implicitly final
Modifier – Final
Prevents inheritance / polymorphism May be useful for Security Object oriented design
Programs can depend on properties specified in Java library API Prevents subclass that may bypass security restrictions
Modifier – Abstract
Represents generic concept Can not be instantiated
Placeholder in class hierarchy Can be partial description of class Can contain non-abstract methods Required if any method in class is abstract
abstract class Foo { // abstract class abstract void bar( ) { … } // abstract method