APPLICATION
SOFTWARE
DEVELOPMENT
~ THARUKA PERERA
Lecture 04
Foundation Certificate in IT – UOB Batch
CLASSES AND OBJECTS
What Is a Class?
CONTENT What Is an Object?
Usage of constructor
CAR?
WHAT IS A CLASS?
A data structure that includes both data
and functions
A template for objects that share Data •Color, Model, Manu.Year, Brand
common characteristics.
•Drive() , Reverse(), Speed(),
Functions
Simply a class is a blueprint that Break()
defines the variables and the methods
common to all objects of a certain kind.
CLASS NOTATION
class ClassName
{
//Fields, operations and properties go here
...
}
EXAMPLE:
class Car
{
string color, model, brand;
int year;
void drive();
void speed();
}
WHAT IS AN OBJECT?
An object is an instance of a class
When a program is executed, the objects interact by sending messages to one another
Objects exhibit:
Identity: Objects are distinguishable from one another
Behaviour: Objects can perform tasks
State: Objects store information
OBJECTS
An object is created in the memory using the keyword ’new’ and is referenced by an identifier called a "reference".
Example: Student Object1 = new Student();
Student Object2 = new Student();
Student Object3 = new Student();
//Creating an object
ASSIGNING VALUES TO
Rectangle rect1 = new Rectangle();
VARIABLES USING
METHODS //Calling the method using the object
rect1.setData(15,10);
Rect1 Object
Rectangle Class
Rect2 Object
Purpose of a constructor is to assign values to
the instance variables at the runtime.
✓ Constructor has the same name as the
class
CONSTRUCTOR ✓ No return type
C# has two constructors
1. Default constructor
2. Parameterized constructor.
C# DEFAULT CONSTRUCTOR
constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
public class Car {
int modelYear;
String modelName;
Default Constructor
by default added by
public Car() { the C# compiler
}
}
PARAMETERIZED CONSTRUCTOR.
Purpose of a constructor is
public class Car {
to assign values to the
int modelYear; instance variables at the
runtime
String modelName;
public Car(int year, String name) {
modelYear = year;
modelName = name;
}
Let’s Discuss this at the Labs
SEE YOU ON
NEXT WEEK..!!