Object Oriented Programming

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advertisements

Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Computer Science I Inheritance Professor Evan Korth New York University.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
Object Oriented Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Object-Oriented Programming: Polymorphism Chapter 10.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object-Oriented Programming: Polymorphism
Object-Oriented Programming: Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
IFS410: Advanced Analysis and Design
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Java Inheritance.
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Presentation transcript:

Object Oriented Programming Object Oriented Programming (OOP) Encapsulates data (attributes) and methods (behaviors) Objects Allows objects to communicate Well-defined interfaces Procedural programming language C is an example Action-oriented Functions are units of programming Object-oriented programming language Java is an example Object-oriented Classes are units of programming Functions, or methods, are encapsulated in classes

Implementing a Time Abstract Data Type with a Class We introduce classes Time1 and TimeTest Time1.java defines class Time1 TimeTest.java defines class TimeTest public classes must be defined in separate files Class Time1 will not execute by itself Does not have method main TimeTest1, which has method main, creates (instantiates) and uses Time1 object

Implementing a Time Abstract Data Type with a Class (cont.) Every Java class must extend another class Time1 extends java.lang.Object If class does not explicitly extend another class class implicitly extends Object Class constructor Same name as class Initializes instance variables of a class object Called when program instantiates the class object Don’t have any data type Can take arguments, but cannot return data types Class can have several constructors, through overloading Example: TheTime1 Project

Implementing a Time Abstract Data Type with a Class (cont.) Overriding methods - Method Object.toString Gives String representation of object We want String in standard-time format Time1 overrides method toString Overriding method gives String in standard-time format If we remove the override method Time1 still compiles correctly Uses method java.lang.Object.toString But gives String representation of object

Controlling Access to Members Member access modifiers Control access to class’ instance variables and methods public Variables and methods accessible to class clients Present the class’s client a view of service that is provides by the class private Variables and methods not accessible to class clients Accessor method (“get” method) Allow clients to read private data Mutator method (“set” method) Allow clients to modify private data

Initializing Class Objects: Constructors Class constructor Same name as class Invoke automatically when the class object is instantiated Initializes instance variables of a class object Call class constructor to instantiate object of that class ref = new ClassName( arguments ); ref is reference to appropriate data type new indicates that new object is created ClassName indicates type of object created arguments specifies constructor argument values

Using Overloaded Constructors Methods (in same class) may have same name Must have different parameter lists Attempting to overload a method of a class with another method that has the exact same signature (name and parameter type) is a syntax error The constructors guarantee that every object begins is a consistent state. Example: TheTime2 Project

Using ‘Set’ and ‘Get’ Methods Accessor method (“get” method) public method Allow clients to read private data Mutator method (“set” method) Allow clients to modify private data Example: TheTime3 Project

Final Instance Variables final keyword Indicates that variable is not modifiable Any attempt to modify final variable results in error private final int INCREMENT = 5; Declares variable INCREMENT as a constant Enforces principle of least privilege Example: Increment Project

Composition: Objects as Instance Variables of Other Classes Class contains references to objects of other classes These references are members Member object does not need to be initialized immediately with constructor arguments. Example: EmployeeTest Project

Static Class Members static keyword static class variable Static member declaration begins with a ‘static’ keyword Static class variables and methods exist independently and they are instantiated before any objects of the class. Static class accessing ClassName.StaticVariableOrMethodName;

Inheritance & Polymorphism Software reusability Classes are created from existing ones Absorbing attributes and behaviors Adding new capabilities Convertible inherits from Automobile Polymorphism Enables developers to write programs in general fashion Handle variety of existing and yet-to-be-specified classes (override) Helps add new capabilities to system

Inheritance (cont.) Inheritance Composition Subclass inherits from superclass Subclass usually adds instance variables and methods Single vs. multiple inheritance Java does not support multiple inheritance!! Interfaces (discussed later) achieve the same effect “Is a” relationship Composition “Has a” relationship

Superclasses and Subclasses “Is a” Relationship Object “is an” object of another class Rectangle “is a” quadrilateral Class Rectangle inherits from class Quadrilateral Form tree-like hierarchical structures

An inheritance hierarchy for university CommunityMembers. CommunityMember is a direct superclass of Employee CommunityMember is an indirect superclass of Faculty A portion of a Shape class hierarchy.

protected Members protected access members Between public and private in protection Accessed only by Superclass methods Subclass methods

Memberships private public protected accessible only from within the class itself. not inherited by its subclasses. public accessible wherever the program has a reference to an object of a class or one of its subclasses. protected intermediate level of access between public and private

Relationship between Superclass Objects and Subclass Objects Can be treated as superclass object Reverse is not true Shape is not always a Circle Every class implicitly extends java.lang.Object Unless specified otherwise in class definition line Example: InheritanceTest Project

Superclass Constructors in Subclasses Initializes superclass instance variables of subclass Not inherited by subclass Called by subclass Implisit call Explisit call with super reference

Implicit Subclass-Object-to-Superclass-Object Conversion Superclass reference and subclass reference Implicit conversion Subclass reference to superclass reference Subclass object “is a” superclass object Four ways to mix and match references Refer to superclass object with superclass reference Refer to subclass object with subclass reference Refer to subclass object with superclass reference Can refer only to superclass members Refer to superclass object with subclass reference Syntax error

Software Engineering with Inheritance Create class (subclass) from existing one (superclass) Subclass creation does not affect superclass New class inherits attributes and behaviors Software reuse “Is a” relationship Teacher is an Employee Composition “Has a” relationship Employee has a TelephoneNumber

Case Study: Point, Cylinder, Circle Consider point, circle, cylinder hierarchy Point is superclass Circle is Point subclass Cylinder is Circle subclass Example: PointCircleCylinder Project

Polymorphism Polymorphism Helps build extensible systems Programs generically process objects as superclass objects Can add classes to systems easily Classes must be part of generically processed hierarchy

Dynamic Method Binding Implements polymorphic processing of objects Use superclass reference to refer to subclass object Program chooses “correct” method in subclass For example, Superclass Shape Subclasses Circle, Rectangle and Square Each class draws itself according to type of class Shape has method draw Each subclass overrides method draw Call method draw of superclass Shape Program determines dynamically which subclass draw method to invoke

Final Methods and Classes Cannot be overridden in subclass final class Cannot be superclass (cannot be extended) Class cannot inherit final classes

Abstract Superclasses and Concrete Classes Abstract classes Objects cannot be instantiated Too generic to define real objects TwoDimensionalShape Provides superclass from which other classes may inherit Normally referred to as abstract superclasses Concrete classes Classes from which objects are instantiated Provide specifics for instantiating objects Square, Circle and Triangle

Case Study: A Payroll System Using Polymorphism Abstract methods and polymorphism Abstract superclass Employee Method earnings applies to all employees Person’s earnings dependent on type of Employee Concrete Employee subclasses declared final Boss CommissionWorker PieceWorker HourlyWorker Program: SimplePayroll Project

Dynamic Binding Dynamic binding (late binding) Object’s type need not be know at compile time At run time, call is matched with method of called object This make it easy to add new capabilities to systems with minimal impact It also promotes software reuse

Case Study: Hierarchy Inheritance, Polymorphism & Dynamic Binding Point, Circle, Cylinder hierarchy Modify by including abstract superclass Shape Demonstrates polymorphism Contains abstract method getName Each subclass must implement method getName Contains (non-abstract) methods area and volume Return 0 by default Each subclass overrides these methods Program: ShapeTest Project

Interface Interfaces define and standardize the ways in which systems can interact with one another. typically used when disparate /unrelated classes need to share common methods and constants. To use an interface, a concrete class must specify that it implements the interface must declare each method in the interface Example: PayableInterfaceTest Project

Case Study: Creating and Using Interfaces Use interface Shape Replace abstract class Shape Interface Definition begins with interface keyword Classes implement an interface (and its methods) Contains public abstract methods Classes (that implement the interface) must implement these methods They are normally defined in files by themselves with the same name as the interface and the *.java extension. Program: InterfaceTest Project