game plan
play

Game plan Inheritance, Subclassing, Polymorphism Inheritance and - PDF document

Game plan Inheritance, Subclassing, Polymorphism Inheritance and Polymorphism Wednesday: Basic concepts Today: How its done in Java Implementation But first But first Questions from last time? CS2 Review Sessions


  1. Game plan • Inheritance, Subclassing, Polymorphism Inheritance and Polymorphism – Wednesday: Basic concepts – Today: How it’s done in Java Implementation But first… But first • Questions from last time? • CS2 Review Sessions – Tuesdays 8-10pm – 12-1125 • Check your e-mail. – Review of course material – Clarification of projects – Exam preparation Inheritance and Java Inheritance and Java • Class hierarchy for theatre app • To define a class as a subclass, in Java we say that the subclass extends the Performer superclass isA isA Actor Musician Guitarist Pianist Drummer

  2. Inheritance and Java Inheritance, Java, and Constructors public class Performer { • When in a subclass, to call the constructor of your … superclass use the super() function. } public class Musician extends Performer { … • super() must be called if all of the constructors } for a superclass have arguments. public class Guitarist extends Musician { … • super(), if used, must be the first statement in } the constructor of the subclass. Inheritance, Java, and Constructors Inheritance, Java, and Constructors public class Performer { public class Musician extends Performer { private String myName; private Instrument myInstrument; public Musician (String name, Instrument I) // constructor { public Performer (String name){…} super(name); } myInstrument = I; } public class Musician extends Performer { } private Instrument myInstrument; public Musician (String name, Instrument I) public class Guitarist extends Musician { { public Guitarist(String name) super(name); { myInstrument = I; super (name, new Guitar()); } } } } The Java Object class toString() example public class Musician extends Performer{ • All classes in Java, whether explicitly stated … public String toString() or not, are subclasses of the java Object { return “I am a musician”; class (defined in package java.lang ) } } • String Object.toString() method public class Drummer extends Musician { … public String toString() { return “I am a drummer”; } }

  3. Accessing superclass methods using super toString example Object P = new Performer (“foo”); • You can access any public or protected Object M = new Musician (“fred”, new Guitar()); Object D = new Drummer (“keith”); member of your superclass explicitly using Object G = new Guitarist (“barney”); super System.out.println (M.toString()); System.out.println (D.toString()); public class Drummer extends Musician { System.out.println (G.toString()); … System.out.println (P.toString()); public String toString() { return super.toString() + “ that plays the drums”; Output: } I am a musician } I am a drummer I am a musician Performer@77d163 Declaring abstract classes Declaring abstract methods • To declare a class as abstract, use the • To declare a method to be abstract, add the abstract keyword when defining the abstract keyword to the method. class: • The abstract method in the superclass will have no body defined for it. public abstract class Performer { … public abstract class Performer { } … public abstract double calculatePay(); } Declaring abstract methods Why use abstract classes • If a class has an abstract class, it must be • Abstract classes declared as abstract – provide a set of methods that a subclass must implement public class Performer { … – Subclass Implementations may be vastly different but set of methods are the same public abstract double calculatePay(); } • Will fail at compile time

  4. Abstract classes Declaring abstract methods • FileSystem Object • Abstract classes – A class that has abstract method must be FileSystem Abstract char read() declared as abstract – However, you can declare an class that does not have abstract methods to be abstract. PCFileSystem MacFileSystem SunFileSystem Abstract classes Abstract classes public abstract class Instrument { • Why would one do this? private double rentalCost; – Design purposes – If, in your logical design of protected Instrument (double cost) your app, a class is so general that it doesn’t { make sense to instantiate an object of the class rentalCost = cost; } directly public double getWeeklyRental () – Reserve the right to add abstract methods later. { return rentalCost; } } More on Polymorphism More on Polymorphism public abstract class Performer { • When you declare a variable with type of a … superclass, it can hold an object of type public abstract double calculatePay(); } superclass or any class inherited from superclass. public class Musician extends Performer { … public double calculatePay() { …} • When sending messages to that object via a method call, you must use the method set public Instrument getInstrument() { … } } available by the superclass Performer P = new Musician(“fred”); double pay = P.calculatePay(); // this call is okay Instrument I = P.getInstrument(); // this call is bad

  5. More on Polymorphism Multiple Inheritance • Polymorphism works on method arguments • In some languages (like C++), it is possible as well. for class to inherit from more than one class public class Payroll { (I.e. have more than one superclass). … public void addPerformer(Performer P) { ... } • This is called multiple inheritance. public static void main (String args[]) { • Java does not support multiple inheritance. Payroll P = new Payroll(); • Instead, Java provides a different Guitarist G = new Guitarist (“fred”); P.addPerformer (G); mechanism: interfaces. ... } } Interfaces Interfaces • An interface defines a set of methods that • To define an interface: need to be implemented by a class. public interface InterfaceName { • It’s “somewhat” like an abstract class that // constants defined by the interface contain only constants and method public static int constant1 = 12; public static int constant2 = 20; definitions where all of the methods are abstract. // set of methods that need to be defined by // classes implementing this interface • A class does not extend an interface, public void method1(); public float method2 (int arg1); instead, it implements an interface. } Interfaces Interface • When a class has definitions for all methods of an • Example: java.lang.Comparable interface, we declare that class to implement the – Interface for objects that can compare itself interface: with other objects public class Foo implements InterfaceName { … – Used by sorting methods in the java.util } package. • A class can have only one superclass, however, it public interface Comparable { can implement as many interfaces as it likes. public int compareTo(Object O); } public class Foo2 extends Foobar implements InterfaceName2, IntefaceName3 { … }

  6. Interface Summary • Let’s make our Performer implement • extends Comparable • super() public class Performer implements Comparable { • abstract private String myName; … • super again public int compareTo(Object O) { • interface / implements // Objects must be of same class if not // exception is thrown Performer P = (Performer)O; • Questions? // compare by name return myName.compareTo (P.myName) }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend