Upload
happyruss
View
216
Download
0
Embed Size (px)
Citation preview
7/30/2019 Java: Week 4
1/15
Java is Object-Oriented
Classes, Inheritance, Interfaces,
Exceptions, Applets
7/30/2019 Java: Week 4
2/15
Class
Java is completely Object Oriented.
This means that everything must be a class
An object is an instance of a classA class has attributes and behavior (properties
and methods)
Lets discuss the Car class
7/30/2019 Java: Week 4
3/15
Class members
Properties
public static final int V6 = 0 Visibility (public, private, protected)
static (optional): class-wide variable, dont need an object
final (optional): means it cant be changed Type (int, String, Object, whatever)
Methods public static int getEngineType(int carType) {
Visibility (public, private)
static (or not)
Return type
Method name
Input parameters
7/30/2019 Java: Week 4
4/15
Get/Set Methods
It is best to utilize getters and setters for yourproperties, rather than making them public.
They enable you to validate the value being
assigned to the attribute Some values might be read-only. Therefore, you
may only want a getter.
Use the this keyword to refer to a class-levelvariable in your setters.
7/30/2019 Java: Week 4
5/15
Constructors
These methods have
Names that match the class name
No return type
They are used for initialization
You can overload constructors, as long as the
signature is different between each constructor.
Note Car
7/30/2019 Java: Week 4
6/15
Inheritance (extends)
java.lang.object is the root of all classes
Every class extends (inherits) a superclass(parent)
If you dont designate extends, then you aresimply inheritingjava.lang.object
extends can be chained. Inheriting one class
will inherit all non-private members up thehierarchy to java.lang.object
How might you use inheritance on CarObject?
7/30/2019 Java: Week 4
7/15
Interfaces (implements)
If two classes share many public members, it is agood idea to use interfaces
In the car example, tuneUpPinto() and
tuneUpPorche() could each be called tuneUp().You can create an interface with tuneUp().
public interface Servicable {
The Porche and Pinto would both implement it:public class Pinto extends Car implements Servicable
{
7/30/2019 Java: Week 4
8/15
Exception Handling
java.lang.Exception
Exceptions are throwable
Throwing an exception can be done by thesystems code or your code
Catching an exception allows you to trap it
and handle it.
7/30/2019 Java: Week 4
9/15
import java.util.Calendar;
public class Labs {public static void main(String args[]) {
Integer month = new Integer(args[0]);Integer day = new Integer(args[1]);Integer year = new Integer(args[2]);
Calendar cal = Calendar.getInstance();cal.setLenient(false);//note the order//note January = month 0cal.set(Calendar.DAY_OF_MONTH,
day.intValue());cal.set(Calendar.MONTH, month.intValue() - 1);cal.set(Calendar.YEAR, year.intValue());
System.out.println(cal.getTime() + valid!);
}
7/30/2019 Java: Week 4
10/15
try {//all the code
} catch (NumberFormatException ex) {//user typed in bad ints
} catch (ArrayIndexOutOfBoundsExceptionex) {//not a valid date
}} finally {//Want to play again?}
7/30/2019 Java: Week 4
11/15
Java Applets
Java applets have many methods called
automatically by the web browser:
public void init()
public void start()
public void paint(Graphics g)
public void stop()
public void destroy()
7/30/2019 Java: Week 4
12/15
Java Applet security
Applets loaded from the internet are considered
untrusted and therefore cant
Read or write files
Open a socket connection
Start a program on the machine
Call non-Java code
Applets running locally are trusted, so dont run
them from your email
7/30/2019 Java: Week 4
13/15
java.awt.Graphics
public void paint(Graphics g)
You can paint a rectangle, arc, circle, or oval
It can be filled or outlinedYou can also paint images and strings
See Page 128Table 3.5
7/30/2019 Java: Week 4
14/15
javax.swing.JFrame
A JFrame is a canvas for your paint object that is
not run in a browser
Ensure your class extends JFrame
public static void main(String[] args) is
how it is invoked (if necessary)
In the constructor, set the size and title for thewindow
You can do what you want, or if another object
will instantiate your class, youre done
7/30/2019 Java: Week 4
15/15
javax.swing
This library holds a host of other useful objects
JButton
JFileChooser
JCheckbox
Note that all of these must run on something
like a JApplet or JFrame