search
Login | Signup | Support
  • 0
  • ×

    Add as FriendAbstract Classes, Interfaces, Polymorphism

    by: Rogers

    Current Rating : Rate It :

    949

    Views

    Download
     
    1 : AbstractClassesInterfacesPolymorphism 1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech ericson@cc.gatech.edu April 2010
    2 : Advanced-OO 2 Learning Goals Understand at a conceptual and practical level Inheritance Abstract Classes Interfaces Polymorphism
    3 : Advanced-OO 3 Object-Oriented Principles Objects with data (fields) and operations (methods) Usually classes too Inheritance Hierarchy of types Generalization / Specialization Polymorphism Executing the right method based on the type of the object at run-time
    4 : Advanced-OO 4 Abstract Classes Abstract classes are classes that can’t be instantiated. Abstract classes can only be subclassed. Create an abstract class by using the keyword abstract in the class declaration. public abstract class Food Hamburger Coke Food price calories
    5 : Advanced-OO 5 Why use an Abstract Class? Represents an abstract idea (like a Shape or a List) Holds methods common to several related classes Holds attributes common to several related classes Enforce naming convention by abstract methods that must be overridden by children Allows for general algorithms based on abstract methods with customization by children
    6 : Advanced-OO 6 Interfaces Interfaces are a description of behavior. They are a special kind of abstract class that only has public abstract methods and constants. public interface ShapeInterface { public void setShape(int shape); } You don’t have to declare the methods as abstract or public They automatically are
    7 : Advanced-OO 7 Classes Implement Interfaces Classes that implement interfaces must provide the implementations for the methods specified in the interface. Or be declared abstract as well public class ShapeCanvas implements ShapeInterface { public void setShape(int shape) { code to handle set shape } }
    8 : Advanced-OO 8 Why use an Interface? Separates what from who I don’t care who you are I just need a way to talk to you Choose from several implementers A class can implement many interfaces but inherit from only one class like multiple inheritance but easier to use thinner than inheritance
    9 : Advanced-OO 9 Interfaces Versus Inheritance When a class inherits from a parent class it inherits all the object attributes and methods. With inheritance it inherits the structure and behavior of the parent class. With an interface it inherits only the method names and parameter lists. A class can inherit from only one parent class public class Person extends Object A class can implement more than one interface. public class ShapeCanvas implements Interface1,Interface2,…
    10 : Advanced-OO 10 Comparable Interface How would you compare any two objects? And decide if one is less than, equal too, or greater than the other It would depend on the Class of the objects being compared For String objects compare the letters in the string Implement the Comparable interface public int compareTo(Object object)
    11 : Advanced-OO 11 Comparable Exercise How would you compare two Person objects? Implement the Comparable interface public class Person implements Comparable Add a compareTo method public int compareTo(Person comparePerson) Compare the last names first If they are equal compare the first names The String class implements Comparable so you can use the results of comparing the last name and first name
    12 : Advanced-OO 12 Collections - java.util Used to hold objects Uses wrapper classes to hold primitive values int numItems = 3; Integer numItemsInt = new Integer(numItems); Three basic types List - ordered list of objects Can have duplicate objects Set - group of objects without an order No duplicate objects allowed Map - map of keys to objects
    13 : Advanced-OO 13 List and Set Interfaces and Classes <> Collection <> List <> Set <> SortedSet ArrayList Vector LinkedList TreeSet HashSet
    14 : Advanced-OO 14 Collection Methods Add an object to a collection boolean add(Object object); // optional Remove an object from a collection boolean remove(Object object); //optional See if the collection has the object in it boolean contains(Object object); Add all objects in another collection boolean addAll(Collection collection); // optional Get the intersection of two collections boolean retainAll(Collection collection); // optional Empty a collection Void clear();
    15 : Advanced-OO 15 Use Interface Name as Type Declare the type of the collection variable to be one of the main interface types List Set SortedSet Map SortedMap This allows you to change the implementation without changing much code Map addressMap = new HashMap(); Map addressMap = new Hashtable();
    :

    Presentation Tags

    Copyright © 2019 www.slideworld.com. All rights reserved.