Objects & Classes Lakshmish Ramaswamy. instanceOf Operator Testing whether an object is of a certain type (class) object instanceOf classname True if.

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
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.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
Chapter 10: Inheritance and Polymorphism
Chapter 10 Classes Continued
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Programming in Java Habib Rostami Lecture 5.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Objected Oriented Programming & Design JAVA Shishir Gupta (704) (704)
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Object Oriented Programming
Department of Computer Science Data Structures Using C++ 2E Chapter 2 Object-Oriented Design (OOD) and C++  Learn about inheritance  Learn about derived.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Coming up: Inheritance
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Inheritance and Polymorphism
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-Oriented Programming: Inheritance
Objects as a programming concept
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 22 Inheritance Richard Gesick.
Chapter 9: Polymorphism and Inheritance
Interfaces.
Chapter 12 Abstract Classes and Interfaces
Applying OO Concepts Using Java
Inheritance Lakshmish Ramaswamy.
Inheritance Lakshmish Ramaswamy.
Presentation transcript:

Objects & Classes Lakshmish Ramaswamy

instanceOf Operator Testing whether an object is of a certain type (class) object instanceOf classname True if object is of type classname False if object is not of type classname False if object is set to null

Java Packages Used to organize similar classes Slightly relaxed visibility restrictions Avoid name conflicts –Classes with identical names can be in two different packages –Use package name followed by class name for disambiguation java.util.Date today = new java.util.Date(); Ex: java.io, java.lang, java.util

import Directive Avoids need to use full package name and class name Allows class specification without package name prefix import java.util.Date; import java.lang.*; // wildcard denoting all classes of package Date today = new Date(); Integer it = new Integer(10); Care needed when using wildcards as it may cause naming conflicts

Package Statement Indicating class as a part of a package requires two things –First line of code should be package Package Name; –Including the file in appropriate directory CLASSPATH variable indicates where to search for packages –setenv CLASSPATH.:$HOME/Directory name containing all packages

Package Visibility Rules No visibility modifier implies field is package visible Only public classes visible outside package –If no visibility modifier specified, class is visible to other classes in same package Classes reachable through CLASSPATH variable considered part of same package

Inheritance Lakshmish Ramaswamy

Introduction Fundamental mechanism for code reuse in OO programming Permits extension of functionality of an object Code maintenance and updating This chapter answers several questions with regards to inheritance in Java

Inheritance Models the IS-A relationship Examples –Car is an automobile –Truck is an automobile –Vertebrates and invertebrates are animals –Circle and square are shapes Can result in hierarchies

IS-A Hierarchy Example Animal VertebrateInvertebrate FishAmphibianReptileBirdMammal Primate Human

HAS-A Relationship Also called COMPOSED-OF relationship Does not have the properties of a IS-A relationship Examples –Car has engine –Human has heart Be careful when using HAS-A and IS-A relationships

Running example

Extending Person Class Student class and Employee class Student is similar to Person class … –Needs all the fields and methods of Person … But needs extra fields and methods –GPA, getGPA(), setGPA() Employee is similar to Person –Salary is extra field –Corresponding accessor and mutator

Why Not Cut & Paste Bug propagation Code maintenance and updates Makes these related concepts look completely unrelated –Cannot send Student or Employee as a parameters to method accepting Person parameter

Inheritance as Solution Student IS-A Person Specify the changes of Student wrt Person Three types of changes are allowed –Add new fields –Add new methods –Override existing methods Changes not permitted –Cannot remove fields –Cannot remove methods Should provide its own constructor

Student as Extension to Person

Memory Layout