Docsity

Technology - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Technology - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

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.

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, words.

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.

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.

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.

Data Abstraction(Cont)

A tree structure for a genealogical database:

Person

Student Employee

Graduate Student^ UndergraduateStudent Faculty^ Staff

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.

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.

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.

Common Sense Overloading

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

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.

Inheritance(Cont)

Person Name Birthday

Student Student ID

Employee Employee ID

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.

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