1 / 25

Reusable Classes

Reusable Classes. Reusable Classes. Motivation: Write less code!. 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. Reusable Classes.

fgrant
Download Presentation

Reusable Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • Don’t 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.

More Related