Object-oriented Programming in Java

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
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.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object-oriented Programming in Java. What is OOP?  The goal is (subtype) polymorphism  Achieved by Classes (user-defined types) Classes (user-defined.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
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.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
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…..
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
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
Advanced Programming in Java
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
abstract classes and casting objects
Computer Science II Exam 1 Review.
Overloading and Constructors
null, true, and false are also reserved.
Extending Classes.
Java Programming Language
Inheritance.
Polymorphism, Abstract Classes & Interfaces
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Generic Classes and Methods
Java Inheritance.
Inheritance.
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
Subtype Substitution Principle
Topics OOP Review Inheritance Review Abstract Classes
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Inheritance Lakshmish Ramaswamy.
CIS 110: Introduction to computer programming
Presentation transcript:

Object-oriented Programming in Java

What is OOP? The goal is (subtype) polymorphism Achieved by Classes (user-defined types) Inheritance (is-a, substitutability) Dynamic Function Binding Function to run is bound according to the dynamic type of the object

Inheritance extends keyword Terminology: super superclass, parent class, base class subclass, child class, derived class super C++ - declare as virtual to get dynamic binding, static is default Java – dynamic binding is default, declare as final to get static binding

Upcasting Storing a reference to a subtype in a supertype variable Makes sense because of the is-a relationship Allows any subtype object to be used in a context expecting a supertype object Employee e = new SalariedEmployee();

The instanceof operator Returns true if the left operand is the same type or a subtype of the right operand new HourlyEmployee() instanceof Employee is true Often used before downcasting To avoid an exception

Polymorphism Dynamic variation of behavior According to an object’s dynamic type Calls the function corresponding to the type the reference indicates at the moment Invisibly to the program Employee e = new HourlyEmployee(); e.computePay(); e = new SalariedEmployee(); e.computePay(); // A different function! Example: Figure 2 (Allison, Nov. 1999)

Benefits of OOP High degree of separation among components Low coupling Insulates components from changes in other components Example: payroll program Uses the Employee interface The dynamic types are invisible

Downcasting When an object must be interpreted as a subtype When extracting from a collection, for example Must use with care The object may not be the type you’re expecting Checked at runtime Examples: Figures 4, 5 (Allison, Nov. 1999)

Abstract Classes Not meant to be instantiated Define an interface Also define part of the implementation Subclasses complete the implementation abstract keyword Both for classes and methods

java.lang.Object The Mother of all Classes Object methods: All objects are subtypes of Object Object methods: int hashCode( ); boolean equals(Object); String toString( ); Among others…

Object.toString Provides a String representation of an object Prints the class name and the hashCode Should override

Object.equals(Object) Used for a value-based equality test Override carefully: Test == this first if true, return true Test instanceof TheClass if false, return false Then check all the fields Use equals() recursively for object fields Can super.equals() for inherited fields Examples: ColorPoint (next slide) and SuperEquals.java Always override hashCode() if you override equals()

ColorPoint public boolean equals(Object other) { if (this == other) return true; else if (!(other instanceof ColorPoint)) return false; ColorPoint p = (ColorPoint) other; return (super.equals(p) && p.color.equals(this.color)); } Instance variables in parent class, Point: Instance variable in child class, ColorPoint: int x, y Color color

Floating-point Fields Don’t compare for equality with ==! Has to do with special values (NaN, etc.) Convert floats to int with Float.floatToIntBits Convert doubles to long with Double.doubleToLongBits Compare the resulting integers with ==

hashCode Used to associate an object with an int Used in hash tables and other containers Should be “as unique as possible” Rules: Boolean: (f ? 0 : 1) byte, char, or short: (int) f Long: (int)(f ^ (f>>>32)) Float: Float.floatToIntBits(f) Double: Double.doubleToLongBits(f) Combine with prime numbers: h = 37*f1; h = 37*h;… Objects: combine their hashcodes (see pp. 197-198)