Reusable Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Reusable Classes

Description:

Suppose Truck and Motorcycle are subclasses of Vehicle. ... Motorcycle m2 = new Truck(); The declarations for t, m1, and m2 are invalid. ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 26
Provided by: mccl8
Category:
Tags: classes | reusable

less

Transcript and Presenter's Notes

Title: Reusable Classes


1
  • Reusable Classes

2
Reusable Classes
  • Motivation Write less code!

3
Reusable Classes
  • A reusable class is one that can be used in
    multiple programs
  • Not all classes should be reusable
  • Decide before you design it which category it
    fits into

4
Reusable Classes
  • We have already heard of the STO principle

5
Reusable Classes
  • We have already heard of the STO principle
  • Design a class that implements a single
    well-defined task.
  • Do not overburden the class with multiple tasks

6
Reusable Classes
  • 4 main reusable object types
  • User interface object
  • Controller object
  • Application logic object
  • Storage object

7
Reusable Classes
  • Dont forget other
  • Not everything fits into these categories, but
    most will

8
Reusable Classes
  • Overriding and overloaded methods
  • Overriding redefining a method previously
    defined in the extends hierarchy
  • Ex. toString( )
  • Overloading defining multiple methods with the
    same name and purpose (different parameters)
  • Ex. - multiple constructors

9
Reusable Classes
  • Overriding
  • public class MarriedWoman extends Woman
  • public String toString()
  • String name super.toString()
  • name name husbandName

10
Reusable Classes
  • Overloading
  • A method signature is determined by the method
    name and number and data type of parameters
  • If methods have the same name but different
    signatures, they are overloaded
  • println() is a good example
  • public void drawCircle(int x, int y, int radius)
  • public void drawCircle(Circle c)

11
Reusable Classes
  • Overloading
  • public class Person
  • public String name
  • public Person()
  • this(UNKNOWN)
  • public (String myName)
  • name myName

12
Reusable Classes
  • Overloading
  • public class Person
  • public String name
  • public Person()
  • this(UNKNOWN)
  • public (String myName)
  • name myName

this calls this constructor
13
Reusable Classes
  • This gives users of your class more flexibility
    in how they call the methods.
  • It makes your classes more easily usable
  • Hence, more reusable.
  • Review
  • Inhertance can help you make a number of objects
    similar in nature from a base class
  • You can override methods from the base class as
    needed in the sub classes
  • Within every class you can overload methods to
    make your class easier to use.

14
Reusable Classes
  • Abstract Classes can help define inheritance
  • Abstract classes are defined to show general
    behavior that must be implemented by sub classes
  • Abstract classes are not instantiable
  • abstract class Student
  • abstract public void computeGrade()

15
Reusable Classes
  • abstract class Student
  • abstract public void computeGrade()
  • Abstract methods must be implemented by a sub
    class
  • This can be very helpful in defining
    functionality
  • Not all methods in an abstract class are abstract
  • More on lesson 8

16
Reusable Classes
  • Questions??

17
Reusable Classes
  • What is the purpose of defining multiple
    constructors?

18
Reusable Classes
  • What is the purpose of defining multiple
    constructors?
  • To increase the ease of use. By having multiple
    constructors, the programmer will have a
    flexibility in creating the instances.

19
Reusable Classes
  • Which of the following method declarations have
    the same signature?
  • public void one ( int x, int y) / 1 /
  • ...
  • private void one ( int x, int y) / 2 /
  • ...
  • public int one ( int x, int y) / 3 /
  • ...
  • private void one ( int x, float y) / 4 /
  • ...

20
Reusable Classes
  • Which of the following method declarations have
    the same signature?
  • public void one ( int x, int y) / 1 /
  • ...
  • private void one ( int x, int y) / 2 /
  • ...
  • public int one ( int x, int y) / 3 /
  • ...
  • private void one ( int x, float y) / 4 /
  • ...

The method signature does not include the access
modifier and the return type, so the methods 1,
2, and 3 have the same signature. Method 4 has a
different signature because the data type of the
second parameter is float.
21
Reusable Classes
  • Which is the subclass and which is the superclass
    in the following declaration?
  • class X extends Y ...

22
Reusable Classes
  • Which is the subclass and which is the superclass
    in the following declaration?
  • class X extends Y ...
  • X is the subclass, and Y is the superclass.

23
Reusable Classes
  • Suppose Truck and Motorcycle are subclasses of
    Vehicle. Which of the following declarations are
    invalid?
  • Truck t new Vehicle()
  • Vehicle v new Truck()
  • Motorcycle m1 new Vehicle()
  • Motorcycle m2 new Truck()

24
Reusable Classes
  • Suppose Truck and Motorcycle are subclasses of
    Vehicle. Which of the following declarations are
    invalid?
  • Truck t new Vehicle()
  • Vehicle v new Truck()
  • Motorcycle m1 new Vehicle()
  • Motorcycle m2 new Truck()
  • The declarations for t, m1, and m2 are invalid.

25
Reusable Classes
  • Suppose Truck and Motorcycle are subclasses of
    Vehicle. Which of the following declarations are
    invalid?
  • Truck t new Vehicle()
  • Vehicle v new Truck()
  • Motorcycle m1 new Vehicle()
  • Motorcycle m2 new Truck()
  • The declarations for t, m1, and m2 are invalid.
Write a Comment
User Comments (0)
About PowerShow.com