260 likes | 285 Views
Learn to create reusable classes efficiently with detailed explanations and examples on overriding, overloading, abstract classes, and inheritance in Java programming.
E N D
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 • We have already heard of the STO principle
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
Reusable Classes • 4 main reusable object types: • User interface object • Controller object • Application logic object • Storage object
Reusable Classes • Don’t forget other • Not everything fits into these categories, but most will
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
Reusable Classes • Overriding public class MarriedWoman extends Woman{ … public String toString(){ String name = super.toString(); name = name + husbandName; } … }
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){ … }
Reusable Classes • Overloading: public class Person{ public String name; public Person(){ this(“UNKNOWN”); } public (String myName){ name = myName; }
Reusable Classes • Overloading: public class Person{ public String name; public Person(){ this(“UNKNOWN”); } public (String myName){ name = myName; } this calls this constructor
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.
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(); … }
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
Reusable Classes • Questions??
Reusable Classes • What is the purpose of defining multiple constructors?
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.
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 */ ... }
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.
Reusable Classes • Which is the subclass and which is the superclass in the following declaration? class X extends Y { ... }
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.
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();
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.
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.