2Object Oriented Software Development Using Java Chapter 5 CS 602 Java and the WebObject Oriented Software Development Using JavaChapter 5
3Chapter Five Objectives Chapter Five is largely about the use of object-oriented technology in Java, particularly inheritance, polymorphism, overloading, overriding, hiding, subtypes and casting.
4Java’s UniquenessChapter Four gave a basic introduction to the Java language that might mislead you into thinking that programming in Java is much like programming in other computer languages. Like C, C++, Pascal and most other languages, Java has a relatively small set of commands, primitives, structures, and operations, with a simple syntax. Calling it a “language” is misleading. It is nowhere near as complicated as English, with irregular syntax and over 500,000 words.
5The Class LibrariesHowever, Java is unlike most other computer languages in at least one way. Most Java programming involves using predefined functionality in the extensive class library, not writing code from scratch. This is not new—Microsoft Windows programmers have used the Microsoft Foundation Class libraries for years. But Java uses predefined functions far more than its predecessor languages.
6Using LibrariesJava programmers need to learn how to search the class libraries, identify classes they need and the class methods, as well as figuring out how to use them.To do this you need to understand object-oriented principles, as well as how to implement interfaces and extend classes. The basics of these topics are in this lecture.
7Searching the Libraries To search the class libraries, a good starting place is java.sun.com. Any link will quickly be obsolete as new versions of Java are released, but an example of the libraries was atChapter Five introduces the javadoc utility, which catalogs the functionality that you acquire by using classes from the Java libraries.
8Object model conceptsFour concepts are critical to understanding object models:Data abstractionPolymorphismInheritanceEncapsulationThese can be remembered with the acronym “A Pie,” for Abstraction, Polymorphism, Inheritance, Encapsulation.
9Data AbstractionData abstraction is the process of distilling data down to its essentials. In an object schema, the abstract data model is implemented as a tree. The following figure in the next slide shows such a tree structure for a genealogical database.
10Data Abstraction(Cont) A tree structure for a genealogical database:PersonEmployeeStudentGraduateStudentUndergraduateStudentFacultyStaff
11ObjectsKey uses of Abstraction in object-oriented programming are the concepts of classes and objects. Classes are abstractions of real world concepts. An object is an instantiation of a class. Note that a software object that represents a person is a very different thing than a real person. (The person would not fit in the computer!)
12PolymorphismDistinctive objects respond distinctly to the same message.Polymorphism mainly requires the ability for different objects to have methods with the same name, but different parameters.
13OverloadingPolymorphism in Java is largely accomplished with overloading. See section 5.1 of the text for an explanation of overloading. See also the discussion of sub-types and polymorphism in section
14Rule of OverloadingTwo methods or constructors can be overloaded if they have different numbers of parameters or the same number of parameters for different types. No two methods or constructors in the same class may have identical signatures.
15Operator OverloadingJava operators are overloaded on primitive types. As an example, the arithmetic operators are overloaded on all numeric types. Operators + and += are also overloaded on class String for concatenation. This allows computations to take place in naturally within the understood meaning of the operations.
16Common Sense Overloading There are only two situations where overloading makes sense:When there is a general, nondiscriminative description of the functionality that fits all of the overloaded methods. (or)When all the overloaded methods offer the same functionality, but some of them have default arguments.Overloading shoud be avoided in other situations.
17InheritanceInheritance in the object model is a means of defining one class in terms of another. This is common usage for most of us. For example, a student is a type of person. There are certain characteristics that are true for all persons, yet there are specific characteristics for students.Section 5.2 is mostly about inheritance.
18Inheritance(Cont)In following example, a Student is a type of Person. Likewise, a Employee is a type of Person. Both Student and Employee inherit all the attributes and methods of Person. Student has a locally defined student ID attribute. Employee has a locally defined employee ID attribute.If you look at a Student object, you see attributes of name, date of birth and student ID.
20Constructors in Extended Classes Initializing an extended class takes place in two phases.Inherited fields from the superclass are initialized by calling constructors from that classConstructors of the extended class must initialize fields declared in that class.
21Dynamic BindingMethod invocations for object oriented languages are bound at run time, not at compile time. This is called Dynamic Binding. Dynamic Binding is required because objects can be instantiated at any time, not just when the code itself is initialized. Dynamic binding has the benefit of additional flexibility.
22Definitions Variable –a storage location with an associated type Object –an instance of a classSubtypes –properties of a subclass variable that may differ from the properties of the superclass variables
23SubclassesA subclass can extend a superclass by inheriting features and adding more features. It can also specialize a superclass for a particular purpose. Every instance of a subclass is an instance of the superclass, but the reverse is not true. Some features are available in the subclass but not in the superclass.
24Polymorphic Assignment In static programming languages like c, the left and right hand sides of an assignment must be the same type. In object oriented languages, polymorphic assignment is allowed. The right hand side may be either the same type as the left hand side or a subtype of it.
25DowncastingAlthough you can assign a subtype to a supertype variable, if you are going to assign the result to a variable of the subtype, you must downcast it. This is because the subtype does not know that the supertype contains compatible information. Text example:student3 = (Graduate) student2
26Java Array typesAll Java arrays are objects, subtypes of object. If a class or interface is a subtype of another class or object, then an array of the subclass is a subtype of an array of the superclass. You would need to downcast array members under the same conditions that would require downcasts if it were not a member of an array.
27Overriding MethodsA key idea in inheritance is overriding methods from a superclass in a subclass to make them more specific. (section 5.2.3)Overriding refers to the introduction of an instance method in a subclass that has the same name, signature and return type of a method in the superclass. An overridden method replaces the implementation of the method in the superclass for that subclass.
28Overriding vs. Overloading Overloading refers to methods of the same class with the same name but different signatures or return types.Overriding refers to methods of different classes with the same name, signatures and return types. The overriden method appears in a subclass.
29Final MethodsA method that is declared as final cannot be overridden. Such methods often collaborate with other methods or classes and need to be protected from possible failures that might be caused by overriding. Final methods also allow the Java compiler and JVM to optimize the byte code in ways that would not be possible if it had to remain flexible and available for overriding.
30Implementing Interfaces Interfaces declare features but provide no implementation. Classes that implement an interface should provide implementations for all features or methods declared in the interface.If a class implements multiple interfaces, it should override all of the abstract methods declared in all of the interfaces.
31Multiple InheritanceA key problem for inheritance in object-orientated languages is multiple inheritance. The text explains the concept in terms of a student employee class that inherits characteristics from two superclasses, student and employee. Java does not allow multiple inheritance, but simulates it with interfaces.
32Interface Name Collisions When methods from multiple interfaces have the same name in an implementing class:Methods with different signatures are overloadedWith same signature and return type, they are considered to be the same methodWith the same signature and different return types, they will generate a compilation errorWith same signature and return type bu different exceptions, they are considered to be the same method and the throws list contains all the exceptions.
33EncapsulationEncapsulation is the object model concept of including processing or behavior with the object instances defined by the class. Encapsulation allows code and data to be packaged together.The definition of methods for a class is an integral part of encapsulation. A method is programming code that performs the behavior an object instance can exhibit.
34Information HidingInformation hiding is an important part of encapsulation. The idea is that if the inputs and outputs of an object are fully specified, the way that it is accomplished does not need to be visible. However, as explained in section 5.4, certain forms of hiding are dangerous, as they can lead to unexplained inconsistencies in output.
35HidingHiding refers to introducing a field instance or class or a class method in a subtype with the same name as a field or class in the superclass.Instance Methods cannot be hidden. They can only be overridden by a method with the same signature and return type.Class methods and fields can only be hidden. They can be hidden by class methods or fields of any signature or type.
36AnimationThe final section of chapter five, section 5.5 on Animation Applets, is really a separate topic.However, animation is very commonly used, especially on Web Sites, so it is important to understand.Note that one of the most common uses of animation is extremely obnoxious—to call attention to advertisements.
37SummaryThe author does an excellent job of calling attention to key points in the summary of each chapter. Make certain that you understand each concept mentioned in the summary.
38Review QuestionsHow does Java’s extensive class library represent a different approach to software development from earlier languages?What is the relationship between encapsulation and information hiding?When does it make sense to overload methods?Does inheritance represent an “is a” relation?
39BibliographyJia, Xiaoping, Object Oriented Software Development Using Java. Addison Wesley, 2003