You are on page 1of 51

OOP

Java SE
Contents
• What is OOP?

• Class & Objects

• Major Concepts of OOP

• Abstract Class

• Interface
What is OOP?
➢OOP ဆiuသည&မ() အပလ-eက01င&0တစ&ခu အတ6င&0မ()ပ7ဝင&eသ)
အခ9က&အလက&မ9)0အ)0 Object အeန;ဖင&= >က?&=;မင&@ပ-0၊ အဆiuပ7 Object မ9)0

အခ9င&0ခ9င&0 ဆက&သ6ယ&လuပ&eဆ)င&;ခင&0အ)0;ဖင&= အသuC0ခ9နiuင&eသ)

အပလ-eက01င&0တစ&ခuအ)0 တည&eဆ)က&eပ0Diuင&eသ) နည&0လမ&0 ;ဖစ&ပ7သည&။

➢Object မ9)0အခ9င&0ခ9င&0ဆက&သ6ယ& လuပ&eဆ)င&eစ;ခင&0 အ)0;ဖင&= လuပ&eဆ)င&ရန&

ရည&G6ယ&ခ9က&မ9)0အ)0 ;ဖစ&e;မ)က&eအ)င& eဆ)င&G6က&eစ;ခင&0 ;ဖစ&ပ7သည&။

➢တစ&ခ7 eရ0@ပ-0eသ) Object မ9)0အ)0 ;ပန&လည& အသuC0;ပu;ခင&0အ)0;ဖင&= Application


Development Cost အ)0 မ9)0စ6)သက&သ)eစDiuင&ပ7သည&။
What is OOP?
Why we use OOP

Easy to Easy to Easy to


Create Maintain Enhance
Object & Class
Object & Class
• Object တစ&ခuသည& ၎င&0၏အe;ခအeနအ)0 eဖ)&;ပDiuင&eသ) Properties
D(င&= အ;ပuအမKအ)0 eဖ)&;ပeပ0Diuင&eသ) Method
မ9)0အ)0ပiuင&ဆiuင&ပ7သည&။

• Class တစ&ခuသည& Object တစ&ခu၏ ပiuင&ဆiuင&သင&=eသ) Properties


မ9)0D(င&= Methods မ9)0အ)0 စuစည&0eဖ)&;ပeပ0ထ)0eသ) Unit တစ&ခu
;ဖစ&ပ7သည&။

• Class တစ&ခuတ6င& eဖ)&;ပထ)0eသ) အခ9က&အလက&မ9)0အ)0


;ဖ?&=စ6က&eပ0;ခင&0အ)0;ဖင&= Objectမ9)0အ)0 တည&eဆ)က&Diuင&@ပ-0 အဆiuပ7
Object မ9)0က အခ9င&0ခ9င&0 ဆက&သ6ယ& အလuပ&လuပ&;ခင&0အ)0;ဖင&= Java
Program တစ&ခuအ;ဖစ& အeက)င&အထည&eဖ)&သ6)0Diuင&မည& ;ဖစ&ပ7သည&။
OO Concept in Java

Encapsulation

Inheritance

Polymorphism
Encapsulation
• Encapsulation ဆiuသည&မ() Object တစ&ခu၏ အတ6င&0ပiuင&01i member မ9)0အ)0
;ပင&ပမ( Access လuပ&လiuမရeအ)င& eဆ)င&G6က&ထ)0Diuင&မN=ကiu ဆiuလiuပ7သည&

• ဥပမ)အ)0;ဖင& က6န&;ပPတ) တစ&လuC0အ)0 ပ7ဝ7ဖ6င&=လQင& အသuC;ပuDiuင&@ပ-0


ပ7ဝ7ပiတ&လQင& မ-0ပiတ&မည&မ(န&0 အသuC0;ပuသKတiuင&0 သi1iနiuင&ပ7သည&

• သiuရ)တ6င& အတ6င&0ပiuင&0တ6င&မည&သiu အလuပ&လuပ&မည&၊ မည&သည& အရ)မ9)0


ပ7ဝင&သည& ဆiuသည&ကiu သi1iနiuင&မည& မဟuတ&eပ

• ထiuကS=သiu ;ပင&ပသiu ဆက&သ6ယ&Diuင&မ?&= အရ)ကiuသ) ;ပသထ)0@ပ-0


မပတ&သက&သ?&= အရ)မ9)0ကiu ဖuန&0က6ယ&ထ)0Diuင&;ခင&0အ)0;ဖင&= Object မ9)0သည&
မiမi၏ အeနအထ)0ကiu မ(န&ကန&မN=ရ1ieအ)င& eဆ)င&G6က&ထ)0Diuင&ပ7သည&
Why we need
encapsulation?
public class User {
int age;
String name;
}
public class Test {
public static void main(String [] args) {
User user = new User();
user.age = -10; Cannot protect state
} of User Object

}
How to do Encapsulation?
• OOP တ6င& Object တစ&ခu၏ Properties မ9)0D(င&= Methods မ9)0အ)0
ဆက&သ6ယ&အသuC0;ပuDiuင&ခ6င&=ကiu Access Modifier မ9)0အ)0 အသuC0;ပu၍
သက&မ(တ&Diuင&၏။
Access Modifiers
Access Modifier Description
public မည&သ?&=eနရ)က မဆiu ဆက&သ6ယ&
အသuC0;ပuDiuင&သည&
protected Inheritant လuပ&ထ)0eသ) Object
မ9)0မ(ဆက&သ6ယ&Diuင&သည&
default Package တစ&ခuအတ6င&01i Object
မ9)0မ(ဆက&သ6ယ&Diuင&သည&
private လက&1i Object အတ6င&0မ(တပ70
ဆက&သ6ယ&Diuင&မည& မဟuတ&eပ
Encapsulation of an object
• Encapsulation ကiu eက)င&0စ6) eဆ)င&G6က&ထ)0eသ) Object မ9)0သည&
၎င&0တiu၏ အတ6င&0ပiuင&01i အစiတ&အပiuင&0မ9)0ကiu ;ပင&ပမ( မ;မင&Diuင&eအ)င&
eဆ)င&G6က&ထ)0eလ=1i၏

• ဆiuလiuသည&မ() Instance Variable မ9)0D(င&= အတ6င&0ပiuင&0မ()သ) အသuC0;ပuမ?&=


Method မ9)0ကiu private access modifier မ9)0ကiu အသuC0;ပuက)
ဖuန&0က6ယ&ထ)0@ပ-0 အသuC0;ပueစလiuသ?&= method မ9)0ကiu public လuပ&ထ)0ပ7သည&

• အဆiuပ7 method မ9)0မ( Variable မ9)0ကiu ဆက&သ6ယ& အသuC0;ပueစပ7သည&

• Variable မ9)0၏ တန&ဖiu0မ9)0ကiu Reference လuပ&Diuင&ရန& setter (Accessor)


method မ9)0ကiu ဖန&တ-0eပ0ထ)0@ပ-0၊ Variable မ9)0၏တန&ဖiu0မ9)0ကiu Update
လuပ&Diuင&ရန& setter (Mutator) method ကiu ;ပင&ဆင&ထ)0eလ=1iပ7သည&
Inheritance
Inheritance
• Class မ9)0သည& အ;ခ)0eသ) Class တစ&ခuမ( တဆင&= မ9iu0Giu0ဆက&ခCDiuင&@ပ-0၊
ဤကS=သiu မ9iu0Giu0ဆက&ခCလiuက&ပ7က Super Class ၏ Properties မ9)0D(င&= Method
မ9)0အ)0 Sub Class မ(တဆင&= အလiuအeလQ)က& အသuC0;ပuDiuင&မည& ;ဖစ&၏။

• Java ဘ)သ)ရပ&တ6င& တစ&ခuတည&0eသ) Class မ(သ)


မ9iu0Giu0ဆက&ခCပiuင&ခ6င&=1ieသ)&လည&0၊ အဆင&=ဆင&= မ9iu0Giu0ဆက&ခCပiuင&ခ6င&=1iပ7သည&။

• Inheritance အ)0 အသuC0;ပuပ7က Class အခ9င&0ခ9င&0 ပတ&သက&မN=အ)0


တiu0ပ6)0eစeသ)e>က)င&= Properties မ9)0D(င&= Methods မ9)0အ)0 အသuC0;ပuDiuင&သည&
ဆiuသ?&= အခ9က&တစ&ခuတည&0D(င&= Inheritance အ)0 အသuC0မ;ပuသင&=eပ။

• အမ9iu0အစ)0တK Class မ9)0အ)0 အuပ&စuဖ6S=လiuသ?&= အခ7မ9iu0တ6င&သ) Inheritance


အ)0 အသuC0;ပuသင&=ပ7သည&။
IS-A Relationship
• Class A ဟ) Class B ကiu Inheritance လuပ&လiuလ9င& eအ)က&ပ7 အတiuင&0eရ0သ)0ရမ()
;ဖစ&ပ7တယ&

class A extends B {}

• ထiuသiu extends လuပ&လiuက&;ခင&0အ)0;ဖင&= Class A D(င&= B အ>က)0မ() IS-A Relationship


ကiu ;ဖစ&ပ670eစ@ပ-0၊ Class A အ)0 class B အeနနS= အသuC0;ပuDiuင&မ() ;ဖစ&ပ7တယ&

• တဖန& class B မ()1iတS= Public, Protected D(င&= Package တစ&ခu အတ6င&0မ()1iပ7က


Default members eတ6ကiu လည&0 အသuC0;ပuDiuင&ပ7လiမ&=မယ&

• ထiuအ;ပင& Super Class အတ6င&0မ() eရ0သ)0ထ)0တS= Abstract Method DS= Default


Constructor မဟuတ&တS= Constructor သ)1iပ7က ၎င&0တiuကiu လS Override
လuပ&ဖiuဒ7မ(မဟuတ& မiမi၏ Constructor ထSမ(eန super ပ7ဝင&eသ) Argument အတiuင&0
eခXဆiueပ0ဖiu လiuအပ&လ)ပ7တယ&
Method Inheritance
• Class Shape မ() getArea
Shape ဆiuတS= Method 1i@ပ-0၊
showArea() Rectangle ကeန Shape ကiu
extends လuပ&လiuက&တS=အခ7
Rectangle DS= Shape >က)0မ()
Rectangle
IS-A Relationship
က;ဖစ&eပXeစတS=အတ6က&၊
Rectangle ဟ) Shape GS=
showArea ကiu အသuC0;ပuDiuင&မ()
;ဖစ&ပ7တယ&
Shape Class
public class Shape {
protected double base;
protected double height;
public Shape(double base, double height) {
this.base = base;
this.height = height;
}
public void showArea() {
System.out.println(base * height);
}
}
Rectangle Class
public class Rectangle extends Shape {
public Rectangle(double base, double height) {
super(base, height);
}
}
public class Test {
public static void main(String [] args) {
Shape s = new Rectangle(20, 10);
s.showArea();
}
}
Method Overloading

• Java ဘ)သ)ရပ&တ6င& Method တစ&ခuD(င&=တစ&ခuအ)0 ခ6S;ခ)0သတ&မ(တ& Diuင&ရန& အတ6က& အမည&၊


Arguments D(င&= Return ပuCစCတiu;ဖင&= ခ6S;ခ)0သတ&မ(တ& Diuင&ပ7သည&။

• တဖန& အမည&တK၍ Argument Type D(င&= အeရအတ6က& မတKည-eသ) Method မ9)0အ)0


eရ0သ)0Diuင&@ပ-0 ဤကS=သiu eရ0သ)0ခ9င&0အ)0 Overload ဟu eခXဆiuပ7သည&။
Method Overriding
• Extends လuပ&မS= Triangle ဟ) Shape GS=
Shape showArea ကiu အသuC0မ;ပuလiuပ7က၊ eဖX;ပပ7
အတiuင&0 showArea ကiu Triangle အတ6င&0မ()
showArea()
ကiuယ&ပiuင& eရ0သ)0Diuင&ပ7တယ&

• ထiuသiueရ0သ)0ထ)0;ခင&0အ)0;ဖင&= Triangle
Triangle Object GS= showArea ကiu eခXဆiuတS=အခ7မ()
ကiuယ&ပiuင& method ကiu သ) အသuC0;ပuမ()
showArea() ;ဖစ&ပ7တယ&

• Super Class ထSမ()1iတS= method DS= ထပ&တK


method တiuကiu Sub Class အတ6င&0မ()
eရ0သ)0;ခင&0ကiu Overriding လiueခXပ7တယ&
Triangle Class
public class Triangle extends Shape {
public Rectangle(double base, double height) {
super(base, height);
}
@Override
public void showArea() {
System.out.println((base * height) / 2);
}
}
Test Class
public class Test {
public static void main(String [] args) {
Shape s = new Triangle(20, 10);
s.showArea();
}
}
Type Casting
Employee Employee emp = new Programmer();

extends
Programmer prog = emp;

Programmer Programmer prog = (Programmer)emp;


Designing Object
• eဖX;ပပ7 အတiuင&0 takeShower ကiu
ပiuင&ဆiuင&eသ) Shower class က
1iပ7သည&

• တဖန& BathRoom class တ6င&လS


takeShower method ကiu
eရ0သ)0ရန& လiuအပ&လ)ပ7သည&

• BathRoom အ)0 Shower ကiu


extends လuပ&;ခင&0ဟ)
သင&=eတ)&ပ7မည&eလ)
HAS-A Relationship
• စY&0စ)0>က?&=မည& ဆiuလ9င& BathRoom သည& Shower
အမ9iu0အစ)0 မဟuတ&eသ)e>က)င&= Shower အ)0 extends
မလuပ&သင&=eပ

• BathRoom အတ6င&0တ6င& Shower 1i@ပ-0 eရခ9iu0လiuပ7က


Shower ကiu သuC0@ပ-0 eရခ9iu0ခiuင&0သည&က သဘ)ဝက9ပ7သည&

• ထiuကS=သiuပတ&သက&ပuCအ)0 HAS-A Relationship ဟu


eခXပ7သည&
HAS-A Relationship
public class Shower {

public void takeShower() {

System.out.println( I am taking Shower );

BathRoom has
a Shower
public class BathRoom {

private Shower shower;

BathRoom use
public void takeShower() { shower to take
shower.takeShower(); Shower
}

}
Polymorphism
Override အ)0အသuC0;ပu၍

အမ9iu0အစ)0 တKeသ)&လည&0

အ;ပuအမKမတKည-eသ)

ပuCစCက6Sမ9)0အ)0 eရ0သ)0Diuင&@ပ-0၊

ဤကS=သiueရ0သ)0Diuင&မN=အ)0

Polymorphism ဟu

eခXဆiuပ7သည&။
Inheritance
Hierarchy
Abstract
Class
Abstract Class
• Object တစ&ခuတ6င& ပiuင&ဆiuင&သင&=သ?&= အခ9က&အလက&D(င&=
အ;ပuအမKတiuအ)0 သeဘ)တရ)0 အeနန(င&= eရ0သ)0eစလiuသ?&=
အခ7မ9iu0တ6င& abstract keyword အ)0 အသuC0;ပu၍
eရ0သ)0Diuင&ပ7သည&။

• Abstract Class သည& သeဘ)တရ)0အရသ) တည&1ieသ)e>က)င&=


၎င&0တiuအ)0 Object အ;ဖစ& တည&eဆ)က&၍ မရနiuင&eပ။

• Abstract Class အ)0 အမ9iu0အစ)0တKeသ) Class မ9)0အ)0


တစ&မ9iu0စ)0တည&0 အeနန(င&= အသuC0;ပuလiuသ?&= အခ7မ9iu0တ6င&
eရ0သ)0အသuC0;ပuDiuင&ပ7သည&။
Abstract Class
public abstract class Shape {
protected double height;
protected double base;
public Shape(double height, double base) {
this.height = height;
this.base = base;
}

public abstract void showArea();


}
Abstract Method
• Abstract Method ဆiuသည&မ() ဘ)ကiu လuပ&Diuင&မလS ဆiuတS= Method Signature
သ) ပ7@ပ-0၊ ဘယ&လiuလuပ&eဆ)င&မလS ဆiuတS= Method Body မပ7eသ) Method
မ9)0;ဖစ&>ကပ7သည&

• Abstract Method မ9)0ကiu Abstract Class မ9)0တ6င&သ) eရ0သ)0Diuင&ပ7သည&

• Abstract Class မ9)0ကiu extends လuပ&ထ)0eသ) Class မ9)0တ6င& Abstract


Class တ6င&ပ7ဝင&eသ) Abstract Method မ9)0ကiu
အ;ပ?&=အစuCeရ0သ)0ရမည&;ဖစ&ပ7သည&

• ဥပမ)အ)0;ဖင&= Animal တiuင&0သည& အစ)စ)0တတ&ရမည&

• မည&သiuစ)0မည& ဆiuသည&ကiu ၎င&0တiuအ)0 extends လuပ&ထ)0eသ) Class မ9)0တ6င&


eရ0သ)0ရမည& ;ဖစ&ပ7သည&
Extending Abstract Class
public class Rectangle extends Shape {
public Rectangle(double height, double base) {
super(height, base);
}
@Override
public void showArea() {
System.out.println(base * height);
}
}
Object Creation from
Abstract Class

From Instance
By Anonymous
of It’s Sub
Class
Classes
By instance of it’s sub class

public class Test {


public static void main(String [] args) {
Shape rect = new Rectangle();
rect.showArea();
}
}
by anonymous class
public class Test {
public static void main(String [] args) {
Shape triangle = new Shape(20, 10) {
@Override
public void showArea(){
System.out.println((base * height)/2);
};
triangle.showArea();
}
}
If you want to
take relationship
within different
Hierarchy
Multiple Inheritance
• ဒ-eနရ)မ() Multiple Inheritance ကiu စY&0စ)0မiပ7လiမ&=မယ&

• Pet ဆiuတS= Abstract Class တစ&ခuကiu eဆ)က&@ပ-0 Dog DS= Cat


ကiu Extends လuပ&ခiuင&0လiuက&ရင& အစY&e;ပပ7လiမ&=မယ&

• ဒ7eပမS= Java မ() Class eတ6ဟ) Multiple Inheritance ကiu


လက&မခCပ7ဘK0

• ဘ)လiuလSဆiueတ)= Multiple Inheritance ဟ) Deadly


Diamond of Dead ;ပဿန)ကiu ;ဖစ&ပ6)eစDiuင&တS= အတ6က&ပ7ပS
Optional is Interface
• Interface ဟ) 100% Abstract Class ;ဖစ&တယ&

• Interface ထSမ() Abstract Method မ9)0သ) eရ0Diuင&တယ&

• Interface ကiu implements လuပ&ထ)0တS= Class မ9)0မ(တဆင&= Interface Object


ကiu တည&eဆ)က&Diuင&ပ7တယ&

• Class တစ&ခuဟ) Class တစ&ခuကiuသ) Extends လuပ&Diuင&eသ)&လည&0 Interface


ဟ) Interface တစ&ခuထက&မက Extends လuပ&Diuင&ပ7တယ&

• တဖန& Class တစ&ခuဟ) Interface တစ&ခuထက&မက Implements လuပ&Diuင&ပ7တယ&

• Java SE 8 ကeန စ@ပ-0 Interface မ() static method DS= Default Method
eတ6ကiu eရ0သ)0လ)Diuင&ပ7တယ&
Using Interface
Writing Interface
• Java ဘ)သ)ရပ&တ6င& အသuC0;ပueသ) Interface သည& Reference Type
မ9)0;ဖစ&>က@ပ-0၊ class D(င&= ပuCသ[)န& ဆင&တKပ7သည&။

• Interface မ9)0တ6င& Constants မ9)0၊ Abstract Method မ9)0D(င&= Default


Method မ9)0အ)0 eရ0သ)0Diuင&ပ7သည&

• Interface ၏ Abstract Method မ9)0ကiuလည&0 Abstract Class ၏ Abstract


method မ9)0ကS=သiuပင& Method Body အ)0 eရ0သ)0၍ မရပ7။ သiuရ)တ6င& abstract
keyword အ)0 အသuC0;ပuရန&မလiuအပ&ပ7

• Interface အ)0 Implement လuပ&သ?&= Class မ9)0တ6င& အဆiuပ7 method မ9)0အ)0


Override လuပ&၍ method body အ)0 eရ0သ)0ရပ7မည&

• Default Method မ9)0မ()မK Class ၏ Method မ9)0D(င&= အတKတKပင& ;ဖစ&၏


Writing Interface
public interface Gear {

public enum GearNum {


GEAR_ONE, GEAR_2, GEAR_3, GEAR_4, BACK, NEUTRAL;
}

int changeGear(GearNum gearNum);

public default void showMessage(String message) {


System.out.println(message);
}

public static int getSpeed(int speed) {


return speed * 1.3;
}
}
Interface Implementation
public interface Gear {
int chageGear(GearNum gearNum);
}

public class MountainBikeGear implements Gear {

@Override
public int changeGear(GearNum gearNum) {
return gearNum.ordinal() * 2;
}

}
Creating Interface Object

Instance of It’s Lambda


By Anonymous Expression &
Implementation
Classes
Class Method
References
By Implementation Class
public class MountainBike {

private Gear gear;

public MountainBike() {
this.gear = new MountainBikeGeer();
}

public void changeSpeed(GearNum num, int min) {


int speed = this.gear.changeGear(gear);
System.out.println("Gear : " + num.toString());
System.out.println("Speed : " + speed);
System.out.println("Time : " + min + " Minutes");
System.out.println("Distance : " + (speed * min));
}

}
By Anonymous Class
public class CityBike {
private Gear gear;
public CityBike() {
this.gear = new Gear() {
@Override
public int changeGear(GearNum gearNum) {
return gearNum.ordinal() * 1;
}
};
}

public void changeSpeed(GearNum gear, int min) {


int speed = this.gear.changeGear(gear);
System.out.println("Gear : " + gear.toString());
System.out.println("Speed : " + speed);
System.out.println("Time : " + min + " Minutes");
System.out.println("Distance : " + (speed * min));
}
}
Interface make contract
• Interface တစ&ခuဟ) Class တစ&ခuမ() ပiuင&ဆiuင&သင&=တS= method
eတ6ကiu စuစည&0eပ0ထ)0ပ7တယ&

• ဒ7e>က)င&= အသuC0;ပuမယ&= Class DS= အသuC0;ပuခCရမS= Class >က)0က


Contract eတ6ကiu ;ဖeပXeစပ7တယ&

• Interface ကiu Implement လuပ&မယ&= Class ကလည&0 Interface


ထSမ() ပ7တS= method eတ6ကiu implement လuပ&ထ)0ဖiu
လiuအပ&သလiu၊ အသuC0;ပuမS= Class ထSကလS Interface ထSမ()ပ7တS=
Method eတ6အတiuင&0သ) အသuC0;ပuDiuင&မ() ;ဖစ&ပ7တယ&
Advantages of Interface

• Encapsulation ကiu ပiuမiu တiu0တက&eစDiuင&ပ7တယ&

• Class eတ6>က)0မ()1iတS= ခ9Pပ&eD()င&မN= (Coupling) ကiu


eလ9)=ခ9eစDiuင&ပ7တယ&

• မတKည-တS= Inheritance Hierarchy အတ6င&0မ()1iတS= Class


eတ6ရS=ပတ&သက&မN=ကiu ဖန&တ-0eပ0Diuင&ပ7တယ&
Questions
• Interface DS= Abstract Class တiuGS= က6);ခ)0မN=ကiu eဖX;ပပ7

• Method Overloading D(င&= Overriding တiu၏ က6);ခ)0ခ9က& ကiu


eဖX;ပeပ0ပ7

• OOP ကiu အသuC0;ပu;ခင&0အ)0;ဖင&= ဘ)e>က)င&= Easy of Create


;ဖစ&သည& ဟu ဆiuDiuင&ပ7သနည&0

• Easy of Maintain ;ဖစ&eအ)င& မည&သiu eဆ)င&G6က&Diuင&သနည&0

• Easy of Enhance ;ဖစ&eအ)င& မည&သiu eဆ)င&G6က&Diuင&သနည&0

You might also like