Upload
happyruss
View
216
Download
0
Embed Size (px)
Citation preview
7/30/2019 Java: Week 6
1/14
Java Classes and
Objects
7/30/2019 Java: Week 6
2/14
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()
7/30/2019 Java: Week 6
3/14
Object Oriented Terms
Inheritance
Polymorphism Method Overloading
start(Key key)
start (Screwdriver s, Hammer, h)
The list of input variables must be unique for eachoverloaded method
Method Overriding For vehicle: start() { engineStatus = ON }
For bicycle: start() { throw new DoesNotStartEx()}
7/30/2019 Java: Week 6
4/14
Access Specifiers/Modifiers
Private, Protected, Public, (none)
Accessed By Inherited?
Private
this class only NoProtected
this class only Yes
Public All Yes
Package Yes
7/30/2019 Java: Week 6
5/14
Returning values from Methods
If your method returns something, use return ;
If your method returns nothing (void),
you can use return alone. return;
Note: only use this when you want to exit themethod early. No need to put it at the end!
7/30/2019 Java: Week 6
6/14
Constructors
No return type
If a class does not have a constructordefined, the compiler automatically adds a
default constructor with no input values.
Constructors are NOT inherited!
If you wish to inherit a constructor, you mustwrite out the signature and call the parentsconstructor using the super keyword
7/30/2019 Java: Week 6
7/14
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 scopewithin the following {}{
Try { //ex out of scope}
Catch (Exception ex) {// ex in scope}Finally { // ex out of scope }
//ex out of scope
}
7/30/2019 Java: Week 6
8/14
The Stack and the Heap
The stack is an orderly storage of datathat is currently in scope
The heap is an disorderly storage of datathat may or may not be in scope
Primitive data types exit on the stack
Objects exist in the heap, but if they arein scope, pointer(s) to them exist in thestack
7/30/2019 Java: Week 6
9/14
The stack
Once popped from the stack, the
variables are out of scope
7/30/2019 Java: Week 6
10/14
Garbage Collection
Cleans the heap
Done sporadically by JRE after an object is outof the scope of the stack
finalize() method of an object is called beforethe memory occupied by that object is released
System.gc() makes a request that garbage
collection take place, but there is no guaranteethat it will happen
Setting reference variables to null marks theassociated object for garbage collection
7/30/2019 Java: Week 6
11/14
Passing Data To Methods
If you call a method with a primitivevariable, 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, youpass a reference, so any modifications to
the object are done to the same data usedby your pointer.
7/30/2019 Java: Week 6
12/14
The return ofreturn
In addition to modifying classes that arepassed into a method, you can return areference 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()
7/30/2019 Java: Week 6
13/14
Building your own packages
Normally, your projects will include morethan a single class. Therefore, it is best toorganize them in packages
Create a folder hierarchy that makessense with classes in each folder
The package keyword is used on the first
line (above import statements!) of eachclass to define its package structure to beused in import statements
package com.mypackage;
7/30/2019 Java: Week 6
14/14
-classpath parameter
When compiling and running classes that useother classes, the compiler and JRE need toknow 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