You are on page 1of 15

Java is Object-Oriented

Classes, Inheritance, Interfaces, Exceptions, Applets

Class

Java is completely Object Oriented. This means that everything must be a class An object is an instance of a class A class has attributes and behavior (properties and methods) Lets discuss the Car class

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

Get/Set Methods
It is best to utilize getters and setters for your properties, 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-level variable in your setters.

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

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 are simply inheriting java.lang.object extends can be chained. Inheriting one class will inherit all non-private members up the hierarchy to java.lang.object How might you use inheritance on CarObject?

Interfaces (implements)
If two classes share many public members, it is a good 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 {

Exception Handling

java.lang.Exception Exceptions are throwable Throwing an exception can be done by the systems code or your code Catching an exception allows you to trap it and handle it.

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 0 cal.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!);

try { //all the code } catch (NumberFormatException ex) { //user typed in bad ints } catch (ArrayIndexOutOfBoundsException ex) { //not a valid date } } finally { //Want to play again? }

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()

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

java.awt.Graphics
public void paint(Graphics g) You can paint a rectangle, arc, circle, or oval It can be filled or outlined You can also paint images and strings See Page 128 Table 3.5

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 the window You can do what you want, or if another object will instantiate your class, youre done

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

You might also like