190 likes | 516 Views
Inheritance & OOP Concepts. AP CS Spring 2012. Agenda . Abstract Classes Polymorphism Interfaces Comparable<T> Interface. More on the IS-A relationship. If we have the classes: public class Animal { …. } public class Dog extends Animal { …} public class Spaniel extends Dog {…}.
E N D
Inheritance & OOP Concepts AP CS Spring 2012
Agenda • Abstract Classes • Polymorphism • Interfaces • Comparable<T> Interface
More on the IS-A relationship If we have the classes: public class Animal { …. } public class Dog extends Animal { …} public class Spaniel extends Dog {…} These are legal declarations: Spaniel s = new Spaniel (…); Dog d = new Spaniel (… ); //This is possible because Spaniel is a Dog Animal a = new Spaniel (….); // This is possible because Spaniel is an Animal However, if you have public class Horse extends Animal { …} You can NOT say Horse h = new Spaniel(…);
Abstract Classes • Classes close to the top of the hierarchy are more abstract – the properties and methods of their objects are more general • Classes down the hierarchy are more specific Solid Sphere Cube Pyramid
Abstract Class Declarations Public abstract class Solid { public abstract double getVolume(); } Vocab Alert! A class in which all the methods are defined is called concrete Abstract methods do not have any code. Every Solid object has a method that returns its volume but must be overwritten in the subclasses You can not instantiate an abstract class…. Solid s1 = new Sphere(radius); Solid s2 = new Cube(side); No constructor for Solid
Exam Practice public abstract class Account { public Account () { … } } public class BankAcct extends Account { private double balance; public BankAcct(double amount) { super(); balance = amount; } } public class CheckAcct extends Account { private String customerName; public CheckAcct(String n, double amt) { <Missing statements> } } • Which of the following is an acceptable replacement for <Missing Statements>? • Balance = amt; customerName = n; • super(amt); customerName = n; C. super(name, amt);
Exam Practice public abstract class Account { public Account () { … } } public class BankAcct extends Account { private double balance; public BankAcct(double amount) { super(); balance = amount; } } public class CheckAcct extends Account { private String customerName; public CheckAcct(String n, double amt) { <Missing statements> } } Which of the following declarations are valid? • Account acct = new BankAcct(10.00); • CheckAcct c = new BankAcct(10.00); • BankAcct acct = new CheckAcct(“amy”, 10.00);
Polymorphism • Definition: A mechanism that ensures that the correct method is called for an object disguised as a more generic type Recall the solids class from a few slides ago…. If we have: Solid[] solids = {new Sphere(100), new Cube(100)}; The method call double volume = solids[0].getVolume(); The compiler does not know whether solids[0] is a Sphere or Cube. The decision which getVolume function to call is made at run-time. This is called dynamic method binding – each object has a pointer to at table of entry points for its methods… so the correct method is called on an object.
Polymorphism • Polymorphism is implemented in the language; all you have to do is understand it and use it correctly! • Another example of polymorphism Assume you have a list of many types of solids named solids for(Solid s: solids) totalVolume += s.getVolume(); How to know which getVolume method to call? Polymorphism makes sure the correct method is called for each object
Polymorphism and method arguments Remember: Object is the ultimate superclass! public void print(Object x) { if(x != null) print(x.toString()); else print(“<null>”); } Which toString method is called??? Polymorphism ensures the correct toString method is called for all Object subclasses
Exam Practice public class Person { private String name; Person(String nm) { name = nm; } public String getName() { return name; } public String toString() { return getName(); } } public class OldLady extends Person { private int age; public OldLady(String nm, intyrs) { super(nm); age = yrs; } public String getName() { return “Mrs. “ + super.getName(); } public intgetAge() { return yrs;} } What is the output of the following statements? Person p = new OldLady(“Robinson”, 92); System.out.print(p + “, “ + ((OldLady)p).getAge()); Can use print method from previous slide
Interfaces • An interface is even more abstract than an abstract class. • An interface has no constructors or instance variables and no code at all – just headings for methods! • All its methods are public and abstract public interface Fillable { void fill (int x); intgetCurrentAmount() ; intgetMaximumCapacity(); }
A class “implements” an interface by supplying code for all the interface methods. public class Car implements Fillable { … public void fill(int gallons) {fuelAmount += gallons; } public intgetCurrentAmount() { return fuelAmount; } public intgetMaximumCapacity() { return fuelTankCapacity; } } For a concrete class to implement an interface, it must define all the methods required by the interface. It must also explicitly state that it implements the interface. A class that claims it implements an interface but does not define some of the interface methods must be declared abstract.
Interfaces A class can extend only one class, but it can implement several interfaces. public class Car extends Vehicle implements Fillable, Sellable { . . . } Each interface adds a secondary data type to the objects of the class that implements it. If a class C implements interface I, objects of C can be disguised as type I objects and polymorphism applies. If class C implements interface I, all subclasses of C automatically implement I.
The Comparable<T> interface • The library interface java.lang.Comparable<T> is widely used for designating objects that can be compared in some way. • Objects are compared to: • Order a list • Performa a binary search • Implement certain data structures • Objects that implement Comparable are said to have a natural ordering defined.
Comparable<T> • Is a generic interface – works with objects of a specific type • The interface specifies only 1 method intcompareTo(T other); Returns positive integer if object is “greater than” the other. Zero if they are “equal” Negative number if object is “less than” the other.
A comparable interface example Public class fraction implements Comparable<Fraction> { private intnum, denom; public intgetNum() { return num;} public intgetDemon() { return denom;} public double doubleValue() { return (double)num/denom; } public intcompareTo(Fraction other) { double x = doubleValue(); double y = other.doubleValue(); if(x < y) return -1; else if (x > y) return 1; else return 0; } } Defines the compareTo method which is in the Comparable interface.