0% found this document useful (0 votes)
2 views13 pages

Lecture 4

The document discusses the concepts of classes and objects in software development, defining a class as a blueprint that includes data and functions, while an object is an instance of a class. It explains the usage of constructors, including default and parameterized constructors, to assign values to instance variables. The document also provides examples of class notation and object creation in C#.

Uploaded by

used.subtype-4e
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

Lecture 4

The document discusses the concepts of classes and objects in software development, defining a class as a blueprint that includes data and functions, while an object is an instance of a class. It explains the usage of constructors, including default and parameterized constructors, to assign values to instance variables. The document also provides examples of class notation and object creation in C#.

Uploaded by

used.subtype-4e
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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..!!

You might also like