Dart Classes and Object

Add Your Heading Text Here

Almost every OOP languages uses Classes and Objects concepts to organise program code or Syntax Structure in a way that is helpful to achieve smoother code structure, better flow control, code reusability, more efficiency , testability and many more features.

Since dart is also an object oriented programming language. Therefore it’s not an exception to class-object code structure.

Classes and Objects are Interconnected. They can’t be distinguished from each other. So, we’ll discuss class and objects relatively to each other.

Class

A class is a blueprint or template used to construct the objects. A class consists of different variables, constructors, objects and methods. It can have many properties and methods that are common to the objects of that particular class.

A class is a Structure which tells us that what will be the behaviour of the object and what things will an object contain?

Objects

Object is an entity which represents state and behaviour.

Object is the instance of the class. In simple words, object is the specific type or kind of the class.

Let’s understand the class and objects terminologies by our day-to-day examples.

For ex :

If Car is a Class then what can be the objects of the class Car ?

So, we can think like, the properties and methods(behaviours) satisfied by any entity can be the objects of the class.

So, what can be the states of a Car Class ?

A car can have color, Wheels, Fuel Level, Brake , Accelarator etc.

Now, what can be the Behaviour for a Car class?

A car can run at high speed, it can horn, it can turn right or left, reverse turn etc. are the behaviours of that class.

So, the entities which has state(properties) and behaviour of the Car class are the objects of the class.

So, Maruti 800, Suzuki Swift, Chevrolet cruze, Audi A6, Buggatti Veyron, Aston Martin and many more are the examples of the Car class.

Let’s take another example to understand more briefly the meaning of the objects and the class.

Let’s say If Person is a class then what can be the objects for the person class.

Now, to understand what can be the objects of a Person class we can think of the properties and behaviours of a Person.

Properties of a Person are

2 Legs, 2 Eyes, 2 Hands, 2 Ears, a Nose, a Mouth, a Breakable Heart, Many More Bones and also you can think other human properties as well to understand this class and object concept.

Behaviours of a Person are

A person can eat, think, drink, speak, play, run and many more behaviours you can imagine of.

So, Now the elements or entities in the universe who can fulfill the properties and behaviours of the person class are the objects of the Person class.

You and I are also satisfy the person class properties. so, we are also the objects of the person class.

This is the simplest way to undestand and imagine the Class-Object Concept.

To write a program In object oriented dart programming langauge, the class structure is required to simplify the program.

Class Syntax :

class ClassName{

//variables
// constructors
// methods

}

Class keyword is required to declare a class structure. and an object is required to access the states(properties) or behaviours(methods in case of programming) of a class.

For creating object or instance of class like below,

Syntax :

ClassName objectName=new ClassName();

Here, new keyword is used to create an Instance or object of a particular class. And then we can access or say use the properties and methods of that particular class.

To access any property or methods of a class a . operator is used. It is also known as Member Access operator.

For ex :

class Test{
  int a=5;			// a is an instance variable
}

void main() {
  Test test=new Test();	// new keyword is used to create instance of a class.
  print(test.a);		// “.” is a Member Access operator
}

Output :

5

this is the basic Introduction to the Class and Object.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Show Buttons
Hide Buttons