Dart Program in OOPs concepts - TechXplore

Thursday, 2 March 2023

Dart Program in OOPs concepts

 Object-Oriented Programming is a programming paradigm that uses objects to represent and manipulate data and behavior in software systems.



1.Class & Object:

In Object-Oriented Programming (OOP), a class is a blueprint or template that defines the characteristics and behavior of a category of objects. It specifies the attributes (data) and methods (functions) that an object of that class should have.

examples:

class Student{

  var name;

  var rollno;

  var age;

  var mark;

  void display(){

    print('Student name is $name');

    print('Student rollono is $rollno');

    print('Student age is $age');

    print('Student mark is $mark');

  }

}

void main(){

Student student1=new Student();

student1.name='Raja';

student1.rollno=12;

student1.age=21;

student1.mark=97;

student1.display();

}

output:

Student name is Raja

Student rollono is 12

Student age is 21

Student mark is 97

2.constructor:

A constructor is a special method used to initialize an object. It is called automatically when an object is created, and it can be used to set the initial values for the object’s properties. For example, the following code creates a Person class object and sets the initial values for the name and age properties.

examples:

class Student{

  var name;

  var rollno;

  var age;

  var mark;

  Student(String name,int rollno, int age,int mark){

    this.name=name;

    this.rollno=rollno;

    this.age=age;

    this.mark=mark;

}

void display(){

  print('Student name is $name');

  print('Student rollno is $rollno');

  print('Student age is $age');

  print('Student is mark $mark');

}

}

void  main(){

Student std=new Student('Raja', 38, 21,98);

std.display();

}

output:

Student name is Raja

Student rollno is 38

Student age is 21

Student is mark 98

3.Inheritance:

Inheritance is a mechanism that allows objects to inherit properties and behaviors from a parent class. It enables code reuse and helps to create a hierarchy of classes where the child class inherits properties and methods from its parent class.

examples:

class college{

  var name='Raj';

  var roll=33;

  var addres='7007';

}

class Student extends college{

  var mark;

  var code;

  Student(var mark,var code){

    this.mark=mark;

    this.code=code;

}

  void display(){

    print('Student name is $name');

    print('Student roll is $roll');

    print('Student address $addres');

    print('Student mark is $mark ');

    print('Student code is $code');

  }

}

void main(){

Student std=new Student(88, 2001);

std.display();

}

output:

Student name is Raj

Student roll is 33

Student address 7007

Student mark is 88 

Student code is 2001

4.Polymorphism: 

Polymorphism means the ability to take many forms. In object-oriented programming, polymorphism allows objects to take different forms and behave differently depending on the context. Polymorphism can be achieved through method overriding or method overloading.

examples;

class first{

void displaying(){

  print('The first class runing');

}

}

class second extends first{

  void displaying(){

    print('The second class running');

  }

}

void main(){

second sd=new second();

sd.displaying();

first sdd=new first();

sdd.displaying();

}

output:

The second class running

The first class runing

5.Abstraction:

 Abstraction refers to the process of simplifying complex systems by breaking them down into smaller, more manageable components. In object-oriented programming, abstraction means defining an abstract class or interface that specifies a set of methods without actually providing their implementation. This allows the programmer to focus on the high-level design of the system without worrying about the implementation details.

examples:

abstract class Student{

  void mins();

  void hii(){

    print('running student class');

  }

}

class college extends Student{

  void mins(){

    print('running college');

  }

}

void main(){

var std=new college();

  std.hii();

  std.mins();

}

output:

running student class

running college

6.Encapsulation: 

Encapsulation refers to the practice of hiding the internal details of an object and providing access to its functionalities through a well-defined interface. It ensures that the object's internal state remains hidden from the outside world and can only be accessed through a set of methods or functions.

examples;

class Employee{

  var id;

  var name;

  int getId(){

     return id;

  }

  String getName(){

    return name;

  }

void setId(int id){

    this.id=id;

  }

  void setname(String name){

    this.name=name;

  }

 }

void main(){

Employee emp=new Employee();

emp.setId(1);

emp.setname('Raja');

print('The id is : ${emp.getId()}');

print('The name is : ${emp.getName()}');

}

output:

The id is : 1

The name is : Raja

7.Enum:

An enum is a special type that represents a fixed number of constant values. An enum is declared using the keyword enum followed by the enum’s name.

examples;

enum days {

 sunday,

 monday,

 thues,

 wed,

 thurs,

 frid,

 sat,

}

void main(){

var today=days.sunday;

switch(today){

 case days.monday:{

    print('The Day is monday');

    break;

  }

  case days.thues:{

    print('The Day is thues');

    break;

  }

  case days.wed:{

    print('The Day is wed');

    break;

  }

  case days.thurs:{

    print('The Day is thurs');

    break;

  }

  case days.frid:{

    print('The Day is frid');

    break;

  }

  case days.sat:{

    print('The Days is Sat');

    break;

  }

  case days.sunday:{

    print('The Day is sunday');

     break;

  }

}

}

output:

The Day is sunday



No comments:

Post a Comment