Java Short Review - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Java Short Review

Description:

... world objects, which has states and behaviour. State - Data/Information ... Declaring methods that one or more classes are ... initializing new objects ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 35
Provided by: adiantow
Category:

less

Transcript and Presenter's Notes

Title: Java Short Review


1
Java Short Review
Source http//java.sun.com/docs/books/tutorial
15 Jan 2004
Adianto Wibisono
2
Outline
  • General Concepts
  • Object, Messages, Class, Inheritance, Interfaces
  • Class Structure
  • Class Declaration, Class Body, Constructor,
    Member Variables, Methods, Access Control,
    Instance Class Members
  • Examples Walkthrough
  • Hello World, Other simple examples describing the
    concepts.
  • Practical Information
  • Compiling, environment setup (CLASSPATH), using
    existing packages, Java tools

3
General Concept
  • Object Oriented Programming Overview
  • What is object ?
  • How object interact ?
  • What is a class ?
  • What is inheritance ?
  • What is interface ?

4
What is Object ?
  • Taken from real world objects, which has states
    and behaviour
  • State lt-gt Data/Information ?Variable
  • Behaviour lt-gt Function ?Methods
  • Definition 
  • An object is a software bundle of variables and
    related methods.

5
Objects
Bycicle Object
6
Object Interaction Message passing
Receiver/Target Object
7
Class
  • Definition 
  • A class is a blueprint, or prototype, that
    defines the variables and the methods common to
    all objects of a certain kind.

8
Class Instances a.k.a Objects
Class
Instance/Object
Instance/Object
9
Inheritance
Superclass
Subclasses
10
Inheritance
  • Subclasses provide specialized behaviors from the
    basis of common elements provided by the
    superclass.
  • Enable reuse of the code in the superclass.
  • Superclasses can be abstract classes that define
    "generic" behaviors
  • Not all members of this abstract classes need to
    be implemented, it will be done on the subclasses

11
Interface
  • Definition
  • A Java(TM) programming language keyword used to
    define a collection of method definitions and
    constant values. It can later be implemented by
    classes that define this interface with the
    "implements" keyword

12
Interface
  • Usage
  • Capturing similarities among unrelated classes
    without artificially forcing a class
    relationship.
  • Declaring methods that one or more classes are
    expected to implement.
  • Revealing an object's programming interface
    without revealing its class

13
Class Structure
  • Structure Outline
  • Class Declaration
  • Class Body
  • Constructor
  • Member Variables
  • Instance Class Members
  • Implementing Method
  • Control Access

14
Structure Outline
15
Class Declaration
Required
16
Class Body
  • Body of a class contains
  • Constructors for initializing new objects
  • Declaration of Variables providing the state of
    the class and its objects
  • Methods to implement the behaviour of the class
    and its objects
  • Ocassionally a finalize method for cleaning up an
    object

17
Constructor
  • Has the same name as the class
  • Can have many constructor with different
    parameter list, but the same name (overloading)
  • Automatically invoked when you create an object
    with operator new

18
Member Variables
19
Instance vs Class Member
  • Instance variable/method ? Belong specifically to
    the instance
  • Class variable/method ? Belong to the class (all
    instances has it)
  • Declared as static variables

20
Instance vs Class Member
21
Implementing Method
22
Control Access of a Method/Member Variable
  • Private
  • accessible only to the class in which it is
    defined
  • Protected
  • Accessible to the class itself, subclasses, and
    classes in the same package
  • Public
  • Any class, Any Package has access to this method
    / member variable

23
Example Walkthrough
  • Hello World
  • Bycicle Class
  • Using Bycicle class
  • Inheritance example
  • Interface example
  • Collection
  • Exception Handling

24
Hello World
  • /
  • The HelloWorldApp class implements an
    application that
  • displays "Hello World!" to the standard
    output. /
  • public class HelloWorldApp
  • public static void main(String args)
  • // Display "Hello World!"
  • System.out.println("Hello World!")

Class Variable/Member
Instance Method
25
Bycicle Class
  • public class Bycicle
  • protected int speed
  • protected int gear
  • Bycicle()
  • speed0
  • gear1
  • public void changeGear(int newGear)
  • gear newGear
  • public void increaseSpeed(int deltaSpeed)
  • speed deltaSpeed
  • public void printState()
  • print(Bycicle speed is speed in
    gear gear)

26
Using Bycicle Class
  • public class UseBike
  • public static void main(String args)
  • Bycicle bike new Bycicle()
  • bike.increaseSpeed(10)
  • bike.changeGear(2)
  • bike.printState()

27
RacingBike Inherit Bycicle
  • public class RacingBike extends Bycicle
  • public void boostSpeed()
  • speed speed10

Specialized function specific to this subclass
28
Using RacingBike Class
  • public class UseRacingBike
  • public static void main(String args)
  • RacingBike bike new RacingBike()
  • bike.increaseSpeed(10)
  • bike.changeGear(2)
  • bike.printState()
  • bike.boostSpeed()
  • bike.printState()

29
Interface TravelingObj
  • public interface TravelingObj
  • public int getDistance(int time)
  • public void printDistance(int time)

30
Sprinter Implements TravelingObj
  • public class Sprinter implements TravelingObj
  • protected int speed
  • protected String name
  • Sprinter (String name, int speed)
  • this.name name
  • this.speed speed
  • public int getDistance(int time)
  • int distance time speed
  • return distance
  • public void printDistance(int time)
  • System.out.println(Distance travelled so far by
    name is time speed)

31
Sprinter Bike treated as the TravelingObj
  • public class UseTravelingObj
  • public static void printDistance(TravelingObj
    obj)
  • obj.printDistance(10)
  • public static void main(String args)
  • Bycicle bike new Bycicle()
  • Sprinter ali new Sprinter("Ali baba",
    10)
  • printDistance(ali)
  • printDistance(bike)

32
Use Collection
dont reinvent the wheel.
  • import java.util.
  • public class Sort
  • public static void main(String args)
    List l Arrays.asList(args) Collections.sort(l
    ) System.out.println(l)

33
Sample Exception Handling
  • class ErrorTest
  • public static void main(String args)
  • int arrInt new int20
  • int i 0
  • for (i 0 i lt 20 i)
  • arrInti (int)(Math.random()100)
  • try
  • for (i 0 i lt 21 i)
  • System.out.println("Value at " i "
    " arrInti)
  • catch (ArrayIndexOutOfBoundsException e)
  • System.out.println("Index out of bounds at
    index " i)

34
Practical Information
  • How to compile
  • About classpath
  • About Packages
  • Java tools
Write a Comment
User Comments (0)
About PowerShow.com