
Play Store Application link – Java to Dart in 10 Steps – App on Google Play
Welcome to our exploration of object-oriented programming (OOP) in Dart! If you’re coming from a Java background, you’ll find Dart’s approach to OOP quite familiar. In this post, we’ll cover classes and objects, inheritance, polymorphism, and encapsulation in Dart, with comparisons to Java to make the transition smooth.
1. Classes and Objects
Defining Classes
In Dart, defining a class is similar to Java. A class is a blueprint for creating objects. Here’s how you define a class in Dart:
- Java:
public class Car {
String brand;
int year;
public Car(String brand, int year) {
this.brand = brand;
this.year = year;
} public void displayInfo() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}
- Dart:
class Car {
String brand;
int year;
Car(this.brand, this.year);
void displayInfo() {
print('Brand: $brand, Year: $year');
}
}
In Dart, you define a class and its constructor with a more concise syntax. The this
keyword is used in the constructor to initialize the class members, similar to Java.
Creating Objects
Creating objects in Dart is straightforward:
- Java:
Car myCar = new Car("Toyota", 2020);
myCar.displayInfo();
- Dart:
var myCar = Car('Toyota', 2020);
myCar.displayInfo();
In Dart, you use the new
keyword for object creation, but it’s optional. Dart’s syntax for object creation is more streamlined.
2. Inheritance
Extending Classes
Inheritance allows you to create a new class based on an existing class. Here’s how you extend a class in Dart:
- Java:
public class ElectricCar extends Car {
int batteryLife;
public ElectricCar(String brand, int year, int batteryLife) { super(brand, year);
this.batteryLife = batteryLife;
}
@Override
public void displayInfo() {
super.displayInfo();
System.out.println("Battery Life: " + batteryLife + " hours");
}
}
- Dart:
class ElectricCar extends Car {
int batteryLife;
ElectricCar(String brand, int year, this.batteryLife) : super(brand, year);
@override
void displayInfo() {
super.displayInfo();
print('Battery Life: $batteryLife hours');
}
}
In Dart, you use the extends
keyword to create a subclass, and the super
keyword to call the superclass constructor and methods. The @override
annotation is used to indicate that a method is being overridden.
Overriding Methods
Overriding methods allows subclasses to provide specific implementations of methods defined in the superclass:
- Java:
@Override
public void displayInfo() {
System.out.println("Electric Car Info");
}
- Dart:
@override
void displayInfo() {
print('Electric Car Info');
}
3. Polymorphism
Method Overloading and Overriding
Dart does not support method overloading (having multiple methods with the same name but different parameters), but it does support method overriding:
- Java (Method Overloading):
public void displayInfo() {
System.out.println("No parameters");
}
public void displayInfo(String info) {
System.out.println(info);
}
- Dart (No Method Overloading):
void displayInfo() {
print('No parameters');
} // No overloaded methods in Dart
Dart uses method overriding to provide specific implementations for inherited methods.
Abstract Classes and Interfaces
Abstract classes and interfaces in Dart are used to define contracts for subclasses:
- Java:
public abstract class Animal {
public abstract void makeSound();
}
public class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
- Dart:
abstract class Animal {
void makeSound();
}
class Dog implements Animal {
@override
void makeSound() {
print('Woof!');
}
}
In Dart, abstract classes are defined using the abstract
keyword. Dart also uses implements
to implement an interface, as opposed to Java’s implements
keyword.
4. Encapsulation
Getters and Setters
Encapsulation is about hiding the internal state of an object and requiring all interaction to be performed through an object’s methods. Dart provides getters and setters to manage this:
- Java:
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
- Dart:
class Person {
int _age;
int get age => _age;
set age(int value) => _age = value;
}
In Dart, you use get
and set
to define getter and setter methods. Dart’s syntax is more concise, using the arrow syntax for single-line getters and setters.
Private and Public Members
In Dart, you use an underscore (_
) before a member name to make it private to its library:
- Java:
private int age;
- Dart:
class Person {
int _age; // Private to the library
}
Members with an underscore in Dart are private to the library, providing encapsulation.