More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Starting Out with Java: From Control Structures through Objects
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance and Polymorphism- I Math 130 Introduction to Computer Programming Lecture #27 Monday, November 5, 2007.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Inheritance, Polymorphism, and Virtual Functions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance, Polymorphism, and Virtual Functions
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance Chapter 11.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
11-1 Chapter.9 Classes & Objects: Inheritance –What Is Inheritance? –Calling the Superclass Constructor –Overriding Superclass Methods –Protected Members.
AN INTRODUCTION TO INHERITANCE. In your group Define 5 attributes that you would use to describe persons. Define 3 different attributes that can describe.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 11: Inheritance Starting Out with Java: From Control Structures through Data Structures Second.
Chapter Topics Chapter 10 discusses the following main topics:
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Inheritance Starting Out with Java: From Control Structures.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
© 2010 Pearson Addison-Wesley. All rights reserved. AN INTRODUCTION TO INHERITANCE.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 11 Polymorphism. Contents I. Polymorphism 1. Polymorphism 2. Polymorphism and Dynamic Binding 3. The “is-a” Relationship Does Not Work in Reverse.
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
COP 3330 Notes 4/13. Today’s Topics UML Class Diagrams.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Abstract classes and interfaces
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Starting Out with Java: From Control Structures through Objects
Inheritance, Polymorphism, and Virtual Functions
by Tony Gaddis and Godfrey Muganda
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
An introduction to inheritance
Starting Out with Java: From Control Structures through Objects
Abstract classes and interfaces
Interface.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 9: Polymorphism and Inheritance
Inheritance, Polymorphism, and Virtual Functions
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Introducing PA6 – The Last one
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Chapter 8 Class Inheritance and Interfaces
2009 Test Key.
Final and Abstract Classes
Lecture 0311 – Polymorphism
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Presentation transcript:

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 packagepackage

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 = / questions; numericScore = (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 s The value to store in score. */ public void setScore(double s) { score = s; } /** The getScore method returns the The value stored in the score field. */ public double getScore() { return score; } /** The getGrade method returns a letter grade determined from the score 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.

Access Specifiers Summarized Access Modifier Accessible to a subclass inside the same package? Accessible to all other classes inside the same package? default (no modifier) Yes PublicYes ProtectedYes PrivateNo Access Modifier Accessible to a subclass outside the package? Accessible to all other classes outside the package? default (no modifier) No PublicYes ProtectedYesNo PrivateNo

General UML diagram layout Class name goes in this box Fields go in this box Methods are listed here

Access Specification in UML diagrams Class name goes in this box Fields go in this box + indicates public - indicates private # indicates protected Methods are listed here

Specific UML diagrams GradedActivity2 # score : double + setScore(s : double) : void + getScore() : double + getGrade() : char FinalExam2 - numQuestions : int - pointsEach : double - numMissed : int + FinalExam(questions : int, missed : int) : + getPointsEach() : double + getNumMissed() : int - adjustScore() : void

Chains of Inheritance Example See the code in your text and on your CD Code Listings: 11-1 GradedActivity 11-21PassFailActivity 11-22PassFailExam PassFailExamDemo Object PassFailActivity PassFailExam GradedActivity

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 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 Relatable.java FinalExam3.java 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 RelatableFinalExam3 A dashed line with an arrow indicates implementation of an interface.