Object-Oriented Programming Concepts

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Object Oriented Programming in Java. Characteristics of Object Oriented Programming  Object Programming classes  Encapsulation  Polymorphism  Inheritance.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Object-Oriented Programming Concepts. 2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-oriented Programming Concepts
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
Inheritance using Java
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
By Waqas The topmost class in the java class hierarchy is called “Object”. If you declare a class which does not sub class of any super class.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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 5 Introduction to Defining Classes
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Classes, Interfaces and Packages
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Classes and Objects Introduction Life Cycle Creation Example.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Object Oriented Programming in Java Habib Rostami Lecture 2.
Comp1004: Inheritance II Polymorphism. Coming up Inheritance Reminder Overriding methods – Overriding and substitution Dynamic Binding Polymorphism –
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Object-Oriented Programming
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
JAVA By Waqas.
University of Central Florida COP 3330 Object Oriented Programming
Inheritance and Polymorphism
University of Central Florida COP 3330 Object Oriented Programming
Advanced Programming Behnam Hatami Fall 2017.
Java Inheritance.
Final and Abstract Classes
Presentation transcript:

Object-Oriented Programming Concepts

Good Questions What are Objects? What are Classes? What are Messages? What is Inheritance?

What Are Objects? Software objects model read-world objects or abstract concepts dog, bicycle, queue Real-world objects have states and behaviors Dogs' states: name, color, breed, hungry Dogs' behaviors: barking fetching How do Software objects implement real-world objects? Use variables to implement states Use methods to implement behaviors An object is a software bundle of variables and related methods

Visual Representation of A Software Object Variable Method

Software Bicycle Instance Variables and Instance Method

Encapsulation The objects' variables make up the center of the object. Methods surround and hide the object's center from other objects. Benefit of encapsulation Modularity Information hiding For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.

What Are Classes? A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind. An object is an instance of a certain class. After you have created a class, you must create an instance of it before you can use. Class variables and class methods The benefit of Classes: Reusability

Visual Representation of A Software Class

What Are Messages? Software objects interact and communicate with each other by sending messages to each other.

More on Messages Components of a Message The Benefits of Messages The object to whom the message is addressed (Your Bicycle) The name of the method to perform(changeGears) Any parameters needed by the method (low gear) The Benefits of Messages Messages passing supports all possible interactions between objects (aside from direct variable access) Objects don’t need to be in the same process or even on the same machine to send and receive messages.

What is Inheritance? Inheritance allows classes to be defined in terms of other classes superclass and subclass Each subclass inherits variables and methods from its superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. Subclasses can also override inherited method and provide specialized implementations for those methods. Inheritance or class hierarchy

Benefits of Inheritance Programmers can reuse the code in the superclass many times. Programmers can implement superclasses called abstract classes Abstract class defines “generic” behaviors Define and may partially implement the behavior but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

Message Example

Inheritance Example

Object-Oriented Development in Java

Agenda Java Class and Object Declaration The Life Cycle of an Object Controlling Access to Members of a Class

Java Classes and Objects Class: templates for specifying the state and behavior of an object at runtime Object: instances of a class The concepts of class/object provide a mechanism for encapsulation

Basic Structures of a Class Class Declaration Variable Instance Variable Class Variable Constructor Method Instance Method Class Method Cleanup Rectangle2.java

Point and Rectangle Rectangle2.java

The Class Declaration

Constructor A method in a class that initialize an instance of an object before it's used. The same name as the class and have no return type Multiple Constructors: the name name but a different number of arguments or different typed arguments Method Overloading Java Provides default constructors. The special variable, this, can be used inside a method to refer to the object instance. Rectangle.java

Member Variables Declaration

Instance and Local Variable Local variable is defined inside a block of code or a method. Example (FirstClass.java) Instance Variable: firstVariable Local Variable: half public int getHalf() { int half; // local variable half = firstVariable / 2; return half; }

Methods

Method Declaration

Return a Value from a Method Use return operator in the method to return the value. Methods can return a primitive type or a reference type. The class of the returned object must be either a subclass of or the exact class of the return type.

Method Overload Signature of a Method: return value, name, parameter list Method Overloading: Use the same method name with different arguments to group together related methods. Constructors can also be overloaded. this(parameters) : Call another constructor within a constructor Example: FirstClass.java, SecondClass.java, Rectangle.java

Passing Information into a Method Argument types primitive and reference data type: Yes method: No Argument Names Can have the same name as one of the class's member variable Use this to refer to the member variable Primitive arguments are passed by value. Reference arguments are passed by reference. FirstClass.java

The Life Cycle of an Object Creating Objects Using Objects Cleaning Up Unused Objects

Creating Objects Rectangle r = new Rectangle(5,5,100,200); Declaration: Rectangle r (Type name) Instantiation: new Allocate memory for the object Initialize instance variables Call a constructor Initialization by Calling a Constructor Rectangle(5,5,100,200)

Using Objects Manipulate or inspect its variables Call its methods objectReference.variable r.x = 50 r.y = 80 Call its methods objectReference.methodName(argumentList) r.move(20,30) Java provides an access control mechanism whereby classes can restrict or allow access to its variables and methods.

Clean Up When all references to an object are dropped, the object is no longer required, and become eligible for garbage collection. Call finalize() to release system resources such as open files or open sockets before the object is collected. Release references to other objects protected void finalize() throws Throwable Rectangle2.java

Controlling Access to Members of a Class

Private class Alpha { private int iamprivate; private void privateMethod() { System.out.println ("privateMethod"); } class Beta { void accessMethod() { Alpha a = new Alpha(); a.iamprivate = 10; a.privateMethod(); } How About one Alpha object access the private member of another Alpha object?

Protected package Greek; class Alpha { protected int iamprotected; protected void protectedMethod() { System.out.println ("protectedMethod"); } package Greek; class Gamma { void accessMethod() { Alpha a = new Alpha(); a.iamprotected = 10; a.protectedMethod(); }

Protected (II) import Greek.*; package Latin; class Delta extends Alpha { void accessMethod(Alpha a, Delta d) { a.iamprotected = 10; d.iamprotected = 10; a.protectedMethod(); d.protectedMethod(); }

Public package Greek; public class Alpha { public int iampublic; public void publicMethod() { System.out.println("publicMethod"); } import Greek.*; package Roman; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iampublic = 10; a.publicMethod(); }

Package package Greek; class Alpha { int iampackage; void packageMethod() { System.out.println("packageMethod"); } package Greek; class Beta { void accessMethod() { Alpha a = new Alpha(); a.iampackage = 10; a.packageMethod(); }

Increase the reusability of codes Inhereitance in Java A mechanism you can use to create a new class by extending the definition of another class Increase the reusability of codes Single Inheritance

Simple Class Hierarchy Object Vehicle Building Car Bike House Office Van Truck Superclass and Subclass

More About Inheritance Use the extends keyword to create a subclass. Method Override Use method overriding when you need a subclass to replace a method of its superclass. Define a new method that replaces the superclass method that has the same signature. Calling Superclass Methods super.<method>(parameters) super(parameters) (Calling superclass constructors) Example: GraphicsProgram.java