OOP Reviewer Final Exam
OOP Reviewer Final Exam
Overriding
➢ This is called overriding a method
➢ Method print in Dog overrides method print in Animal
➢ A subclass variable can shadow a superclass variable, but a subclass method can
override a superclass method
How to override a method
➢ Create a method in a subclass having the same signature as a method in a superclass
➢ That is, create a method in a subclass having the same name and the same number and
types of parameters
• Parameter names don’t matter, just their types
➢ Restrictions:
• The return type must be the same
• The overriding method cannot be more private than the method it overrides
More about toString()
➢ It is almost always a good idea to override
➢ public String toString()
➢ to return something “meaningful” about the object
➢ When debugging, it helps to be able to print objects
➢ When you print objects with System.out.print or System.out.println, they automatically call
the objects toString() method
➢ When you concatenate an object with a string, the object’s toString() method is
automatically called
➢ You can explicitly call an object’s toString() method
➢ This is sometimes helpful in writing unit tests; however...
➢ Since toString() is used for printing, it’s something you want to be able to change easily
(without breaking your test methods)
➢ It’s usually better to write a separate method, similar to toString(), to use in your JUnit tests
Calling an overridden method
➢ When your class overrides an inherited method, it basically “hides” the inherited method
➢ Within this class (but not from a different class), you can still call the overridden method, by
prefixing the call with super.
• Example: super.printEverything();
➢ You would most likely do this in order to observe the DRY principle
• The superclass method will do most of the work, but you add to it or adjust its results
• This isn’t a call to a constructor, and can occur anywhere in your class (it doesn’t
have to be first)
Summary
➢ You should overload a method when you want to do essentially the same thing, but with
different parameters
➢ You should override an inherited method if you want to do something slightly different than
in the superclass
• It’s almost always a good idea to override public void toString() -- it’s handy for
debugging, and for many other reasons
• To test your own objects for equality, override public void equals(Object o)
• There are special methods (in java.util.Arrays) that you can use for testing array
equality
➢ You should never intentionally shadow a variable
Inheritance
Inheritance
➢ is one of the features of Object-Oriented Programming (OOPs). Inheritance allows a class
to use the properties and methods of another class. In other words, the derived class
inherits the states and behaviors from the base class. The derived class is also called
subclass and the base class is also known as super-class. The derived class can add its
own additional variables and methods. These additional variable and methods differentiate
the derived class from the base class.
➢ Inheritance is a compile-time mechanism. A super-class can have any number of
subclasses. But a subclass can have only one superclass. This is because Java does not
support multiple inheritance.
➢ Superclass - is also referred to as a supertype, a parent class, or a base class.
➢ Subclass – is a subtype, a child class, an extended class, or a derived class. It inherits
accessible data fields and
➢ The keyword used for inheritance is extends. Syntax:
➢ public class ChildClass extends ParentClass {
➢ // derived class methods extend and possibly override
➢ }
➢ The use of the key word: super
GUI IN JAVA
What is GUI in Java?
➢ GUI (Graphical User Interface) in Java is an easy-to-use visual experience builder for Java
applications. It is mainly made of graphical components like buttons, labels, windows, etc.
through which the user can interact with an application. GUI plays an important role to build
easy interfaces for Java applications.
What is Swing in Java?
➢ Swing in Java is a Graphical User Interface (GUI) toolkit that includes the GUI
components. Swing provides a rich set of widgets and packages to make sophisticated GUI
components for Java applications. Swing is a part of Java Foundation Classes (JFC), which
is an API for Java GUI programing that provide GUI.
➢ The Java Swing library is built on top of the Java Abstract Widget Toolkit (AWT), an older,
platform dependent GUI toolkit. You can use the Java simple GUI programming
components like button, textbox, etc., from the library and do not have to create the
components from scratch.
What is a Container Class?
➢ Container classes are classes that can have other components on it. So for creating a Java
Swing GUI, we need at least one container object. There are 3 types of Java Swing
containers.
• Panel: It is a pure container and is not a window in itself. The sole purpose of a
Panel is to organize the components on to a window.
• Frame: It is a fully functioning window with its title and icons.
• Dialog: It can be thought of like a pop-up window that pops out when a message has
to be displayed. It is not a fully functioning window like the Frame.
▪ JPanel, a part of the Java Swing package, is a container that can store a
group of components. The main task of JPanel is to organize components,
various layouts can be set in JPanel which provide better organization of
components, however, it does not have a title bar
▪ JFrame frame = new JFrame("Simple GUI"); What this line does is create a
new instance of a JFrame object called "frame". You can think of "frame" as
the window for our Java application
▪ JDialog is a part Java swing package. The main purpose of the dialog is to
add components to it. JDialog can be customized according to user need.
JDialog(Window o, String t): creates an empty dialog with a specified window
as its owner and specified title.
Action Events
➢ An action event occurs, whenever an action is performed by the user. Examples: When
the user clicks a button, chooses a menu item, presses Enter in a text field. The result is
that an actionPerformed message is sent to all action listeners that are registered on the
relevant component.
SWING Event Listener Interfaces
➢ Following is the list of commonly used event listeners.
• ActionListener
• ComponentListener
• ItemListener
• KeyListener
• WindowListener
• AdjustmentListener
• ContainerListener
• MouseMotionListener
• FocusListener
Action Listener
➢ An ActionListener can be used by the implements keyword to the class definition. It can
also be used separately from the class by creating a new class that implements it. It should
also be imported to your project
How to Write Action Listener in Java
➢ To write an Action Listener, follow the steps given below:
• Declare an event handler class and specify that the class either implements an
ActionListener interface or extends a class that implements an ActionListener
interface. For example:
public class MyClass implements ActionListener {
➢ The implements keyword is used to implement an interface. The interface keyword is used
to declare a special type of class that only contains abstract methods. To access the
interface methods, the interface must be "implemented" (kinda like inherited) by another
class with the implements keyword (instead of extends).
• Register an instance of the event handler class as a listener on one or more
components. For example:
someComponent.addActionListener(instanceOfMyClass);
• Include code that implements the methods in listener interface. For example:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}
➢ Event listeners represent the interfaces responsible to handle events. Java provides various
Event listener classes, however, only those which are more frequently used will be
discussed. Every method of an event listener method has a single argument as an object
which is the subclass of EventObject class. For example, mouse event listener methods will
accept instance of MouseEvent, where MouseEvent derives from EventObject.
JTable
➢ JTable class is a part of Java Swing Package and is generally used to display or edit two-
dimensional data that is having both rows and columns.
➢ Constructors in JTable:
• JTable(): A table is created with empty cells.
• JTable (int rows, int cols): Creates a table of size rows * cols.
• JTable(Object [][] data, Object []Column): A table is created with the specified name
where []Column defines the column names.
➢ Functions of Table
• addColumn(TableColumn [] column): adds a column at the end of the JTable.
• clearSelection(): Selects all the selected rows and columns.
• editCellAt(int row, int col): edits the intersecting cell of the column number col and
row number row programmatically, if the given indices are valid and the
corresponding cell is editable.
• setValueAt(Object value, int row, int col): Sets the cell value as ‘value’ for the
position row, col in the JTable.
Add Columns
Search/Sort
Search Using Mouse Click
➢ The MouseEvent interface represents events that occur due to the user interacting with a pointing
device (such as a mouse). Common events using this interface include click, dblclick, mouseup,
mousedown.