Title: Programming Principles using Java
1Programming Principles using Java
2Object Oriented Programs
- Object Oriented Programs consist of classes and
objects - We create objects from classes and then use the
objects by sending messages to them
3Defining the Dog class
- What attributes do all dogs have in common?
- What behaviours do all dogs have in common?
4Class Diagram
Class name
Attribute name
Operations
5Dog class in Java
- class Dog
- int head1, tail1, legs4//dog attributes
- //dog behaviours
- void run()
- System.out.println(I am running)
-
- void sit()
- System.out.println(I am sitting)
-
- void bite()
- System.out.println(I am biting)
-
-
6What is a Class ?
- A class is a
- Template for objects
- Pattern for objects
- Blueprint for objects
- Factory for objects
- Description for objects
- Classes make objects in their own image
- Classes are abstract, objects are concrete
7Creating Objects from Classes
- Dog snoopy new Dog()
- Dog lassie new Dog()
- Dog scooby new Dog()
- Then we can make the objects do something by
sending messages to them - snoopy.sit()
- lassie.run()
- scooby.bite()
8Classes
- Classes facilitate code reuse
- Dog snoopy, lassie, scoobyetc
- BankAccount jim_ac, tim_ac, jane_ac..etc
- Classes facilitate code libraries
- java.awt, java.net, java.io, etc
9Inheritance reusing classes
10Inheritance
- Inheritance is a way of reusing classes
- reusing classes saves writing code
- Inheritance takes the attributes and methods of a
class and adds to them
11Inheritance
- Inheritance implements an IS-A relationship
- Where ANIMAL is the superclass and
- DOG is a subclass
- A DOG IS-A ANIMAL
- So a dog inherits all the
- features of an animal
12Inheritance
Aeroplane
wings engines
take-off() fly() land()
PassengerJet
Fighter
passengers attendants
guns bombs
serve()
fire() drop()
13Inheritance uses extends
- public class PassengerJet extends Aeroplane
-
- //attributes
- private int passengers, attendants
-
- //method
- public void serve()
- System.out.println( attendants serve passengers
meals) -
-
14World Wide Web
- Internet - a network of networks
- World Wide Web - built on top of the Internet
- contains web pages, held on web servers, which
can be downloaded onto web clients - web browsers - web pages are written in HTML
15Applets
- Applets are Java programs that run in a web page
in a browser - all Applets are subclasses of the Applet class
- Applets do not have a main() method as Java
applications do
16Web pages and APPLET tags
- ltHTMLgt
- ltBODYgt
- ltAPPLET CODELighting.class
- HEIGHT300 WIDTH400gt lt/APPLETgt
- lt/BODYgt
- lt/HTMLgt
17Applet methods
- init() is used for initialiation, it is called
only once, when the applet is first run - start() is run each time the web page is
loaded - stop() is run when the browser leaves the web
page containing the applet - destroy() used to relinquish any system
resources it has allocated - You may need to override these inherited methods
18- import java.awt.
- import java.awt.event. import
java.applet. -
- public class Lighting extends Applet implements
ActionListener -
- private Button lightswitch
- private Label display
- private boolean state false
-
- public void init()
-
- lightswitch new Button(On/Off) //
creates a Button object - display new Label(OFF)
- lightswitch.addActionListener(this)
- //register the applet to receive and
handle ActionEvent events from the button -
- add(lightswitch) //add the button to the
applet - add(display)
-
19private Button lightswitch private Label
display private boolean state false public
void init() lightswitch new
Button(On/Off) // creates a Button object
display new Label(OFF)
lightswitch.addActionListener(this)
//register the applet to receive and handle
ActionEvent events from the button
add(lightswitch) //add the button to the applet
add(display)
20(No Transcript)