Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Web Application Development Slides Credit Umair Javed LUMS.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
1. 2 Object-Oriented Concept Class & Object Object-Oriented Characteristics How does it work? Relationships Between Classes Development Tools Advantage.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Abstraction ADTs, Information Hiding and Encapsulation.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Introduction to Defining Classes
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Classes, Interfaces and Packages
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
OOP Basics Classes & Methods (c) IDMS/SQL News
Comp1004: Building Better Objects II Encapsulation and Constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming Concepts
Classes C++ representation of an object
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Objects as a programming concept
Inheritance and Polymorphism
Module 5: Common Type System
Intro To Classes Review
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Object Oriented Analysis and Design
CSC 143 Inheritance.
Inheritance Basics Programming with Inheritance
Encapsulation and Constructors
Computer Programming with JAVA
Object-Oriented Programming in PHP
Classes C++ representation of an object
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
Lecture 10 Concepts of Programming Languages
Classes and Objects CGS3416 Spring 2019.
Presentation transcript:

Object-Oriented Programming

What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions) ANYTHING can be described or treated as an object Primitive vs. Complex types Primitives are simple (int, double, char, boolean) Objects are complex types

How do we define objects? Objects are defined in “class” files Classes are object definitions Classes are templates for objects Class variables store attributes Data members Instance variables Class functions (methods) represent actions Methods are functions defined in classes

Why define objects? Encapsulation! Advantages? Wrap together related data and actions into an object Advantages? Organization Limits the scope of data and actions to a single entity Limits dependencies between unrelated things

Example Class return type method signature public class Person { private String name = “None”; //instance variables private int age = 0; public Person() {} //constructor public String getName() //getter or accessor method { return name; } public void setName(String newName) //setter or mutator method name = newName; return type method signature

Method Terminology return type – the type of data that the method returns parameter variables – data passed into the method signature – the part of the method header comprised of the method name and its parameters constructor – a method that runs to create an instance of the object (has the same name as the class name) accessor – a method that accesses and returns an instance variable, data member, or attribute of the class mutator – a method that changes an instance variable or attribute of the class

Controlling access to attributes & methods http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html public – visible to all things outside of the class private – *only* visible within the class itself protected – visible to child classes What’s a child class?!?

Inheritance A parent class contains common attributes and methods for a group of related child classes Child classes inherit all public and protected “traits” (attributes and methods) from the parent Example: Person class protected String name protected int age Student class inherits from Person protected String name inherited from Person automatically protected int age  inherited from Person automatically private int IDnum  only created and found in Student

Java Inheritance Example public class Person { protected String name = “”; protected int age = 0; public Person() {} }

Java Inheritance Example cont’d public class Student extends Person { private int IDnum = 0; public Student() {} } What attributes does Student have? name, age, IDnum

IS-A and HAS-A relationships When a class “has” the data member (variable) or method Example: Student has-a ID number Is-a When a class is a subclass of a super/parent class Student is-a Person

What are the advantages of inheritance? Organization! Parent classes can have common attributes and methods Reuse parent classes for different child classes No need to redefine all the same attributes and methods every time you have a new, similar child class Reduce programming maintenance overhead Less time to make widespread changes (by updating parents)

Inheritance Practice Create a Person class with appropriate instance variables and methods (accessors and mutators) Create a Teacher class with appropriate instance variables and methods (accessors and mutators) Teacher should inherit from (extends) Person Create a Student class with appropriate instance variables and methods (accessors and mutators) Student should inherit from (extends) Person Create Freshman, Sophomore, Junior, and Senior classes that each have one new instance variable Each of these classes should inherit from (extends) Student Write a main method that creates objects for all of the above classes, populates the instance variables using the mutators, and prints out their values using the accessors.

Polymorphism Actions or methods that have the same name, but different parameter lists and/or processes Overriding methods in child classes is polymorphic too E.g. toString() in both Person and Student //polymorphic constructor example public Student() {} public Student(String name, int IDnum) { this.name = name; this.IDnum = IDnum; }

Polymorphism cont’d String substring(int beginIndex) String substring(int beginIndex, int endIndex) String substring (int beginIndex, int endIndex) int indexOf(int ch) int indexOf(int ch, int fromIndex) int indexOf(String str) int indexOf(String str, int fromIndex)

Polymorphism Advantages External programs can use the same action on a family of objects without knowing implementation details Example: Person p = new Person(); Student s = new Student(); System.out.println(p.toString()); //different toString() System.out.println(s.toString()); //but we don’t know that

Libraries Collect useful methods together into a library Examples: Java Math Library Advantages? No need to rewrite / reinvent methods or algorithms

Instantiation vs. Static Creating an instance of an object (e.g. using the word new) Class is defined with the idea that multiple objects will be created Each instantiated object has a copy of the attributes and methods Example: Person p = new Person() // instantiates a Person object What if you don’t need to make multiple things? Example: Java Math Library We use the static keyword (static means “only one” or unchanging) static can be used with variables or methods static members are not copied (i.e. only one copy ever exists)

Advantages/Disadvantages of OOP Organization! Modular design and development Easier debugging Easier testing Programming team development – focus on particular modules, testing, information hiding (to reduce module dependencies) Code reuse Faster to develop complex projects Reduce maintenance overhead Disadvantages: Small, simple programs and problems are more complex E.g. Java’s requirement of classes and methods vs. Python