Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Corso di Algoritmi e Strutture Dati con Laboratorio A.A. 2014/15 Lezione 5.1 The String and Scanner classes.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
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.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Chapter 8 More Object Concepts
Object Oriented Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance in the Java programming language J. W. Rider.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Software Development
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
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,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Classes, Interfaces and Packages
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 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
A CLASS CONSISTS OF VARIABLES,
Topic: Classes and Objects
Object-Oriented Concepts
More Sophisticated Behavior
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Computer Science II Exam 1 Review.
Chapter 7: Strings and Characters
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism, Abstract Classes & Interfaces
Defining Classes and Methods
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
Presentation transcript:

Chapter 1 Object-Oriented Concepts

A class consists of variables called fields together with functions called methods that act on those fields.

Let’s look at the String class

An object is a variable whose type is a class. An object has the fields and can call the methods of its class.

A String object is a variable that contains a string (a sequence of characters) and can call methods in the String class.

String s; In this declaration, s is not a String object, but rather a String reference, that is, a variable that can hold the address of a String object.

To store the address of a String object in s, we will: 1. Allocate space for a new String object. 2. Initialize the fields in that object. 3. Assign to s the address of that object.

s = new String();

A method with the same name as the class is called a constructor. The purpose of a constructor is to initialize the object’s fields. A class’s default constructor has no parameters.

The String class’s default constructor initializes the fields so that the String object represents an empty string.

Another constructor in the String class has a String parameter. Here is the heading parameter public String (String original) String t = new String (“Aloha”); argument is a reference to “Aloha”

Now the String objects referenced by s and t can invoke String methods: s.length() // returns 0 t.toLowerCase() // returns (a reference to) // “aloha”. t is still a // reference to “Aloha”

/** * Returns the index within this String object * of the first occurrence of the specified * substring. str – the specified substring the index of the first occurrence * of str in this String object, or –1 * if str is not a substring of this * String object

NullPointerException – if str is * null */ public int indexOf (String str) The JAVADOC comments plus the method heading constitute the method specification – A user’s view of the method.

System.out.println (t.indexOf (“ha”)); System.out.println (t.indexOf (“a”)); System.out.println (s.indexOf (“ha”)); Hint: Indexes start at 0.

String w = null; w does not contain the address of any String object, so w cannot call any methods.

The equals method tests for equality of objects, and the == operator tests for equality of references. String z = new String (“Aloha”);

s.equals (“”) s == “” t.equals (“Aloha”) t == “Aloha” t.equals (null) t.equals (z) t == z w.equals (null) w == null

String y1 = “Aloha”; String y2 = “Aloha”; These statements create two references, y1 and y2, to the same string object, so y1 == y2 // returns true y1 == t // returns false but y1.equals (t) // returns true

So far, we have studied what a class does, not how the class does it. That is, we have studied a class from the user’s perspective (method specifications) rather than from a developer’s perspective (fields and method definitions)

Principle of data abstraction: A user’s code should not access the implementation details of the class used.

Many of the classes we will study share the same method specifications.

When we abstract these specifications from the classes we get an interface. An interface consists of method specifications and constants only.

For example, here is an interface for the employees in a company. The information read in for each employee consists of the employee’s name and gross pay.

Note: Each method is automatically public, and each method heading is followed by a semicolon.

To implement that interface, we will create a class with fields and, using those fields, definitions of at least the two methods.

Note: The parameter type must be Employee because that is the parameter type in the interface. public boolean makesMoreThan (Employee otherEmployee) { if (!(otherEmployee instanceof FullTimeEmployee)) return false; FullTimeEmployee full = (FullTimeEmployee)otherEmployee; return grossPay > full.grossPay; } // method makesMoreThan

In a method definition, when a member (field or method) appears without an object reference, a reference to the calling object is assumed.

Now suppose we want to find the best-paid full-time employee in a company. We will create a Company class.

There are methods to initialize a Company object, to find the best-paid full-time employee, and to print that employee’s name and gross pay.

Finally, we need a main method to get everything started.

Exercise: Make up sample input, and the corresponding output.

Inheritance

Inheritance is the ability to define a new class that includes all the fields and some or all of the methods of an existing class.

SUPERCLASS SUBCLASS Existing class = superclass = base class New class = subclass = derived class

The subclass may declare new fields and methods, and may override existing methods by giving them method definitions that differ from those in the superclass.

Example:Find the best-paid hourly full-time employee with no overtime (40 hours) Input: Name, Hours worked, Pay rate

Modify FullTimeEmployee class?

The Open-Closed Principle Every class should be Open:extendible through inheritance Closed:stable for existing applications

Specifically, the FullTimeEmployee class should be stable for the existing application of finding the best-paid employee in a company. And extendible for this new application!

Find best paid employee project Find best paid hourly employee project FullTimeEmployee Class HourlyEmployee Class

Overridden methods?

The declarations of name and grossPay must be altered in the FullTimeEmployee class: those fields cannot be private. Would it be a good idea to make them public?

Object-Oriented Essentials: 1. Encapsulation 2. Inheritance 3. Polymorphism

A virtual method is a method that is bound to its method identifier at run- time. In Java, almost all methods are virtual.