Title: Software Design and Interfaces
1Software Design and Interfaces
- Chapter 12 part 2
- Dr. James Jiang
Advanced Inheritance Concepts
2The Object Class
- Every class in Java is a subclass
- Except the Object Class
- All classes are extensions of the Object class
- Use or override Object class methods
3The toString() Method
- Converts primitive type to a string
- Using the Object class toString() method provides
unpredictable/unuseful results
public String toString() return(entertainer
, featuring typeOfMusic music. Fee
is fee per event)
OUTPUT Bob, featuring country music. Fee is 600
per event
4The equals() Method
- Object class equals() method requires a single
argument - Returns a boolean if objects are equal
- Only if one is a reference to the other
- To compare objects based on their contents, write
your own equals() method
someObject.equals(someOtherObjectOfTheSameType
5Dog class does not override the equals() method
6Overriding the equals() method
- Boolean equals(Dog anotherDog)
-
- boolean result
- if(getName().equals(anotherDog.getName()))
- result true
- else
- result false
- return result
7Inheritance and Good Software Design
- Inheritance makes programming easier!
- Use existing components as-is OR with slight
modifications
8Advantages of Extendable Superclasses
- Subclass creators save development time
- Subclass creators save testing time
- Superclass code is reliable
- Programmers understand superclass
- Superclass code maintains integrity when subclass
extends the superclass
9Multiple Inheritance
- Multiple inheritance capability to inherit from
more than one class
Employee
Product
Who does super() refer to?
Patent
10 Multiple inheritance is prohibited in Java
11Interfaces
- Alternative to multiple inheritance
- Interface looks like a class
- All methods must be abstract
- All data must be static and final
Any class that implements Working must contain
a work() method
public interface Working public void
work()
12WorkingDog Class
- public class WorkingDog extends Dog implements
Working -
- public WorkingDog(String nm)
-
- super(nm)
-
- public void work()
-
- speak()
- System.out.println(I can herd cows)
-
13Testing the WorkingDog Class
public class DemoWorkingDog public static
void main(String args) WorkingDog
mySheltie new WorkingDog(Simon)
mySheltie.work()
14Comparing Abstract classes and Interfaces
- Cant instantiate concrete objects
- Abstract classes can contain nonabstract methods
- Class can inherit from only one abstract
superclass
- Cant instantiate concrete objects
- All methods within an interface must be abstract
- Class can implement multiple interfaces
INTERFACE
ABSTRACT CLASS
15When to use an abstract class?
- Create an abstract class when you want to provide
data or methods that subclasses can inherit AND
the subclasses will need to override some
specific methods
- Example
- CardGame class
- shuffle()
- deal()
- listRules()
- keepScore()
Hearts, Solitaire, and Poker subclasses inherit
the shuffle() method and override deal(),
listRules(), and keepScore()
16When to use an interface?
- Create an interface when you know what actions
you want to include, but you want every user to
define behavior separately
- Example
- MusicalInstrument class
- playNote()
- outputSound()
-
Piano, Violin, and Drum classes must implement
playNote() and outputSound() methods differently