Handout-2(a) Basic Object Oriented Concepts

Slides:



Advertisements
Similar presentations
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 10 Introduction to Objects and Classess 1.
Introduction To System Analysis and Design
UML – Class Diagrams.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
1 Basic Object-Oriented Concepts  Object-Oriented Paradigm What is an Object?  What is a Class?  Constructing Objects from a class Problem Solving in.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Introduction To System Analysis and design
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object-oriented programming and software development Lecture 1.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
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.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
1 21 COP 3540 Data Structures with OOP Overview: Chapter 1.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
 Computer Science 1MD3 Introduction to Programming Michael Liut Ming Quan Fu Brandon.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Learners Support Publications Object Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
9-Dec Dec-15  INTRODUCTION.  FEATURES OF OOP.  ORGANIZATION OF DATA & FUNCTION IN OOP.  OOP’S DESIGN.
Introduction to Object-Oriented Programming Lesson 2.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically assess the.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Chapter 3 Implementing Classes
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
 The Object Oriented concepts was evolved for solving complex problems. Object- oriented software development started in the 1980s. Object-oriented design.
Concepts of Object Oriented Programming
Programming Logic and Design Seventh Edition
Classes and OOP.
Object-Oriented Analysis and Design
Objects as a programming concept
Chapter 3: Using Methods, Classes, and Objects
Object Oriented Concepts -I
OBJECT ORIENTED PROGRAMMING overview
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Introduction to Objects
Chapter 3 Implementing Classes
Chapter 4: Writing classes
Object oriented vs procedural programming
Fundamentals of Programming
Appendix A Object-Oriented Analysis and Design
Interfaces, Classes & Objects
Dr. R Z Khan Handout-3 Classes
Appendix A Object-Oriented Analysis and Design
Appendix A Object-Oriented Analysis and Design
Introduction to Computer Science and Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Introduction to Objects
Presentation transcript:

Handout-2(a) Basic Object Oriented Concepts Overview What is Object-Orientation about? What is an Object? What is a Class? Constructing Objects from Classes Problem Solving in OO languages (Java) Java Class Library

What is Object-Orientation about? One of the key challenges faced by Computer Scientist is how to handle complexity. Two main concepts used to manage complexity are Modularity and Abstractions. Modularity means breaking a large system up into smaller pieces until each peace becomes simple enough to be handled easily. Abstraction means hiding implementation details in a module and providing a well-defined interface through which the functionality of the module can be accessed by other modules. Thus, each module in a system is treated as a “black box” by other modules. Over the years, computer scientists have developed a number of approaches to achieve modularity and abstraction. The latest of these approaches is Object-Orientation or OO for short. The key concept in OO is of course Object, so we need to first understand what an object is.

What is an Object? An Object is a software entity that models something in the real world. It has two main properties: State: the object encapsulates information about itself - attributes or fields. Behaviour: the object can do some things on behalf of other objects – methods Example: In a banking system, a particular bank account is an example of an object. Its state consists of attributes like: owner, account number, balance, etc. Its behaviours consist of: deposit, withdraw, etc. Other examples of objects are: myself – an instructor object. What are my fields and methods? you – a student object this room – a room object this university your car, etc. In an object-oriented system, objects are created from something called a Class, so next we need to know what a class is.

What is a class? A class is a general, abstract representation of an object, that specifies the fields and methods that such an object has. When we write OO programs we don't define individual objects, we define classes, and then use them as templates for constructing objects. Each individual object is called an instance of its class. For example, you might have a Tree class that describes the features of all trees (each tree has branches and roots, grows, etc.). The Tree class serves as an abstract model for the concept of a tree. To reach out and grab, or cut down a tree, you must have a concrete instance of that tree – a tree object. Of course, once you have a Tree class, you can create lots of different instances of that tree, and each different tree instance can have different features (it can be short, tall, bushy, have fruits, etc), yet still behave like a tree and can be recognized as one – the figure next:

What is a Class? (cont’d) Diagram showing the relationship between Classes and Objects Other examples of classes are: Instructor, Student, Room, University, Car, etc. Notice the difference between these examples and the previous on objects - while these are general, the previous ones are specific. Objects Class

Constructing Objects from classes Once a class has been defined, it can be used to create any number of objects of that class. To create an object, we use new operator, the class name, and supply construction parameters (if any) in parenthesis. For example, the following statement creates and prints an object of the Rectangle class System.out.println(new Rectangle(5, 10, 20, 30)); Most of the times, we would like to do more with an object than just create it, and print it. For example, we might want use one of its methods. To do this, we need to keep the reference (or address) of the object in an object reference variable. An object reference variable is declared by given the class name followed by the variable as in: Rectangle myRectangle; 

Constructing Objects from classes -Cont’d Once the variable is declared, it can be used to create an object and store its reference in the variable as follows: myRectangle = new Rectangle(5, 10, 20, 30); In fact, the process of declaring an object reference variable and creating an object can be combined in one statement as follows. Rectangle myRectangle = new Rectangle(5, 10, 20, 30); The relationship between the reference variable, myRectangle and the Rectangle object created is shown in the following figure:

Constructing Objects from classes -Cont’d We can create any number of objects from the same class. For example, the following defines another object of the rectangle class.  Rectangle yourRectangle = new Rectangle(5, 5, 10, 10); An object reference can be assigned to another object reference as shown by the following:  myRectangle = yourRectangle;

Constructing Objects from classes -Cont’d The above statement makes both reference variable to point to the same object. Thus, the object previously referenced by myRectangle no longer has any reference. Once an object is not being referenced by any reference variable it becomes garbage. A Java runtime module called Garbage collector reclaims the memory occupied by the object.

Problem Solving in OO languages (Java) In the real world, problems are solved by interaction among objects. For example, if you have a problem with your car (an object), you take is to a mechanic (another object) for repairs. Similarly, in Java, problems are solved by interactions among objects. We create objects that have methods that can solve the problem and then calls the method. We call a method of an object by using the dot operator. We have been calling the println method of the System.out object in previous examples. One of the methods of the Rectangle class is translate, which moves a rectangle by a certain distance in the x- and y- direction. The following statement moves the object being reference by yourRectangle by 15 units along the x-direction and 25 along the y-direction.   yourRectangle.translate(15, 25);

Java Class Library JDK comes with thousands of classes that can be used to solve different types of problems. These classes are organized into related groups called packages. For example, the System class belongs to the java.lang package, while the Rectangle class belongs to java.awt package. Except for the classes in the java.lang package, we must import each class we wish to use in our programs using the import statement. The following program creates a Rectangle object, translate it and then print   import java.awt.Rectangle; public class MoveRectangle { public static void main(String[] args) { Rectangle myRectangle=new Rectangle(5, 5, 10, 10); myRectangle.translate(15,25); System.out.println(myRectangle); }