Java Summary

Document Sample
Java Summary
Shared by: Anna Uls
Stats
views:
303
posted:
6/7/2009
language:
English
pages:
14
Java Summary



• OOP

• classes, data and methods

• definition and use

• public and private

• inheritance - extends keyword

• method overloading

• method overriding

• threads and dynamic memory

• libraries

Objects



• OOP - object oriented

programming

• code built from objects

• Java these are called classes

• Each class definition is coded in

a separate .java file

• Name of the object must match

the class/object name

Classes





eg. Fruit.java contains the lines:



class Fruit{

int grams;

int cals_per_gram;

}

Methods ...

Class Fruit{

int grams;

int cals_per_gram;



int total_calories(){

return(grams*cals_per_gram);

}

}

• brackets () signify a method

rather than data

Using objects



• Here, code in one class creates

an instance of another class and

does something with it …

Fruit plum=new Fruit();

int cals;

cals = plum.total_calories();

• Dot operator allows you to

access (public) data/methods

inside Fruit class

Public/private



• Methods/data may be declared

public or private meaning they

may or may not be accessed by

code in other classes …

• Good practice:

– keep data private

– keep most methods private

• well-defined interface between

classes - helps to eliminate

errors

Creating objects

• Following code creates an

instance of the Fruit class

Fruit plum;

• defines the plum object

plum = new Fruit();

• creates it in memory

• the content of the Fruit class

must be defined in another file

Fruit.java

Constructors



• The line

plum = new Fruit();

• invokes a constructor method

with which you can set the

initial data of an object

• You may choose several

different type of constructor

with different argument lists

eg Fruit(), Fruit(a) ...

Overloading



• Can have several versions of a

method in class with different

types/numbers of arguments

• Fruit(){grams=50;}

Fruit(a,b){

grams=a;cals_per_gram=b;

}

• By looking at arguments Java

decides which version to use

Inheritance ...



• Important feature of OOP - new

classes can be based on existing

classes eg. Could define a

`specialized’ type of Fruit class

called Citrus …

• Has all methods of Fruit plus

possibly some new ones eg

class Citrus extends Fruit{

void squeeze(){….}

}

Inheritance II



• How to use …

eg.

Citrus lemon = new Citrus();

lemon.squeeze();

lemon.total_calories();

• old methods exist alongside

new methods …

Overriding

• Even more powerful concept of

OOP

– can override the functionality of

one method in a descendant class

• eg. Add method peel() to Fruit

class. Since Citrus extends Fruit

this method will also be

available to an instance of

Citrus

• But can redefine content of

peel() inside of Citrus - the new

definition hides the earlier ...

Threads ...



• Java allows code in some

objects to be run like a separate

program - it is given its own

thread of execution

• eg. Runner = new Thread(sim)

• sim is name of class containing

a run() method which is

responsible for eg a simulation

• class sim implements

Runnable{ ..run(){…}}

Libraries



• Java comes with libraries for

creating GUIs and network

applications and for embedding

in Web pages-

java.applet.Applet

• eg import java.awt.*;

• compile to byte code - can be

run by any system with a Java

interpreter - portable!

• Relatively robust and secure


Share This Document


Related docs
Other docs by Anna Uls
54 Summary Worksheet
Views: 16  |  Downloads: 2
Machine Summary of SuperB workshop in Hawaii
Views: 5  |  Downloads: 0
Summary (Break Down)
Views: 13  |  Downloads: 2
Summary_yes-no
Views: 3  |  Downloads: 0
Summary - Part 3 - Objectives
Views: 2  |  Downloads: 1
REV EXP SUMMARY
Views: 17  |  Downloads: 0
MI County Summary
Views: 4  |  Downloads: 0
CALLA Instruction Summary
Views: 10  |  Downloads: 0
Pine summary
Views: 3  |  Downloads: 0
Project Summary (1 slide)
Views: 26  |  Downloads: 0
by registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!