 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
Object-oriented Programming Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Object-Oriented Programming Concepts
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
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.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Programming in Java CSCI-2220 Object Oriented Programming.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
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)
What is an Object? Real world objects are things that have: 1) state 2) behavior Example: your dog: 1) state – name, color, breed, sits?, barks?, wages.
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.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
Introduction to Object-Oriented Programming Lesson 2.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Introduction to Object-oriented Programming
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
JAVA By Waqas.
Java Primer 1: Types, Classes and Operators
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming
CIS 199 Final Review.
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning

 Data (nouns) versus operations (verbs):

 Procedural programming is verb-oriented: ◦ decomposition around operations ◦ operation are divided into smaller operations  Programming Languages: ◦ C ◦ Pascal ◦ Fortran, etc.

 Real world objects are things that have: ◦ state ◦ Behavior  Example: your dog: ◦ state – name, color, breed, sits?, barks?, wages tail?, runs? ◦ behavior – sitting, barking, waging tail, running  A software object is a bundle of variables (state) and methods (operations).

 A class is a blueprint that defines the variables and methods common to all objects of a certain kind.  Example: ‘your dog’ is a object of the class Dog.  An object holds values for the variables defines in the class.  An object is called an instance of the Class

 operations: change Gears, brake, change Cadence  variables: current Speed, current Cadence, current Gear  static variable: number Of Gears ◦ It holds the same value for all objects of the class.

 Programming defined in terms: ◦ objects (nouns) and ◦ relationships between objects  Object-Oriented programming languages: ◦ SmallTalk ◦ C++ ◦ C# ◦ Java

Encapsulation Inheritance Polymorphism

 Enclose data inside an object.  Along with data, include operations on this data.  Data cannot be accessed from outside except through the operations.  Provides data security and facilitates code reuse.  Operations provide the interface - internal state can change without affecting the user as long as the interface does not change.

 Features: ◦ a class obtains variables and methods from another class ◦ the former is called subclass, the latter super-class ◦ a sub-class provides a specialized behavior with respect to its super-class ◦ inheritance faciliates code reuse and avoids duplication of data Super-class Subclass

 Polymorphism = many different (poly) forms of objects that share a common interface respond differently when a method of that interface is invoked.  Polymorphism is enabled by inheritance: ◦ a super-class defines an interface that all sub-classes must follow ◦ it is up to the sub-classes how this interface is implemented; a subclass may override methods of its super-class  Therefore, objects from the classes related by inheritance may receive the same requests but respond to such requests in their own ways.

 Everything in Java is an object.  Well... almost.  Object lifecycle: ◦ Creation ◦ Usage ◦ Destruction

 A variable s is declared to refer to the objects of type/class String: String s;  The value of s is null; it does not yet refer to any object.  A new String object is created in memory with initial “abc” value: String s = new String(“abc”);  Now s contains the address of this new object.

 Objects are used mostly through variables.  Four usage scenarios: ◦ one variable, one object ◦ two variables, one object ◦ two variables, two objects ◦ one variable, two objects

 One variable, one object: String s = new String(“abc”);  What can you do with the object addressed by s? ◦ Check the length: s.length() == 3 ◦ Return the substring: s.substring(2) ◦ etc.  Depending what is allowed by the definition of String.

 Two variables, one object: String s1 = new String(“abc”); String s2;  Assignment copies the address, not value: s2 = s1;  Now s1 and s2 both refer to one object. After s1 = null;  s2 still points to this object.

 Two variables, two objects: String s1 = new String(“abc”); String s2 = new String(“abc”);  s1 and s2 objects have initially the same values: s1.equals(s2) == true  But they are not the same objects: (s1 == s2) == false  They can be changed independently of each other.

 One variable, two objects: String s = new String(“abc”); s = new String(“cba”);  The “abc” object is no longer accessible through any variable.

 A program accumulates memory through its execution.  Two mechanism to free memory that is no longer need by the program: ◦ manual – done in C/C++ ◦ automatic – done in Java  In Java, when an object is no longer accessible through any variable, it is eventually removed from the memory by the garbage collector.  Garbage collector is parts of the Java Run-Time Environment.

 A basis for the Java language.  Each concept we wish to describe in Java must be included inside a class.  A class defines a new data type, whose values are objects: ◦ a class is a template for objects ◦ an object is an instance of a class

 A class contains a name, several variable declarations (instance variables) and several method declarations. All are called members of the class.  General form of a class:

 A class with three variable members:  A new Box object is created and a new value assigned to its width variable:

 Place the Box class definitions in file Box.java:  Place the BoxDemo class definitions in file BoxDemo.java:  Compilation and execution:

 Each object has its own copy of the instance variables: changing the variables of one object has no effect on the variables of another object.  Consider this example:

What are the printed volumes of both boxes?

 Obtaining objects of a class is a two-stage process: ◦ Declare a variable of the class type: Box myBox;  The value of myBox is a reference to an object, if one exists, or null.  At this moment, the value of myBox is null. ◦ Acquire an actual, physical copy of an object and assign its address to the variable. How to do this?

 Allocates memory for a Box object and returns its address: Box myBox = new Box();  The address is then stored in the myBox reference variable.  Box() is a class constructor - a class may declare its own constructor or rely on the default constructor provided by the Java environment.

 Memory is allocated for objects dynamically.  This has both advantages and disadvantages: ◦ as many objects are created as needed ◦ allocation is uncertain – memory may be insufficient  Variables of simple types do not require new: int n = 1;  In the interest of efficiency, Java does not implement simple types as objects. Variables of simple types hold values, not references.

 Assignment copies address, not the actual value: ◦ Box b1 = new Box(); ◦ Box b2 = b1;  Both variables point to the same object.  Variables are not in any way connected. After b1 = null; b2 still refers to the original object.

 General form of a method definition:  Components: ◦ type - type of values returned by the method. If a method does not return any value, its return type must be void. ◦ name is the name of the method ◦ parameter-list is a sequence of type-identifier lists separated by commas ◦ return value indicates what value is returned by the method.

 Classes declare methods to hide their internal data structures, as well as for their own internal use:  Within a class, we can refer directly to its member variables:

 When an instance variable is accessed by code that is not part of the class in which that variable is defined, access must be done through an object:

 The type of an expression returning value from a method must agree with the return type of this method:

 The type of a variable assigned the value returned by a method must agree with the return type of this method:

 Parameters increase generality and applicability of a method: ◦ method without parameters int square() { return 10*10; } ◦ method with parameters int square(int i) { return i*i; }  Parameter: a variable receiving value at the time the method is invoked.  Argument: a value passed to the method when it is invoked.

Questions? 62