1 / 22

More on Inheritance

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

mabelp
Download Presentation

More on Inheritance

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. More on Inheritance Chapter 11 Continued

  2. Reminders • Overloading – different signatures • Overriding – same signatures • Preventing overriding – use final • public final void message()

  3. 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

  4. Special Note • IF you do not specify package visibility the default package visibility is directory.

  5. Protected • Protected is between public and private • Next 2 slides show examples of use.

  6. ** 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); }}

  7. /** 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; }}

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

  9. Access Specifiers Summarized

  10. General UML diagram layout

  11. Access Specification in UML diagrams

  12. 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

  13. 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

  14. Polymorphism • Skipping this topic for now

  15. 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

  16. 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

  17. 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)

  18. 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

  19. 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

  20. 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

  21. 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

  22. Interfaces in UML GradedActivity A dashed line with an arrow indicates implementation of an interface. FinalExam3 Relatable

More Related