In this tutorial I talk about the fundamental inheritance concept, I explain and compare abstract classes with interfaces, and touch a little bit to the concept of Graphical User Interfaces in Java. Also I provide an example game project called Puzzler for you to practice on.
Check out rest of the Tutorials: https://berksoysal.blogspot.com/2016/06/java-se-tutorials-basics-exercises.html
2. Inheritance is one of the most important
features of Object-Oriented Programming
(OOP).
Inheritance allows a class to get 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.
2016 Winter
3. Every Class extends the Object Class
The Object class, in the java.lang package, sits at the top of the class hierarchy tree.
Every class is a descendant, direct or indirect, of the Object class.
Every class you use or write inherits the instance methods of Object class.
You need not use any of these methods, but, if you choose to do so,
you may need to override them with code that is specific to your class.
Some methods inherited from Object class are shown below:
•protected Object clone() throws CloneNotSupportedException
Creates and returns a copy of this object.
•public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
•protected void finalize() throws Throwable
Called by the garbage collector on an object when garbage
collection determines that there are no more references to the object
•public String toString()
Returns a string representation of the object.
2016 Winter
4. To reduce the complexity and simplify the language,
multiple inheritance is NOT supported in Java.
2016 Winter
6. • Any class containing an abstract method is
an abstract class
• You must declare the class with the
keyword abstract:
abstract class MyClass {...}
• An abstract class is incomplete
– It has “missing” method bodies
• You cannot instantiate (create a new
instance of) an abstract class.
2016 Winter
7. 2016 Winter
An interface is a way to describe what classes should
do, without specifying how they should do it. It’s not a class
but a set of requirements for classes that want to conform
to the interface, for example;
public interface Comparable {
int compareTo(Object otherObject);
}
This requires that any class implementing the Comparable
interface contains a compareTo method, and this method must
take an Object parameter and return an integer.
Folder Lab4 Lab4 Examples Interface
8. • Why bother introducing two concepts: abstract class and interface?
abstract class Comparable {
public abstract int compareTo (Object otherObject);
}
public class Employee extends Comparable {
pulibc int compareTo(Object otherObject) { . . . }
}
public interface Comparable {
int compareTo (Object otherObject)
}
public class Employee implements Comparable {
public int compareTo (Object otherObject) { . . . }
}
• A class can only extend a single abstract class, but it can implement as
many interfaces as it wants
• An abstract class can have a partial implementation, protected parts, static
methods and so on, while interfaces are limited to public constants and
public methods with no implementation.
2016 Winter
10. An option pane is a simple dialog box for graphical
input/output
• Advantages:
– simple
– flexible (in some ways)
– looks better than printing out to the console
• Disadvantages:
– created with static methods;
not very object-oriented
– not very powerful (just simple dialog boxes)
2016 Winter
11. • public static void showMessageDialog(
Component parent, Object message)
Displays a message on a dialog
with an OK button.
• public static int showConfirmDialog(
Component parent, Object message)
Displays a message and list of
choices Yes, No, Cancel
• public static String showInputDialog(
Component parent, Object message)
Displays a message and text
field for input, and returns the
value entered as a String.
2016 Winter
12. • showMessageDialog is analogous to
System.out.println for displaying a simple message
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null,
"How's the weather?");
JOptionPane.showMessageDialog(null,
"Second message");
}
}
2016 Winter Folder Lab4 Lab4 Examples JOptionPane
13. • showConfirmDialog analogous to a System.out.print
that prints a question, then reading an input value from the
user (can only be one of the provided choices)
import javax.swing.*;
public class Main {
public static void main(String[] args) {
int choice = JOptionPane.showConfirmDialog(null,
"Erase your hard disk?");
if (choice == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Disk erased!");
} else {
JOptionPane.showMessageDialog(null, "Cancelled.");
}
}
}
2016 Winter Folder Lab4 Lab4 Examples JOptionPane
14. • showInputDialog is analogous to a
System.out.print that prints a question, then
reading an input value from the user (can be any value)
import javax.swing.*;
public class Main {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null,
"What's yer name, pardner?");
JOptionPane.showMessageDialog(null, "Yeehaw, " + name);
}
}
2016 Winter Folder Lab4 Lab4 Examples JOptionPane
15. Swing is a GUI widget toolkit for Java. It is part of
Oracle's Java Foundation Classes (JFC) – an API for
providing a graphical user interface (GUI) for Java
programs.
Swing was developed to provide a more
sophisticated set of GUI components than the earlier
Abstract Window Toolkit (AWT).
2016 Winter
16. JFrame: a heavy-weight container used as the top-level window.
JPanel: a light-weight container used to organize GUI components.
2016 Winter
18. 2016 Winter
• The game Puzzler consists of a 7 × 7 board.
Initially, all the cells are filled with marbles from
one to five available colors.
• When the user clicks on a cell for the first time,
the cell and all the adjacent cells of the same
color are selected. The marbles are gray to
indicate this state of the game.
• When the user clicks a second time on a
selected cell, all the selected cells vanish.
Marbles fall from top to bottom, and left to
right, in order to fill the empty spaces.
• To win the game, the user must make all the
marbles vanish.
Click on a Yellow marble
Click again !
Folder Lab4 Lab4 Examples Puzzle
19. You can clearly see the important role of inheritance and interface for
this game. Each of the three classes is derived from an existing class.
2016 Winter