(5) Java Support for Object Oriented Programming

(5) Java Support for Object Oriented Programming - CMSC...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMSC 132: Object-Oriented Programming II Java Support for Object- Oriented Programming Department of Computer Science University of Maryland, College Park 1 2 Overview Objects and class, this , super References, alias, levels of copying Constructor, initialization block Garbage collection, destructor Implementing equals() , and cloning Package, scope, inner classes Modifiers public , private , protected static , final , abstract Generic programming 2 3 Object-oriented programming (OOP) Object-oriented principles Abstraction Encapsulation Abstract data type (ADT) Implementation-independent interfaces Data and operations on data Java Many language features supporting OOP 3 4 Objects and classes Object Abstracts away details of data and algorithms Encapsulates data Instances exist during program execution at runtime Class A blueprint for objects (of same type) Exists at compile time 4 5 References and aliases Reference A way to get to an object, not the object itself All variables in Java are references to objects Aliasing refers to multiple references to the same object In Java, " x == y " tests for aliasing Object z Reference x Reference y 5 6 Three levels of copying objects Assume y refers to object z y z y x z z ' y x z z ' 1. Reference copy Make a copy of the reference x= y , or pass y into x 2. Shallow copy Make a copy of object 3. Deep copy Make a copy of the object z and all objects (directly or indirectly) referred to by z x 6 7 The this reference Description A reserved keyword that refers to the object through which a method was invoked Allows objects to refer to themselves Is used to refer to instance variables of an object 7 8 The this reference example 8 class Node { value val1; value val2; void method1(Node m) { } void method2(value val2) { = val1; // same as this.val1 (implicit this) = val2; // parameter to method = this.val2; // instance variable for object f(this); // passes reference to current obj. Node n; n.method1(this); } } 9 Inheritance Definition Relationship between classes when state and behavior of one class is a subset of another class Terminology A superclass (also called a parent or base class) is a more general class A subclass is a more specialized class Forms a class hierarchy Helps promote code reuse 9 10 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 C { C () { } // first constructor for C C (int n) { } // second constructor for C } 10 11 The super reference Description Reserved keyword Refers to superclass Allows object to refer to methods / variables in superclass Examples: super.x // accesses variable x in superclass super() // invokes constructor in superclass super.f() // invokes method f() in superclass...
View Full Document

Page1 / 10

(5) Java Support for Object Oriented Programming - CMSC...

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

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