220 likes | 232 Views
More on Inheritance. Chapter 11 Continued. Reminders. Overloading – different signatures Overriding – same signatures Preventing overriding – use final public final void message(). Access Specifications. public - everyone private – methods of class methods of subclass protected
E N D
More on Inheritance Chapter 11 Continued
Reminders • Overloading – different signatures • Overriding – same signatures • Preventing overriding – use final • public final void message()
Access Specifications • public - everyone • private – • methods of class • methods of subclass • protected • methods of class • methods of subclass • methods of any class in same package
Special Note • IF you do not specify package visibility the default package visibility is directory.
Protected • Protected is between public and private • Next 2 slides show examples of use.
** This class determines the grade for a final exam. The numeric score is rounded up to the next whole number if its fractional part is .5 or greater.*/public class FinalExam2 extends GradedActivity2{ private int numQuestions; // Number of questions private double pointsEach; // Points for each question private int numMissed; // Number of questions missed public FinalExam2(int questions, int missed) { double numericScore; // To hold a numeric score numQuestions = questions; numMissed = missed; pointsEach = 100.0 / questions; numericScore = 100.0 - (missed * pointsEach); setScore(numericScore); adjustScore(); // adjusts the score } public double getPointsEach() { return pointsEach; } public int getNumMissed() { return numMissed; } private void adjustScore() { double fraction;fraction = score - (int) score; score = score + (1.0 - fraction); }}
/** A class that holds a grade for a graded activity.*/public class GradedActivity2{protected double score; // Numeric score /** The setScore method sets the score field. @param s The value to store in score. */ public void setScore(double s) { score = s; } /** The getScore method returns the score. @return The value stored in the score field. */ public double getScore() { return score; } /** The getGrade method returns a letter grade determined from the score field. @return The letter grade. */ public char getGrade() { char letterGrade; if (score >= 90) letterGrade = 'A'; else if (score >= 80) letterGrade = 'B'; else if (score >= 70) letterGrade = 'C'; else if (score >= 60) letterGrade = 'D'; else letterGrade = 'F'; return letterGrade; }}
Access Subtleties • IF you do not provide an access specifier for a class member, the class member is given package access by default. • Example public class Circle { double radius; int centerX, centerY; // methods omitted here } • Any method in the same package as the Circle class may directly access radius, centerX and centerY. • Protected members may be accessed by methods in a subclass even if the subclasses are in a different package.
GradedActivity2 # score : double FinalExam2 + setScore(s : double) : void + getScore() : double + getGrade() : char - numQuestions : int - pointsEach : double - numMissed : int + FinalExam(questions : int, missed : int) : + getPointsEach() : double + getNumMissed() : int - adjustScore() : void Specific UML diagrams
Object GradedActivity PassFailActivity PassFailExam Chains of Inheritance Example • See the code in your text and on your CD Code Listings: 11-1 GradedActivity 11-21PassFailActivity 11-22PassFailExam 11- 23PassFailExamDemo
Polymorphism • Skipping this topic for now
Abstract Methods • An abstract method appears in a superclass • An abstract method has no body, only a header • An abstract method must be overriden in a subclass • Header example: public abstract void setValue(int value); • NOTE: semicolon is required
Abstract Classes • An abstract class can not be instantiated, i.e. You can NOT create an object of an abstract class. • Any class that contains an abstract method is an abstract class. • Abstract classes are extended by other classes (i.e. subclasses) • Header Example: public abstract class Student
Code Examples from text • abstract class Student.java (Code Listing 11-26) • subclass CompSciStudent.java (Code Listing 11-27) • demo program CompSciStudentDemo.java (Code Listing 11-28)
Interfaces • An interface specifies behavior for a class • An interface is a class that has all abstract methods. • An interface can not be instantiated. • All of the methods in an interface must be written elsewhere. • Methods in the interface has no bodies only headers terminated by semicolons. • Example interface header public interface InterfaceName
Interface example /** Relatable interface*/public interface Relatable{ boolean equals(GradedActivity g); boolean isGreater(GradedActivity g); boolean isLess(GradedActivity g);} • See Code Listings 11-29 Relatable.java 11-30 FinalExam3.java 11-31 InterfaceDemo.java
Fields & Methods in an Interface • An interface can contain field declarations. • All fields in an interface are final and static. • You must provide an initialization for each of them. • By convention, their names should be all upper case. • Example interface with fields: public interface Doable { int FIELD1 = 1; int FiELD2 = 2; // method headers go here } • All methods in an interface are public by default
Implementing Multiple Interfaces • Remember: • A class can extend only one superclass. • Java does not have multiple inheritance. • However: • A class my implement multiple interfaces. • When it implements multiple interfaces, it must provide the methods specified by all of them. • Example header for a class that implements multiple interfaces public class MyClass implements InterfaceA, InterfaceB, InterfaceC
Interfaces in UML GradedActivity A dashed line with an arrow indicates implementation of an interface. FinalExam3 Relatable