JAVA COURSE LESSON2 - PowerPoint PPT Presentation

About This Presentation
Title:

JAVA COURSE LESSON2

Description:

Java call the methods based on the type of the class it is most of the time in compile time ... All the classes in JAVA are inherited from the Object class. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 16
Provided by: iCs8
Category:
Tags: course | java | java | lesson2

less

Transcript and Presenter's Notes

Title: JAVA COURSE LESSON2


1
JAVA COURSE LESSON2
  • BY OMPUTER ENGINEEING ASSOCIATION

2
What we can learn today?
  • Review of OO programming
  • Exception concept
  • Class concept
  • hashtable
  • Basic event handling
  • Basic jswing uses

3
OO programming
  • OO programming includes encapsulation,
    inheritance and polymorphism.
  • We will talk more about polymorphism.
  • Java call the methods based on the type of the
    class it is most of the time in compile time
  • But, with the use of inheritance, we can use the
    superclass type and call the methods from the
    class inherited from it.
  • This is called polymorphism.
  • Which the calling of method is resolved in run
    time, but not in compile time (dynamic!)

4
Example
  • e.g There are three classes, like employee,
    hourly empoloyee and contract employee
  • Which now the last two inherit employee class
  • Employee class do not have payment() method
  • But the other two have!
  • When we type
  • Employee temp new ConEmployee()
  • We cannot call temp.payment()! As JAVA resolve it
    by type
  • But if the super class employee also has this
    payment() method, then we can call
    temp.payment()!

5
Exception Handling
  • Exception is not something program errors.
  • It maybe a matter of runtime unpredictable
    hardware problems, or usage or program.
  • In JAVA, exception are finely classified into
  • We need to handle
  • And we don't need to handle type
  • We would demonstrate in two examples

6
The type we must handle
  • Public static void main(String args)
  • Try
  • Thread.sleep(100)
  • Catch (Exception e)
  • System.out.println(caught)
  • In the above example, we must do something for
    the Thread.sleep(100) method as it throws the
    exception and it is the type we must handle.
    First type!.

7
Something we can ignore
  • Public static void main(String args)
  • Int a 1,2,3,4,5
  • A6 9
  • It compiles well!
  • But it throws ArrayIndexOutOfBoundsException in
    runtime.
  • BUT WE CAN STILL NOT CATCH IT!

8
Two ways we can deal with exception
  • First, code it to deal with it
  • Try
  • Statement may throw exception
  • Catch (Exception e)
  • Finally
  • The catch block is used to catch certain type
    exception and code the problem. (As Exception is
    the SUPERCLASS of all exception, so the
    statements above catch all exception.)
  • The finally block must execute before any return
    and after the try and catch blocks.

9
  • Even you type return in try block, the statements
    in finally block is executed!
  • After the try block, finally or catch blocks must
    be executed!

10
Second method
  • We don't handle the exception, but we throw it
    away.
  • Like
  • void a() throws Exception
  • Thread.sleep(100)
  • Public static void main(String args)
  • Test temp new Test() try
  • temp.a()catch (Exception)//we catch it
  • We catch it in other block as the exception is
    threw in a() method.

11
Class concept
  • All the classes in JAVA are inherited from the
    Object class.
  • And thus all classes have all the methods in
    Object (if they are stating as right modifier in
    Object!)
  • Eight types are not classes
  • Stated in last lesson
  • But they can be class also
  • Integer temp new Integer(6)
  • The storage are not the same, will not explained
    here

12
Hashtable
  • Each element is a pair of
  • We just the key to find the element we want!
  • In java, hashtable is given in java.utils.Hashtabl
    e
  • We will use two methods in our examples
  • get(key)
  • And put(key, object)
  • Example shown

13
Basic event handling
  • IN java Jswing, we can handle the action, like
    mouse action, window closing......with the Jswing
    elements to write interactive program
  • See the demonstration

14
WE would now teach the Jswing
  • Game.java is demonstrated.

15
The end
Write a Comment
User Comments (0)
About PowerShow.com