Java 4: Class Organization and Abstraction
This week you will not need to submit your solutions.
- Open the IntelliJ IDE
- Choose
Check out from Version Control -> git
in the welcome window, or chooseVCS -> Check out from Version Control -> git
- Enter the git URL:
https://github.com/cs2113f18/template-j-4
- Keep hitting “Next” or “Yes” until it opens a new window with the project you cloned.
- Note: Always use one of the two options listed above. Do NOT try to do File->New… It won’t work correctly!
- The full slides are avaiable here - This includes intro and conclusion slides not shown below!
- You might need the Violet UML editor
Objectives
- To understand how abstract classes promote good software design
- To learn how interfaces allow for multiple inheritance
1. Hierarchies and Abstraction
Exercise: Modify the program in the
shapes
package to:
- Create an ArrayList and put a Circle, Rectangle, and Square into it
- Draw the filled version of each shape to the screen with a for loop
- Get each shape out of the list and then call its drawFilled()
- Add some more shapes to create a beautiful work of art
About abstract
classes:
- Any class with an
abstract
method must be declaredabstract
. - A signature is all that’s provided for an
abstract
method. - Data cannot be
abstract
, but abstract classes can define data members. - Any subclass of an
abstract
class must implement allabstract
methods if it wants to be instantiated. - A subclass that leaves some inherited abstract methods un-implemented is itself
abstract
.
Note:
- Casting an object type might get rid of compiler errors, but you need to be sure at runtime the object will actually be able to correctly cast to the designated type. You can always safely cast up the inheritance tree, but not necessarily the other way down!
- An incorrect cast will cause a runtime error.
- The
instanceof
operator tells whether an object is of the specified type. Keep in mind that a child class will match its parent (the child meets the “is a” criteria of the parent, but not the other way around).- This concept of allowing your code to ask questions about itself is called introspection.
2. A Zoo of Interfaces
Exercise: Write a set of classes in an
animals
package that demonstrates inheritance and interfaces.
About interfaces
:
- An interface is an “advertisement” of capability, a promise to implement methods defined in the interface.
- Alternatively, you can think of an interface as a purely abstract class.
- (All methods are abstract).
- The idea is, a class can
implement
an interface by implementing all the methods advertised in the interface. - Thus, if LinkedList implements the Enumeration interface, in effect, LinkedList is saying “I can do everything an Enumeration is supposed to”.
- A class can only inherit from one parent (superclass), but can implement any number of interfaces.
- Note: Java does not allow you to inherit from more than one class. (C++ does allow this, a feature called multiple inheritance).
- Interfaces can use the
extends
keyword to support inheritance. The child of an interface inherits all function signatures defined in its parent. - Don’t open this link until told to do so.
3. Sorting with Interfaces
Exercise: Modify the code in the “interfaces” package so that it will sort students by their GPA. To allow a list of Names to be sorted, you must implement the Comparable
interface.
- Add code to implement CompareTo
to Student.java - For added challenge: Use last name and then first name as tie breakers
- String already supports the compareTo() function, so you can use that as a base!
About the Comparable<T>
interface:
- This tells Java that you know how to compare an object of this class against another object of type
T
.- Generally,
T
will be the same class name as the current class, but it could be something different, e.g., a parent.
- Generally,
- To successfully implement the interface you must meet its contract: define a
public int compareTo(T t)
function, whereT
is the type you are comparing against.- Inside the function you should return:
- a negative value to indicate this object should appear earlier in the list than
t
- 0 if the ordering of the two items doesn’t matter
- a positive number to indicate this object should appear later in the list than
t
- a negative value to indicate this object should appear earlier in the list than
- Inside the function you should return:
Summary
- An
abstract
class is a class with at least one abstract method. - An
abstract
class must have the reserved wordabstract
in the class definition. - An
abstract
method is declared with its signature and no body. A semi-colon is required at the end. - An
abstract
class cannot be instantiated. However, it can contain non-abstract static methods that can be called without instantiation, e.g.,
abstract class A {
public static void print()
{
System.out.println ("A");
}
}
public class TestAbstract {
public static void main (String[] argv)
{
A.print();
}
}
- Not all methods of an abstract class need be abstract.
- A subclass of an abstract class inherits all methods of the abstract class.
- Subclasses can themselves be extended and thus, abstract methods can be passed down the chain of inheritance.
- No abstract methods can be present for a class to be instantiated.
- The compiler can detect this error in advance.
- Data members cannot be abstract.
- An abstract class with all its methods abstract is really like an interface.
So, what is the difference between an interface and a pure abstract class?
- An abstract class is
extended
, but an interface isimplemented
. - Since a class can have only one superclass, it is more convenient to define interface’s.
- Multiple interface’s can be implemented by a single class.
- Only static final variables (constants) can be declared in an interface.
- A public interface must be defined in a file by itself.
- On the other hand, an
abstract
class can define data members in addition to methods, whereas aninterface
can only define methods.