OOP Explanation by Galib
OOP Explanation by Galib
Encapsulation
Getter ve setter: eslinde bunlari zennimce adi methodlar ile de yazmaq olar
amma megzi cehetden set ve get achar sozleri istifade etmeliyik. Get-I bilrik
parameter almir ve s. Set metodunun achar sozle yazilish varianti ve main
ichinde ishlenmesi beledi, bu gormediyin bisheydi:
Getters and setters: Actually, I think we could write these with regular methods
too, but we need to use the get and set keywords for their meanings. We know
that a getter doesnʼt take any parameters,etc. Here is how to write a setter with
the keyword and use it in main , this might be something new for you:
OOP 1
Ve encapsulation deyende tekce getter ve setter de nezerde tutulmur enc hox
onlar on plandadi o hech, sadece megzi kodu capsule etmek, arxa planda
saxlamaq, orda mueyyen emeliyatlar apararag onde gostermemek, setter ve
getter-de etdiyimiz kimi.
The use of the set method is different from regular methods; in other words,
the setting part is not written as a typical method!
To make a method private, itʼs enough to put _ at the beginning of its name.
Inheritance
https://www.yazilimdili.net/dart-programlama/dart-dersleri/dart-nesne-
yonelimli-programlama/dart-flutter-dersleri-inheritance-kalitim-miras-alma/
https://www.javatpoint.com/dart-inheritance#:~:text=Dart inheritance is
defined as,Object-Oriented programming approach).
https://dart-tutorial.com/object-oriented-programming/inheritance-in-dart/
OOP 2
Hierarchical Inheritance In this type of inheritance, parent class is
inherited by multiple subclasses. For example, the Car class can be
inherited by the Toyota class and Honda class.
// Superclass
class Vehicle {
String brand;
Vehicle(this.brand);
void honk() {
print('$brand is honking.');
}
}
// Subclass
class Car extends Vehicle {
String model;
void displayInfo() {
OOP 3
print('Brand: $brand, Model: $model');
}
}
void main() {
// Creating an instance of Car, which inherits from Vehicle
var myCar = Car('Toyota', 'Corolla');
OOP 4
}
void main() {
// Creating an instance of Dog
var myDog = Dog();
OOP 5
// Superclass
class Animal {
final String animalName;
Animal({required this.animalName});
void eat() {
print('$animalName is eating.');
}
}
// Subclass 1
class Dog extends Animal {
Dog({required super.animalName});
void bark() {
print('$animalName is barking.');
}
}
// Subclass 2
class Cat extends Animal {
Cat({required super.animalName});
OOP 6
void meow() {
print('$animalName is meowing.');
}
}
// Subclass 3
class Bird extends Animal {
Bird({required super.animalName});
void fly() {
print('$animalName is flying.');
}
}
void main() {
// Creating instances of each subclass
var myDog = Dog(animalName: 'Buddy');
var myCat = Cat(animalName: 'Whiskers');
var myBird = Bird(animalName: 'Tweety');
OOP 7
Abstraction
https://www.javatpoint.com/dart-abstract-classes#:~:text=Abstract classes are
the classes,by using the abstract keyword.
Abstract class A class that cannot be instantiated directly and is meant to be
subclassed.
Abstract classes are the classes in Dart that has one or more abstract method.
Abstraction is a part of the data encapsulation where the actual internal
working of the function hides from the users. They interact only with external
functionality. We can declare the abstract class by using the abstract keyword.
ChatGPT explanation (for me it is very good to understand for the end):
OOP 8
Purpose of Abstraction:
Hide Details You hide internal implementation details and only expose
essential features or behaviors.
// Abstract Class
abstract class Vehicle {
// Abstract method (no body)
void startEngine(); // Subclasses must provide implementa
tion
// Subclass 1: Car
class Car extends Vehicle {
@override
void showDetails() {
super.showDetails();
print('But kind of car.');
}
@override
void startEngine() {
print('Car engine started.');
}
}
OOP 9
// Subclass 2: Motorcycle
class Motorcycle extends Vehicle {
@override
void startEngine() {
print('Motorcycle engine started.');
}
}
void main() {
// You cannot create an instance of an abstract class
// var vehicle = Vehicle(); // This would give an error
Explanation:
Abstract Class Vehicle :
OOP 10
It also provides a concrete method showDetails() , which can be used by
all subclasses without modification.
Both subclasses can also use the showDetails() method directly from
the Vehicle class.
Real-World Analogy:
Think of abstraction like a car. As a user, you donʼt need to understand how the
engine works internally to drive the car. You just need to know how to start the
engine (method), accelerate, and brake. The details of how the car performs
these actions are hidden from you.
Summary:
Abstraction in Dart allows you to define essential features (methods)
without specifying the details of how they work.
OOP 11
Abstract classes are the primary mechanism for abstraction, defining
abstract methods that subclasses must implement.
Polymorphism
Biraz geribe ve ya qarishig gele biler, chatgpt-nin izahini beyendim. Ondan
elave bizim Dependency injectionda abstract classi tip olaraq verib class olaraq
impl class-ini vermeyimiz de buna numune ola biler. Elaveden ashagi shekiller
atacam, hansi ki, Kenanin dersinde qeyd etdiyi abstract class-larla olan
temadan danishir amma eslinde polimorfizmle elaqelidi:
https://medium.com/@emanyaqoob/polymorphism-in-dart-refers-to-the-
ability-of-objects-of-different-classes-to-be-treated-as-9e9d7cc9b4da
OOP 12
Polymorphism is one of the fundamental concepts of Object-Oriented
Programming OOP, and in Dart, it refers to the ability of different classes to
provide different implementations of the same method or behavior. The
term polymorphismmeans "many forms," and it allows objects of different
classes to be treated as objects of a common superclass, enabling the same
method to behave differently depending on the object that calls it.
What is Polymorphism?
Polymorphism means "many forms." In simple terms, it allows one method
to do different things depending on the object calling it.
Key Idea:
Different classes (like Dog, Cat, etc.) can have the same method, but that
method does different things based on the class. This is what we
call polymorphism.
Purpose of Polymorphism:
Code Reusability You can write generic code that can handle objects of
different types in a unified way.
Flexibility The same function or method call can work on different types of
objects, giving the ability to extend functionality easily.
OOP 13
// Superclass
class Animal {
String animalName;
Animal(this.animalName);
// Method to be overridden
void makeSound() {
print('$animalName makes a sound.');
}
}
// Subclass 1: Dog
class Dog extends Animal {
Dog(String name) : super(name);
// Subclass 2: Cat
class Cat extends Animal {
Cat(String name) : super(name);
void main() {
OOP 14
// Creating instances of different subclasses
Animal myDog = Dog('Buddy');
Animal myCat = Cat('Whiskers');
// Polymorphism in action
myDog.makeSound(); // Output: Buddy says Woof!
myCat.makeSound(); // Output: Whiskers says Meow!
// Both myDog and myCat are of type Animal, but their beh
avior is determined by the subclass
}
Explanation:
Superclass Animal It has a method makeSound() , which is meant to be
overridden by subclasses.
Subclasses Dog and Cat Both subclasses inherit from Animal and override
the makeSound() method to provide specific sounds Woof for Dog , Meow
for Cat ).
Polymorphic Behavior In the main() function, both myDog and myCat are of
type Animal . However, when makeSound() is called, Dart automatically
invokes the overridden method from the respective subclass ( Dog or Cat ).
This demonstrates runtime polymorphism, where the behavior of the
method is determined by the object that is calling it.
Real-World Analogy:
Think of polymorphism as a family of different vehicles. Each vehicle might
have a start() method, but what happens when you start the vehicle depends
on whether it's a car, a bike, or a boat. In this way, all vehicles share the same
general interface (e.g., they can be started), but the specific implementation of
how they are started varies by vehicle type.
OOP 15
Compile-time Polymorphism In some languages, this includes method
overloading (multiple methods with the same name bu different
parameters). However, Dart does not support method overloading. So,
polymorphism in Dart is mostly focused on runtime polymorphism.
// Abstract class
abstract class Shape {
// Abstract method
void draw();
}
// Subclass 1
class Circle extends Shape {
@override
void draw() {
print("Drawing a circle.");
}
}
// Subclass 2
class Rectangle extends Shape {
@override
void draw() {
print("Drawing a rectangle.");
}
}
OOP 16
void main() {
// Creating polymorphic objects
Shape shape1 = Circle();
Shape shape2 = Rectangle();
// Polymorphic behavior
shape1.draw(); // Output: Drawing a circle.
shape2.draw(); // Output: Drawing a rectangle.
}
Explanation:
The Shape class is an abstract class with an abstract method draw() .
Summary:
Polymorphism in Dart allows you to write code that works with objects of
different classes that share a common interface or superclass.
It lets you override methods in subclasses and ensures that the correct
method implementation is called based on the actual object type at
runtime.
inheritance_main.dart
OOP 17