Monday 29 July 2013

02: classes and objects


Learning Objectives
After completing this session, you will be able to:
  Describe OOP as a programming methodology
  Explain classes and objects
Object Oriented Programming
OOP maps your problem in the real world.
OOP defines “things” (objects), which can either do something or have something done to them.
OOP creates a “type” (class) for these objects sothat you do not have to redo all the work in
defining the properties and behavior of an object.
Examples of class:
  Classroom
  Car
  Person
Procedural Versus OOP
“Nouns” refer to data and “Verbs” refer to operations.
Procedural Languages:
  C or Pascal and so on
  Verb-oriented
  No formal noun-verb structure (not enforced by language or compiler)
OOP languages:Operations (verbs) are performed by or on “Actors” (objects), which have names and store data (nouns)
Objects
An object is a unique programming entity that has attributes to describe it (like adjectives in grammar) and methods to retrieve or set attribute values (like verbs in grammar).
Part of a program which:
  Models some real or conceptual object
  Has behavioural responsibilities (behaviors)
  Has informational responsibilities (attributes)
  Behaviors (methods):
  Things an object can “do”
  Like procedures and functions in other languages
  Attributes (fields):
  Information an object “knows” (has-a)
  Like data and variables in other languages (records)


Class
Exists once:
The class is the template for the object
Defines the attributes and behavior of the objects
Every object must belong to a class: The creation (construction) of an object is called instantiation.
The created object is often called an instance (or an instance of class X)
Example:
Person is a class
EdmundHillary is an instance of class Person
Try It Out
Problem Statement:
What is the difference between a class and an object?
Answer:
A class is not an object. But it is used to construct objects.
A class is a blueprint for an object. It tells the virtual machine how to make an object of that particular type. Each object made from the class can have its own values for the instance variables of that class.
ClassBook {
String id;
String title;
String author;
void getChaptersList() {
System.out.println(“Getting the chapters list”);
}
public static void main(String[] args) {
Book book1 = new Book();
book1.getChaptersList();
Book book2 = new Book();
book2.getChaptersList();
}
}

How It Works:
In the example provided, you have used the Bookclass to make different books, and each book
has its own id, title, and author.
Tips and Tricks
Please tell whether the following file could be compiled:
Class DiscDeck {
boolean canRecord = false;
void playDisc() {
System.out.println(“disc playing”);
}
void recordDisc() {
System.out.println(“disc recording”);
}
public static void main(String[] args) {
dd.canRecord = true;
dd.playDisc();
if (dd.canRecord == true) {
dd.recordDisc();
}
}
}
Solution:The preceding file will result in compilation error since object is not created for the DiscDeck class. This error will be fixed by adding
 DiskDeck dd = new DiscDeck(); 
statement in the main method.
Summary
  Object-oriented programming lets you to extend a program without having to touch the working code that is tested earlier.
  A class describes how to make an object of that class type. A class is like a blueprint.
  An object knows things and does things.
  The things an object knows about itself are called instance variables. They represent the state of an object.
  The things an object does are called methods. They represent the behaviorof an object.

No comments:

Post a Comment