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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An in-depth exploration of object-oriented programming (oop) concepts in java, covering objects and classes, references and aliases, 'this' and 'super' references, constructors, initialization blocks, variable initialization, inheritance, and modifiers. Learn about encapsulation, abstract data types, interfaces, packages, and more.
Typology: Study notes
1 / 17
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
References & Aliases
A way to get to an object, not the object itself All variables in Java are references to objects
Multiple references to same object “X == Y“ operator tests for alias X.equals(Y) tests contents of object (potentially)
Object Z
Reference X
Reference Y
References & Aliases – Issues
References X = new Object(); Y = X; // Y refers to same object as X Objects X = new Object(); Y = X.clone(); // Y refers to different object
X = new Object(); Y = X; X.change(); // modifies object for Y
“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
Constructor
Method invoked when object is instantiated Helps initialize object Method with same name as class w/o return type 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
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
Garbage Collection
All interactions with objects occur through reference variables If no reference to object exists, object becomes garbage (useless, no longer affects program)
Reclaiming memory used by unreferenced objects Periodically performed by Java Not guaranteed to occur Only needed if running low on memory
Destructor
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
class foo { void finalize() { … } // destructor for foo }
Method Overloading
Same name refers to multiple methods
Multiple methods with different parameters Constructors frequently overloaded Redefine method in subclass
class foo { foo() { … } // 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 { } }
Scope – Example
package edu.umd.cs ; public class MyClass1 { public void MyMethod1() { ... } public void MyMethod2() { ... } } public class MyClass2 { }
Method
Method
Class Package
Class
Modifier
Java keyword (added to definition) Specifies characteristics of a language construct
Public / private / protected Static Final Abstract
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 – Static
Single copy for class Shared among all objects of class
Can be invoked through class name Does not need to be invoked through object Can be used even if no objects of class exist Can not reference instance variables
Modifier – Final
Value can not be changed Must be initialized in every constructor Attempts to modify final are caught at compile time
Used for constants Example final static int Increment = 5;
Modifier – Final
Method can not be overloaded 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
Interface
Collection of Constants Abstract methods Can not be instantiated
Must implement all methods in interface Example class foo implements bar { … } // interface bar
But class can “inherit” from multiple interfaces