0% found this document useful (0 votes)
21 views

OOP Explanation by Galib

You may understand OOP accurately here.

Uploaded by

Galib
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)
21 views

OOP Explanation by Galib

You may understand OOP accurately here.

Uploaded by

Galib
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/ 17

OOP

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:

set metodunun istifadesi adi metodlardan ferqlidi yeni menimsetme hissesi


method kimi yazilmir!!

Umumi ozetleyesi olsaq encapsulation-da getter ve setter protected olunmush


datalarimiza elchatanligi mueyyen control esasinda etmeyimizi saglayir, biz get
metodunda ⇒ bununla deyilde {} bununla body achib neyise yoxlayib daha
sonra datani dondere bilerik, hemchinin set metodunda da ele.

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.

Her hansi metodu private etmekchin adinin evveline _ qoymaq kifayetdir.

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 summarize in general, in encapsulation, getters and setters allow us to


access our protected data with a certain level of control. In a get method, we
can use { } instead of => to open a body, check something, and then return
the data. We can do the same in a set method.
And when we talk about encapsulation, itʼs not only about getters and setters
—theyʼre just the most commonly used. The main idea is to hide the code, keep
it in the background, and perform certain operations without showing them up
front, just as we do with setters and getters.

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/

Types Of Inheritance In Dart


 Single Inheritance  In this type of inheritance, a class can inherit from
only one class. In Dart, we can only extend one class at a time.

 Multilevel Inheritance  In this type of inheritance, a class can inherit from


another class and that class can also inherit from another class. In Dart, we
can extend a class from another class which is already extended from
another class.

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.

 Multiple Inheritance  In this type of inheritance, a class can inherit from


multiple classes. Dart does not support multiple inheritance. For
e.g. Class Toyota extends Car, Vehicle {} is not allowed in Dart.

// Superclass
class Vehicle {
String brand;

Vehicle(this.brand);

void honk() {
print('$brand is honking.');
}
}

// Subclass
class Car extends Vehicle {
String model;

Car(String brand, this.model) : super(brand);

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');

// Accessing methods and properties from the superclass


myCar.honk(); // Output: Toyota is honking.

// Accessing methods from the subclass


myCar.displayInfo(); // Output: Brand: Toyota, Model:
}

// Base Class (Superclass)


class Animal {
void eat() {
print('Animal is eating.');
}

OOP 4
}

// Derived Class (Subclass 1)


class Mammal extends Animal {
void walk() {
print('Mammal is walking.');
}
}

// Further Derived Class (Subclass 2)


class Dog extends Mammal {
void bark() {
print('Dog is barking.');
}
}

void main() {
// Creating an instance of Dog
var myDog = Dog();

// Accessing methods from all levels of inheritance


myDog.eat(); // Inherited from Animal -> Output: Animal i
myDog.walk(); // Inherited from Mammal -> Output: Mammal i
myDog.bark(); // Defined in Dog -> Output: Dog is barking.
}

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');

// Accessing methods from the superclass and their own spec


myDog.eat(); // Output: Buddy is eating.
myDog.bark(); // Output: Buddy is barking.

myCat.eat(); // Output: Whiskers is eating.


myCat.meow(); // Output: Whiskers is meowing.

myBird.eat(); // Output: Tweety is eating.


myBird.fly(); // Output: Tweety is flying.
}

Multiple Inheritance - In this type of inheritance, a class can


inherit from multiple classes. Dart does not support multiple
inheritance. For e.g. Class Toyota extends Car, Vehicle {} is not
allowed in Dart.

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 method – govdesi olmayan methoddu. Abstract method A method


that has no body and must be overridden by a subclass.

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):

Abstraction in Object-Oriented Programming OOP is one of the key principles


and refers to hiding the complexity of a system by showing only the relevant
details and exposing what is essential. The idea is to focus on what an
object does(its behavior) rather than how it does it (its implementation details).
In Dart, abstraction is achieved mainly through:

 Abstract classes and

 Interfaces (since Dart doesnʼt have a specific keyword for interfaces,


abstract classes and regular classes can serve as interfaces).

Key Concepts of Abstraction:


Abstract Class A class that cannot be instantiated directly. It often
contains abstract methods (methods without a body) that are meant to be
implemented by subclasses.

Abstract Method A method that is declared without any implementation.


Subclasses are required to provide the concrete implementation for such
methods.

Concrete Class A subclass that provides specific implementations for the


abstract methods.

OOP 8
Purpose of Abstraction:
Hide Details You hide internal implementation details and only expose
essential features or behaviors.

Enforce a Contract When you declare abstract methods, you're enforcing


a contract for subclasses to implement certain behaviors.

Reusability Abstract classes can contain concrete methods as well,


allowing common functionality to be shared across different subclasses.

Example of Abstraction in Dart:

// Abstract Class
abstract class Vehicle {
// Abstract method (no body)
void startEngine(); // Subclasses must provide implementa
tion

// Concrete method (with body)


void showDetails() {
print("This is a vehicle.");
}
}

// 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

// Creating instances of concrete subclasses


var myCar = Car();
var myBike = Motorcycle();

// Accessing the abstract method implementation


myCar.startEngine(); // Output: Car engine started.
myBike.startEngine(); // Output: Motorcycle engine starte
d.

// Accessing the concrete method from the abstract class


myCar.showDetails(); // Output: This is a vehicle.
// Bu
t kind of car.
myBike.showDetails(); // Output: This is a vehicle.
}

Explanation:
 Abstract Class Vehicle :

It defines an abstract method startEngine() without implementation,


forcing subclasses to implement it.

OOP 10
It also provides a concrete method showDetails() , which can be used by
all subclasses without modification.

 Subclasses Car and Motorcycle :

These subclasses extend the abstract class Vehicle and provide


specific implementations for the startEngine() method.

Both subclasses can also use the showDetails() method directly from
the Vehicle class.

Benefits of Abstraction in Dart:


 Simplifies Complex Systems By focusing on what needs to be done
(abstract methods) and hiding how it's done (implementation), you make
the system easier to understand and maintain.

 Provides a Blueprint Abstract classes provide a blueprint for other


classes. They define what actions need to be performed, but leave the
details to the subclasses.

 Encourages Code Reuse Common functionality (e.g., showDetails() ) can be


shared through the abstract class, while more specific functionality
(e.g., startEngine() ) is left to the subclasses.

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.

In code, this abstraction allows developers to define a clear contract (what a


vehicle can do) without revealing how each type of vehicle (car, motorcycle,
etc.) implements these actions.

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.

This simplifies code, promotes reusability, and ensures that subclasses


adhere to a defined contract.

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.

Example of Polymorphism in Dart:

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);

// Overriding the makeSound method


@override
void makeSound() {
print('$animalName says Woof!');
}
}

// Subclass 2: Cat
class Cat extends Animal {
Cat(String name) : super(name);

// Overriding the makeSound method


@override
void makeSound() {
print('$animalName says Meow!');
}
}

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.

Types of Polymorphism in Dart:

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.

 Runtime Polymorphism This is the most common form of polymorphism in


Dart, achieved through method overriding and dynamic method dispatch.
The method that gets executed is determined at runtime, based on the type
of the object calling the method (as shown in
the myDog and myCat examples).

Another Example: Using Abstract Classes for Polymorphism


You can also use abstract classes to enforce polymorphic behavior, where
subclasses must implement certain methods:

// 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() .

Both Circle and Rectangle provide their own implementation of


the draw() method.

At runtime, the correct draw() method is called based on whether the


object is a Circle or Rectangle .

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.

Polymorphism increases flexibility and reusability in code by allowing


objects of different types to be treated in a uniform manner.

inheritance_main.dart

OOP 17

You might also like