Java Summary - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Java Summary

Description:

Add method peel() to Fruit class. ... sim is name of class containing a run() method which is responsible for eg a simulation ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 15
Provided by: simonca8
Category:
Tags: class | java | summary

less

Transcript and Presenter's Notes

Title: Java Summary


1
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

2
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

3
Classes
  • eg. Fruit.java contains the lines
  • class Fruit
  • int grams
  • int cals_per_gram

4
Methods ...
  • Class Fruit
  • int grams
  • int cals_per_gram
  • int total_calories()
  • return(gramscals_per_gram)
  • brackets () signify a method rather than data

5
Using objects
  • Here, code in one class creates an instance of
    another class and does something with it
  • Fruit plumnew Fruit()
  • int cals
  • cals plum.total_calories()
  • Dot operator allows you to access (public)
    data/methods inside Fruit class

6
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

7
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

8
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) ...

9
Overloading
  • Can have several versions of a method in class
    with different types/numbers of arguments
  • Fruit()grams50
  • Fruit(a,b)
  • gramsacals_per_gramb
  • By looking at arguments Java decides which
    version to use

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

11
Inheritance II
  • How to use
  • eg.
  • Citrus lemon new Citrus()
  • lemon.squeeze()
  • lemon.total_calories()
  • old methods exist alongside new methods

12
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 ...

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

14
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
Write a Comment
User Comments (0)
About PowerShow.com