1 / 18

Inheritance & OOP Concepts

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 {…}.

shaun
Download Presentation

Inheritance & OOP Concepts

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. Inheritance & OOP Concepts AP CS Spring 2012

  2. Agenda • Abstract Classes • Polymorphism • Interfaces • Comparable<T> Interface

  3. 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(…);

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

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

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

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

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

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

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

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

  12. 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(); }

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

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

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

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

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

More Related