You are on page 1of 14

Java Classes and Objects

Object Oriented Terms

Abstraction
Before abstraction: Porche, Pinto, Semi, Motorcycle After abstraction: Vehicle

Encapsulation
All vehicles have: number of wheels, color, start(), accelerate(), decelerate(), turnOff()

Object Oriented Terms


Inheritance Polymorphism
Method Overloading
start(Key key) start (Screwdriver s, Hammer, h) The list of input variables must be unique for each overloaded method

Method Overriding
For vehicle: start() { engineStatus = ON } For bicycle: start() { throw new DoesNotStartEx()}

Access Specifiers/Modifiers

Private, Protected, Public, (none) Accessed By this class only All Package Inherited? No

Private Public <none>

Protected this class only

Yes
Yes Yes

Returning values from Methods


If your method returns something, use


return <variable>;

If your method returns nothing (void), you can use return alone.
return; Note: only use this when you want to exit the method early. No need to put it at the end!

Constructors
No return type If a class does not have a constructor defined, the compiler automatically adds a default constructor with no input values. Constructors are NOT inherited!

If you wish to inherit a constructor, you must write out the signature and call the parents constructor using the super keyword

Variable Scope
{} define the scope of a variable When using catch, for, and while, remember that items declared within the catch, for, or while are only in scope within the following {}

{
Try { //ex out of scope} Catch (Exception ex) {// ex in scope} Finally { // ex out of scope }

//ex out of scope }

The Stack and the Heap


The stack is an orderly storage of data that is currently in scope The heap is an disorderly storage of data that may or may not be in scope Primitive data types exit on the stack Objects exist in the heap, but if they are in scope, pointer(s) to them exist in the stack

The stack

Once popped from the stack, the variables are out of scope

Garbage Collection

Cleans the heap Done sporadically by JRE after an object is out of the scope of the stack finalize() method of an object is called before the memory occupied by that object is released System.gc() makes a request that garbage collection take place, but there is no guarantee that it will happen Setting reference variables to null marks the associated object for garbage collection

Passing Data To Methods


If you call a method with a primitive variable, a copy of it is made on the stack, so changing it in the method has no effect on your copy. If you call a method with an object, you pass a reference, so any modifications to the object are done to the same data used by your pointer.

The return of return


In addition to modifying classes that are passed into a method, you can return a reference to an item created in the method with the return statement When calling the method you can:

Use the return value: Dog dog = Kennel.getDog() Use it directly: Kennel.getDog().speak(); Disregard it: Kennel.getDog()

Building your own packages


Normally, your projects will include more than a single class. Therefore, it is best to organize them in packages Create a folder hierarchy that makes sense with classes in each folder The package keyword is used on the first line (above import statements!) of each class to define its package structure to be used in import statements package com.mypackage;

-classpath parameter

When compiling and running classes that use other classes, the compiler and JRE need to know the location of the binaries of the classes in your import clauses.
javac -classpath . MyClass.java java -classpath . MyClass

When using multiple packages, you must do it from the root root by package Javac classpath mypackage/* *.java Java classpath mypackage/* ClsName

http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/classpath.html

You might also like