George Blank University Lecturer.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Advanced Object-Oriented Programming Features
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
Chapter 13: Object-Oriented Programming
Chapter 10 Classes Continued
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12 Support for Object oriented Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
BY:- TOPS Technologies
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Object Oriented Programming
Understanding Inheritance
Java Programming Language
Java – Inheritance.
Java Inheritance.
Inheritance in Java CS 3331 Fall 2009.
Final and Abstract Classes
Presentation transcript:

George Blank University Lecturer

Object Oriented Software Development Using Java Chapter 5 CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 5

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

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.

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.

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.

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 Employee Student Graduate Student Undergraduate Student Faculty Staff

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!)

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.

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.

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

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.

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 ID

Constructors in Extended Classes Initializing an extended class takes place in two phases. Inherited fields from the superclass are initialized by calling constructors from that class Constructors of the extended class must initialize fields declared in that class.

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

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.

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.

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

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.

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.

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.

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.

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.

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.

Interface Name Collisions When 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.

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.

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.

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.

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.

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.

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?

Bibliography Jia, Xiaoping, Object Oriented Software Development Using Java. Addison Wesley, 2003