Java Recapitulare
Java Recapitulare
Read it , do it , know it !
Requirements
If you are not prepared to recap C programming language object oriented programming You can stop reading now. Otherwise :
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
JAVA
All Java code runs on the Java Virtual Machine, so you have to install it (in the labs it is already installed). A virtual machine is like a software computer in a host operating system .
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
A class is a type . For example a class of cars can be Ford. An object is an instance of a class, a materialization of the type. For example: Ford DJ_69_SEX ; // DJ_69_SEX is an object of type Ford
/** this is where you describe your method * @param args arguments received when application is executed */ public static void main(String[] args) { // name type and parameters of the main method are predefined // TODO Auto-generated method stub //"TODO" comments usually mean you should write some code here //this is where you start to implement your application } }
Packages
think of packages as folders for classes: - in the source *.java file it looks like this:
package ro.ucv.main; // ... class definition
Destructor
In C programming language these were special methods called when an object of the class is explicitly cleared from memory. Java has no destructor. Instead it has a garbage collector that will destroy objects for you. Static objects will not be destroyed until the end of the execution of the application.
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
public class Boss extends Worker { ... } In this case class Boss can do everything Worker can do plus whatever you will implement especially for Boss.
For your information : In Java all classes implicitly extend the Object class.
class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e) { } public void setLayout( LayoutManager l) {
} }
class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }
class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }
class JFrame{ private int WIDTH=0, HEIGHT=0; public JFrame(){ } protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ }
protected members are private members that are also accessible from classes that extend this class class JFrame{
private int WIDTH=0, HEIGHT=0; public JFrame(){ }
protected void processWindowEvent (WindowEvent e){ // this method overrides the analogous // method in JFrame }
protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ } }
public members are protected members that are also accessible directly from objects like so : JFrame obj=new JFrame(); obj.setLayout(null);
protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ } }
public void setLayout( LayoutManager l){ // this method overrides the analogous // method in JFrame }
} protected void processWindowEvent (WindowEvent e){ } public void setLayout( LayoutManager l){ } }
if in the override methods (in MyGUI) you want to call the initial, overridden method as well use super = a pointer to the extended class (superclass)
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
Abstract Classes
Example: an action at left click. You are the one creating the class that detects the left click allowing others to extend it with their own functionality. An abstract method has no body, it is meant to be overridden. When a left click event is detected just call an abstract method and allow the extending class to implement it. A class that contains abstract methods is an abstract class. It cannot be instantiated since it is not yet complete. Someone could, however, extend it and implement all abstract methods.
abstract class MouseListener{ //some code to detect left mouse //event and call the abstract method //onLeftClick abstract void onLeftClick(MouseEvent e);
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
Interfaces
An abstract type containing only method signatures (no bodies) and constants.
interface Predator{ static final int aprxWeight; public Stats getStatistics(); }
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
where receiveObject() is a generic fictitious method that receives an object over the network.
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
public class ButtonMapsAction implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // do something when the button is pushed } }
Inputs from mouse - example, not actual code public class PictureGUI extends JFrame implements MouseListener { public PictureGUI() { // create the GUI } @Override public void mouseClicked(MouseEvent e) { //do something on left click } @Override public void mouseEntered(MouseEvent arg0) { //do something when mouse pointer enters the frame } @Override public void mouseExited(MouseEvent arg0) { // do something when mouse pointer exits the frame } @Override public void mousePressed(MouseEvent arg0) { } @Override public void mouseReleased(MouseEvent arg0) { } }
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
Java Archive
A java archive (JAR) is a compact format for a java project. You can include jars implemented by others in your own project: copy them in your project and include them. In Eclipse it looks like this:
copy
include
Overview
1. Installation of Java 2. Object Oriented Programming 1. classes 2. extending 3. abstract classes 4. interfaces 5. transferring object over the network 3. Graphical User Interfaces 4. Using Jars 5. Running java code 6. Example
Prepare yourself ! You are about to make all your dreams come true.