Software Design and Interfaces - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Software Design and Interfaces

Description:

Every class in Java is a subclass. Except the Object Class ... Hearts, Solitaire, and Poker subclasses. inherit the shuffle() method and override ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 17
Provided by: Staf205
Category:

less

Transcript and Presenter's Notes

Title: Software Design and Interfaces


1
Software Design and Interfaces
  • Chapter 12 part 2
  • Dr. James Jiang

Advanced Inheritance Concepts
2
The 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

3
The 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
4
The 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
5
Dog class does not override the equals() method
6
Overriding the equals() method
  • Boolean equals(Dog anotherDog)
  • boolean result
  • if(getName().equals(anotherDog.getName()))
  • result true
  • else
  • result false
  • return result

7
Inheritance and Good Software Design
  • Inheritance makes programming easier!
  • Use existing components as-is OR with slight
    modifications

8
Advantages 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

9
Multiple 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
11
Interfaces
  • 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()
12
WorkingDog 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)

13
Testing the WorkingDog Class
public class DemoWorkingDog public static
void main(String args) WorkingDog
mySheltie new WorkingDog(Simon)
mySheltie.work()
14
Comparing 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
15
When 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()
16
When 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
Write a Comment
User Comments (0)
About PowerShow.com