Referencing other classes in classes(1)
Referencing other classes in classes(1)
Interface
● An interface is a named collection of abstract methods (methods without
implementations), and constant declarations.
● An interface is not a class. → You cannot create variables of an interface.
● Instead, a class implements an interface.
● When a class implements an interface, it does two things.
o Promises to provide bodies for all the functions that interface defines.
o Gets the variables described by the interface for free.
● Note that in an interface:
o All the methods are abstract; that is, they have a name, parameters, and
a return type, but they don’t have anything inside them.
o All the methods are implicitly public.
o All data declarations are implicitly constant declarations. i.e., public static
final. This implies that instance fields cannot be declared in interface.
o interfaces are not classes; i.e., they cannot be instantiated. Thus, the line
Measurable x = new Measurable (); is a mistake!
public class ClassName implements InterfaceName {}
Casting
Type converting: interface to class
You need to do this cuz interface lets you have max variable for multible
classes – it can be maximum for coin or for BankAccount
Risks of casting is that there is risk of losing information like int x = (int)
2.4 and when we try to cast object type into type that is not an object (in
that case we use instanceof -> if(x instanceof Coin) {}
Generics
-We can create class with not yet known type during the creation of
class for example Arraylists use generics to know what type of objects it
contains
-We declare said class like this: public class A<T>, later the <T> will
be replaced with the type when
instance is created
Polymorphism
We can reference objects that are “below” superclass in herarchy
In polymorphism we can point down but we cannot point up
Person p = new Graduate();#Correct
Staff s = new Person(); #NOT correct
● When can call methods, only if the
reference type has them.
○ Assume Graduate has method
getDegree();
○ We can’t do p.getDegree();
because Person doesn't have it.
● If we call a method and the actual object overrides it, we will perform the
lowest override.
○ Assume Person and Student have function payRent();
○ If we call p.payRent(); we will perform payRent of Student because
that’s the lowes override we found.
Casting:
● We can cast objects from superclass references to subclass reference
type, this lets us call methods of the subclass.
○ ((Graduate)p).getDegree(); is correct.
● When casting we dont know the type of object before running the code,
so we might cast to things we are not allowed to.
○ (Employee)P will not tell us there is a problem when we write it
because Employee inherits from Person, but when running, p is not actually
an Employee, so we will get an exception.
● Prevent this by checking the type before casting with:
○ if(p instanceof Employee)
Final modifier
Processing timer
events
javax.swing.Timer object
generate timer “events” at
fixed intervals
When a timer event occurs, the Timer object needs to notify some object
called an event listener;
The class of the event-listener object must implement the ActionListener
interface
To create a listener you write this
containers
Grid layout – it will create a grid, the rows and columns are defined
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3));
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(button4);
...
Components in swing
JLabel – creates a label
Can display text and images.
JLabel label = new JLabel(“TEXT”);
label.setText(“New Text”);
label.getText();
label.setForeground(Color.GREEN);
JButton – creates a button
Buttons trigger events when they are clicked.
JButton button = new JButton(“Text on button”);
button.addActionListener(new MyActionListener);
button.setText(“new text”);
button.setEnabled(<boolean>); #makes it so the button is/isn’t active
JTextField
Provides the user an area to type text.
JTextField field= new JTextField(“Initial text”);
field.getText(); #returns the current text in the text area
field.addActionListener(new MyActionListener()); #listener triggers
when textField is clicked not when text changes
JRadioButton – creates a radio button
Use if you want to select only one choice at a time.
JRadioButton smallButton = new JRadioButton(“Small”); #make a new
radio button
JRadioButton largeButton = new JRadioButton(“Large”);
ButtonGroup group = new ButtonGroup(); #make a new button group.
You can only
check one button from the group.
group.add(smallButton); #add the button to the group
group.add(largeButton); #Add the radio button to the group
if (largeButton.isSelected()) {} #check if the button is clicked
Events
Java swing has some pre-made event listener interfaces which you can
implement in a class to make your own listeners.
MouseListener: Used to get information from the mouse
import java.awt.event.*;
class MouseSpy implements MouseListener{
public void mousePressed(MouseEvent event){ #when the mouse is
presses on a component
System.out.println("Pressed! x=" + event.getX() + " y=" +
event.getY());
}
public void mouseReleased(MouseEvent event){} #when the mouse
is released on a component
public void mouseClicked(MouseEvent event){} #when the mouse is
clicked
public void mouseEntered(MouseEvent event){}#when the mouse
enters a component
public void mouseExited(MouseEvent event){} #when the mouse
exits a component
}
To use a MouseListener you need to add it to the frame:
MouseSpy listener = new MouseSpy();
frame.addMouseListener(listener);
ActionListener: Action listeners are used to to listen for events from sources.
To create a listener, create a class implementing ActionListener, and
override the actionPerformed method:
public class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//Do something
}
}
To use it, you need to add it to the source:
JButton b = new JButton (“Click me please”);
MyListener listener = new MyListener();
b.addActionListener(listener);
Drawing shapes
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;