DOC PREVIEW
UMBC CMSC 341 - Inheritance and the Collection classes

This preview shows page 1-2-3-21-22-23-42-43-44 out of 44 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 341Inheritance in JavaPolymorphismPolymorphism (cont.)Virtual Method InvocationWhat is inherited by the subclass?Constructors and InheritanceThe super ReferenceSuper Class ExampleSubclass ExamplePolymorphism in ActionAbstract Classes and MethodsSubclass of Abstract ClassMore about Abstract ClassesMultiple Inheritance in JavaInterfacesInterface ExampleInterfaces (cont.)Inheritance ProgressionThe Collections FrameworkThe Collections Framework (cont.)The Arrays classNatural OrderThe Comparator InterfaceThe Comparable InterfaceComparable ExampleSort ExampleCollectionsThe Collection InterfaceList and Set InterfacesInterface HierarchyCollection ExampleThread-safetyGenericsCollection <E> InterfaceGeneric Cell ExampleDont’s of Generic ProgrammingDo’s of Generic ProgrammingBounding the TypeBounding Type ParametersMore BoundingGeneric SortingGeneric Sorting (cont.)Slide 44CMSC 341Inheritance and the Collection classes8/03/2007UMBC CMSC 341 Java 32Inheritance in JavaInheritance is implemented using the keyword extends.public class Employee extends Person{//Class definition goes here – only the//implementation for the specialized behavior}A class may only inherit from only one superclass.If a class is not derived from a super class then it is derived from java.lang.Object. The following two class declarations are equivalent:public class Person {…}public class Person extends Object {…}8/03/2007UMBC CMSC 341 Java 33PolymorphismIf Employee is a class that extends Person, an Employee “is-a” Person and polymorphism can occur.Person [] p = new Person[2];p[0] = new Employee();p[1] = new Person();Creates an array of Person references8/03/2007UMBC CMSC 341 Java 34Polymorphism (cont.)However, a Person is not necessarily an Employee. The following will generate a compile-time error.Employee e = new Person();Like C++, polymorphism requires general class on left of assignment operator, and specialized class on right.Casting allows you to make such an assignment provided you are confident that it is ok.public void convertToPerson(Object obj){Person p = (Person) obj;}8/03/2007UMBC CMSC 341 Java 35Virtual Method InvocationIn Java, virtual method invocation is automatic. At runtime, the JVM determines the actual type of object a reference points to. Then, theJVM selects the correct overridden method for it. Supposing the Employee class overrides the toString method inherited from the Person class, then the toString method of the derived class, Employee, is invoked even though the reference is a Person reference.Person p = new Employee;p.toString();Invokes the toString method of the Employee class8/03/2007UMBC CMSC 341 Java 36What is inherited by the subclass?All fields are inherited. Giving fields in super classes protected access allows methods of subclasses to reference the fields.All methods are inherited except for constructors.Inherited methods may be overloaded or overridden.8/03/2007UMBC CMSC 341 Java 37Constructors and InheritanceThe superclass constructors are always called by the constructors of the subclasses, either implicitly or explicitly.To explicitly call the superclass constructor, in the first line of the subclass constructor make a call to the super method passing the appropriate parameters for the desired constructor.8/03/2007UMBC CMSC 341 Java 38The super ReferenceAll overridden methods in a subclass also contain a reference to their corresponding methods in the superclass named super.The following code contains the use of the super reference to call the super class constructor and to use the implementation of the toString method of the superclass.Notice it also contains several uses of the this reference.8/03/2007UMBC CMSC 341 Java 39Super Class Examplepublic class Person {protected String name;private int age;public Person(String name, int age){this.name = name;this.age = age;}public Person(String name){this(name, 0);}public String toString(){ return name;}public int getAge(){ return age;}public void setAge(int age){ this.age = age;}public void setName(String name){ this.name = name;}}Call to other constructor8/03/2007UMBC CMSC 341 Java 310Subclass Examplepublic class Employee extends Person{private double salary;public Employee(String name, int age, double sal){super(name,age);salary = sal;}public Employee(String name, double salary){this(name,18, salary);}public double getSalary(){ return salary; }public void setSalary(double sal){ salary = sal; }public String toString(){return super.toString() + “ has a salary of “ + salary;}}Call to constructor aboveCall to superclass constructorCall to superclass toString method8/03/2007UMBC CMSC 341 Java 311Polymorphism in Actionpublic class Test{public static void main(String []args){Person [] people = new Person[3];people[0] = new Person("Sam");people[1] = new Employee("Jane",45345.63);for(Person someone:people)System.out.println(someone);}}SamJane has a salary of 45345.63nullprintln invokes the toString method of the object the reference is pointing to, as if it were a pointer in C++ and the toString method were virtual.Output8/03/2007UMBC CMSC 341 Java 312Abstract Classes and MethodsJava also has abstract classes and methods like C++. If a class has an abstract method, then it must be declared abstract.public abstract class Node{String name;public abstract void type();public String toString(){ return name;}public Node(String name){this.name = name;}}Abstract methods have no implementation.8/03/2007UMBC CMSC 341 Java 313Subclass of Abstract ClassSubclass of an abstract class must provide implementation for ALL the abstract methods or it must be declared abstract as well.public class NumberNode extends Node{int number;public void type(){System.out.println(“Number node”);}public NumberNode(String name, int num){super(name);number = num;}public String toString(){return super.toString() + “ “ + number;}}8/03/2007UMBC CMSC 341 Java 314More about Abstract ClassesLike C++, abstract classes can not be instantiated.// OK because n is only a reference.Node n;// OK because NumberNode is concrete.Node n = new NumberNode(“Penta”, 5);// Not OK. Gives compile error.Node n = new Node(“Name”);8/03/2007UMBC CMSC 341 Java 315Multiple Inheritance in JavaThere are always cases where a class appears to have characteristics of more than one class. Consider the following hierarchy.PersonTAStudent EmployeeTA has characteristics of both a Student and an


View Full Document

UMBC CMSC 341 - Inheritance and the Collection classes

Download Inheritance and the Collection classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inheritance and the Collection classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inheritance and the Collection classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?