Download presentation
Presentation is loading. Please wait.
Published byAlicia Bailey Modified over 9 years ago
1
Objects & Classes Lakshmish Ramaswamy
2
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
3
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
4
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
5
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
6
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
7
Inheritance Lakshmish Ramaswamy
8
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
9
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
10
IS-A Hierarchy Example Animal VertebrateInvertebrate FishAmphibianReptileBirdMammal Primate Human
11
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
12
Running example
13
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
15
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
16
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
17
Student as Extension to Person
18
Memory Layout
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.