chapter five - classes and inheritance

39
George Blank University Lecturer

Upload: buidang

Post on 14-Feb-2017

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: Chapter Five - Classes and Inheritance

CS 602Java and the Web

Object Oriented Software Development Using Java

Chapter 5

Page 3: Chapter Five - Classes and Inheritance

Chapter Five Objectives• Chapter Five is largely about the use of

object-oriented technology in Java, particularly inheritance, polymorphism, overloading, overriding, hiding, subtypes and casting.

Page 4: Chapter Five - Classes and Inheritance

Java’s Uniqueness• Chapter 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.

Page 5: Chapter Five - Classes and Inheritance

The Class Libraries• However, 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.

Page 6: Chapter Five - Classes and Inheritance

Using Libraries• Java 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.

Page 7: Chapter Five - Classes and Inheritance

Searching 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 at http://java.sun.com/j2se/1.4.2/docs/api/

• Chapter Five introduces the javadoc utility, which catalogs the functionality that you acquire by using classes from the Java libraries.

Page 8: Chapter Five - Classes and Inheritance

Object model concepts– Four concepts are critical to understanding object models:

• Data abstraction• Polymorphism• Inheritance• Encapsulation

– These can be remembered with the acronym “A Pie,” for Abstraction, Polymorphism, Inheritance, Encapsulation.

Page 9: Chapter Five - Classes and Inheritance

Data Abstraction• Data 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.

Page 10: Chapter Five - Classes and Inheritance

Data Abstraction(Cont)A tree structure for a genealogical database:

Person

Student Employee

Graduate Student

UndergraduateStudent

Faculty Staff

Page 11: Chapter Five - Classes and Inheritance

Objects• Key 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!)

Page 12: Chapter Five - Classes and Inheritance

Polymorphism• Distinctive 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.

Page 13: Chapter Five - Classes and Inheritance

Overloading• Polymorphism 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 5.2.2.

Page 14: Chapter Five - Classes and Inheritance

Rule of Overloading• Two 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.

Page 15: Chapter Five - Classes and Inheritance

Operator Overloading• Java 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.

Page 16: Chapter Five - Classes and Inheritance

Common Sense OverloadingThere are only two situations where overloading

makes sense:1. When there is a general, nondiscriminative

description of the functionality that fits all of the overloaded methods. (or)

2. When all the overloaded methods offer the same functionality, but some of them have default arguments.

Overloading shoud be avoided in other situations.

Page 17: Chapter Five - Classes and Inheritance

Inheritance• Inheritance 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.

Page 18: Chapter Five - Classes and Inheritance

Inheritance(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.

Page 19: Chapter Five - Classes and Inheritance

Inheritance(Cont)

PersonName

Birthday

Student

Student ID

Employee

Employee ID

Page 20: Chapter Five - Classes and Inheritance

Constructors in Extended Classes

Initializing an extended class takes place in two phases.

1. Inherited fields from the superclass are initialized by calling constructors from that class

2. Constructors of the extended class must initialize fields declared in that class.

Page 21: Chapter Five - Classes and Inheritance

Dynamic Binding• Method 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.

Page 22: Chapter Five - Classes and Inheritance

Definitions• Variable –a storage location with an

associated type• Object –an instance of a class• Subtypes –properties of a subclass

variable that may differ from the properties of the superclass variables

Page 23: Chapter Five - Classes and Inheritance

Subclasses• A 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.

Page 24: Chapter Five - Classes and Inheritance

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

Page 25: Chapter Five - Classes and Inheritance

Downcasting• Although 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

Page 26: Chapter Five - Classes and Inheritance

Java Array types• All 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.

Page 27: Chapter Five - Classes and Inheritance

Overriding Methods• A 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.

Page 28: Chapter Five - Classes and Inheritance

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

Page 29: Chapter Five - Classes and Inheritance

Final Methods• A 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.

Page 30: Chapter Five - Classes and Inheritance

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

Page 31: Chapter Five - Classes and Inheritance

Multiple Inheritance• A 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.

Page 32: Chapter Five - Classes and Inheritance

Interface Name CollisionsWhen methods from multiple interfaces have the same

name in an implementing class:• Methods with different signatures are overloaded• With same signature and return type, they are

considered to be the same method• With the same signature and different return types, they

will generate a compilation error• With same signature and return type bu different

exceptions, they are considered to be the same method and the throws list contains all the exceptions.

Page 33: Chapter Five - Classes and Inheritance

Encapsulation• Encapsulation 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.

Page 34: Chapter Five - Classes and Inheritance

Information Hiding• Information 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.

Page 35: Chapter Five - Classes and Inheritance

Hiding• Hiding 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.

Page 36: Chapter Five - Classes and Inheritance

Animation• The 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.

Page 37: Chapter Five - Classes and Inheritance

Summary• The 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.

Page 38: Chapter Five - Classes and Inheritance

Review Questions• How 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?

Page 39: Chapter Five - Classes and Inheritance

Bibliography• Jia, Xiaoping, Object Oriented Software

Development Using Java. Addison Wesley, 2003