Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 31 Part 3.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
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.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Inheritance and Class Hierarchies Chapter 3 Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
INHERITANCE, POLYMORPHISM, CLASS HIERARCHIES AND GENERICS.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Lists and the Collection Interface Review inheritance & collections.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Classes Dwight Deugo Nesa Matic
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
CSC 205 Java Programming II
Data Structures and Algorithms
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Java Programming Language
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Advanced Inheritance Concepts
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College

Classes, Abstract Classes and Interfaces

Number: A Predefined Abstract Class Wrapper classes in Java - allow base types to be treated as “first-class objects” (subclasses of Object)

Object is the Root of the Hierarchy Every class in Java extends Object … directly, if no “extends” keyword All classes inherit methods in class Object … and can (sometimes should) override them

Wrapper Classes Allow Object’s methods to be applied to base types –Example: Integer (for int), Double (for double) Allow base types to benefit from polymorphism –Example: list of Objects (including Integers and Doubles)

Number Provides abstract methods intValue, doubleValue, etc. Provides one concrete method: byteValue –Returns the underlying bits of the number

Casting Methods can only be used with the right type of object (members of the appropriate class) For non-abstract methods, the type must match at compile time –Abstract methods have polymorphism; type matches at runtime Casting tells the compiler to assume that an object is a member of a given class To cast, put the class in parentheses before the object ClassCastException - the object doesn’t match the class that it was cast to

Casting Example // This is a silly example to show casting Object[] myObjects = new Object[2]; myObjects[0] = new Integer(17); myObjects[1] = new String (“hello”); int magicnumber = myObjects[0].intValue() + myObjects[1].length(); //error int magicnumber = ((Integer)myObjects[0]).intValue() + ((String)myObjects[1]).length(); System.out.println(magicnumber + “ should be 22”);

More on Casting Casting does not change the object referenced; it creates an anonymous reference to that object Downcast: cast a higher type (superclass) to a lower type (subclass) The instanceof operator can guard against ClassCastException errors You can downcast an interface reference to the specific implementation type

Java 5.0 Reduces Need for Casting Two new features that reduce the need for casting: –Autoboxing/unboxing –Generics Autoboxing/unboxing eases the conversion between a primitive type and its corresponding wrapper type

Object’s Methods

toString Represents the object as a string Usually used for printing Object’s toString creates a string consisting of the class name and the reference (hash code) –This is almost never what you want! –But if you don’t define your own toString, your class inherits Object’s toString

equals The Object.equals method has a parameter of type Object Compares two objects to determine whether they are equal (“this” to the parameter) You must override the equals method if you want to be able to compare two objects of a class Object’s method compares hash codes (unlikely to be useful)

The Shallow Copy Problem

The statement e1.setAddressLine1("Room 224"); creates a new String object that is referenced by e1.address.line1 and e2.address.line1

The clone method Java provides the Object.clone method to help solve the shallow copy problem First make a shallow copy, copying the current object’s fields –myType cloned = super.clone() Then make a deep copy by cloning all non-primitive objects –cloned.myPart = myPart.clone(); –(do this as many times as needed for every part of the object) Replace myType and myPart with your own object’s type and parts.

The Object.clone method (continued) After e1.setAddressLine1("Room 224"); only e1.address.line1 references the new String object.

Employee.clone()

Address.clone()

Multiple Inheritance, Multiple Interfaces, and Delegation Multiple inheritance: the ability to extend more than one class Multiple inheritance is a language feature that is difficult to implement and can lead to ambiguity –Therefore, Java does not allow a class to extend more than one class

Using Multiple Interfaces to Emulate Multiple Inheritance

Using Multiple Interfaces to Emulate Multiple Inheritance (continued)

Implementing Reuse Through Delegation You can reduce duplication of modifications and reduce problems associated with version control through a technique known as delegation In delegation, a method of one class accomplishes an operation by delegating it to a method of another class

Packages The Java API is organized into packages –Packages usually contain multiple classes Declare a package with “package packagename” at the beginning of the file One package per folder –All classes in the same package are stored in the same directory or folder –All the classes in one folder must declare themselves to be in the same package

The No-Package-Declared Environment and Package Visibility If there’s no package statement, your files are in the default package –There is only one default package Package visibility sits between private and protected Classes, data fields, and methods with package visibility are accessible to all other methods of the same package but are not accessible to methods outside of the package Classes, data fields, and methods that are declared protected are visible to all members of the package

Visibility and Packages