ΊέΊέί£

ΊέΊέί£Share a Scribd company logo
Java Presentation
αž˜αž‡αŸ’αžˆαž˜αžŽαŸ’αžŒ αž›αž€αžΌαžš αŸ‰αŸ αžŸαž αŸ’αžœ αžœαŸ‚ αžšαŸαž… េ ឌី
Korea Software HRD Center
www.kshrd.com.kh
αž€αŸ’αž€ αž»αž˜αž‘αžΈαŸ€αŸ– αžšαŸ„αž€ αž‡αŸ’αžΉαž˜ αž˜αž·αž“αžΆ
αžšαŸ„αž€ αž‡αžΆαž‚αžΈαž˜αž αžΆαŸ‚
αžšαŸ„αž€β€‹αž αŸ’αŸŠαž»αŸ† αž‡αŸ’αžΈαŸ‚αŸαž“
αžšαŸ„αž€ αžŸαž„ αž‚αž„αŸ’αž‚αžΆ
αžšαŸ„αž€ αž‚αžΈαž˜ αž”αž“αž αŸ’αž„
αžαŸ’αž“αžΆ αž€αŸ‹ αž—αŸ’αžΆαŸ†αžšαŸαž‰ αž€αŸ’αž€ αž»αž˜αž‘αžΈ ៀ
αž€αŸ’αž”αž’αžΆαž“αž”αž‘αŸ– αž˜αžΌαž›αžŠαŸ’αž‹αžΆ αž“αž‚αŸ’αžšαžΉαŸ‡αžšαž”αžŸαŸ‹αž—αžΆαžŸαžΆJava
αžŸαž˜αžΆαž‡αŸ’αž·αž€
αž›αŸ„αž€ αž‡αžΆαž‚αžΈαž˜αž αžΆαžœ
αž›αŸ„αž€β€‹αž αŸŠαž»αŸ† αž‡αžΈαžœαŸαž“
αž›αŸ„αž€ αžŸαž„ αž‚αž„αŸ’αž‚αžΆ
αž›αŸ„αž€ αž‚αžΈαž˜ αž”αž“αž αž„
αž˜αžΆαžαž·αž€αžΆ
1. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ OOP?
2. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆβ€‹Class αž“αž·αž„ Object ?
3. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Encapsulation ?
4. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Inheritance?
5. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Abstraction (Abstract class Interface)
6. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Polymorphism ?
3
4
OOP
 αž–αžΆαž€αž™αžαžΆ OOP αž˜αžΆαž“αž“αŸαž™αžαžΆ Object Oriented Programming αž› αžΎαž˜αž”αžΈαž‡αž½αž™αž› αŸ„αŸ‡αžŸαŸ’αžšαžŸαž™αž›αŸ…αž”αž‰αŸ’αž αžΆ αž‘αžΆαŸ†αž„αž‘αžΆαž™αŸ”
 αž˜αžΌαž› អ αž“αžŸαŸ’αžšαž‚αžšαŸ„αŸ‡αžšαž”αžŸαŸ‹ OOP αž‚αžΊαž‡αžΆβ€‹class αž› αžΎαž˜αž”αžΈαžŸαžΆ αž›αŸ‹αžαžΆ Program αž˜αž½αž™αž˜αžΆαž“αž›αž€αžαžŽαŸˆαž‡αžΆ OOP
αž›αŸ„αŸ‡αžŸαŸ’αžšαžΆαž Program αž› αŸ„αŸ‡αž”αžΆαŸ†αž›αŸαž‰αž“αžΌαžœαž›αž€αžαžαžŽαžŒ αŸ£αž™αŸ‰αžΆ αž„αž‚αžΊαŸ–
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ OOP ?
1. Encapsulation
2. Inheritance
3. Polymorphism
5
OOP
 Class αž‚αžΊαž‡αžΆαŸαž˜αž–αž‚αžΆαŸ†αžšαžΌαžŸαžŸαŸ’αžšαž˜αžΆαž”αŸ‹αž”αž›αž„αž€αžΎ Object αž¬β€‹αž€αžΆαŸ†αžŽ αŸ‹αž‘αžŸαŸ’αžšαž˜αž„αŸ‹αžšαž”αžŸαŸ‹ Object αŸ”
 Java αž›αžŸαŸ’αžšαž”αžΎαž›αž€αžαžŽαŸˆαŸαž·αž›αžŸαžŸαžšαž”αžŸαŸ‹ Class αž› αžΎαž˜αž”αžΈαž”αž›αž„αž€αžΎ Object αŸ”
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Class ?
 Instance Variable ( Data Member ) αž“αž·αž„ Method ត αž›
Instance Variable αž“αž·αž„ Method αž‚αžΊαž‡αžΆ Member αžšαž”αžŸαŸ‹ ClassαŸ”
6
OOP
class class-name{
type instance-variable1;
type instance-variable2;
//….
type instance-variableN;
type methodname 1(parameter-list){
//body of method
}
type methodname 2(parameter-list){
//body of method
}
type methodname N(parameter-list){
//body of method
}
αž‘αžΆαŸ†αžšαž„αŸ‹αž‘αžΌαž›αŸ…αžšαž”αžŸαŸ‹ Class
7
OOP
Class Vehicle{
int passengers; //number of passengers
int fuelcap; //fuel capacity in liters
int kpl; //fuel consumption in kilometers per liter
}
αž€αžΆαžšαž”αž›αž„αž€αžΎ Class
o Class αž’αžΆαž…αž”αž›αž„αž€αžΎ αž“αžΌαžœαžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™ (Data type) αžαŸ’αž˜αžΈαž˜αž½αž™ αŸ” αž€αž“ αž»αž„αž€αžšαžŽαžΈαž›αž“αŸ„αŸ‡ αžŸαŸ’αžšαž”αž›αŸαž‘
αž‘αž·αž“αž“αž“αŸαž™αžαŸ’αž˜αžΈαž› αŸ„αŸ‡αž›αŸ’αž˜ αŸ„αŸ‡αžαžΆβ€‹ Vehicle αž›αž αžΎαž™αž›αž™αžΎαž„αž“αžšαž„αž›αžŸαŸ’αžšαž”αžΎαž›αŸ’αž˜ αŸ„αŸ‡αž›αž“αŸ„αŸ‡αž› αžΎαž˜αž”αžΈαžŸαŸ’αžšαž”αž€αžΆαžŸ
Objects αŸ”
Vehicle minivan = new Vehicle();
OOP
 Object αž‚αžΊαž‡αžΆ Instance αžšαž”αžŸαŸ‹ ClassαŸ”
 Object αž‡αžΆαž’αŸ’αžœαžΈαŸ—αž αž›αž›αŸ…αž‡αžΆαŸ†αžœ αž·αž‰αžαž› αž½αž“αž›αž™αžΎαž„ αžΌαž…αž‡αžΆ αž€αž„αŸ‹ αž˜αŸ‰αžΆαžΌ αžΌβ€‹αž‘αžΆαž“ αž›αžŸαŸ€αžœαž›αŸ… αž” αŸŠαž·αž… αž€αžΆαŸ†αŸαž™αžΌ αž‘αŸαžšβ€¦αŸ”
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Object ?
 Object αž˜αžΆαž“ State αž“αž·αž„ BehaviorαŸ”
οƒΌ State αž‡αžΆαž›αž€αžαžŽαŸˆ ឬ αž’αžΆ αž•αŸ’αžŸαžΆαŸ†αžŸαžŸαŸ’αžšαž˜αžΆαž”αŸ‹αž”αž›αž„αž€αžΎ Object αŸ”
Ex1: αž˜αž“αžŸαžŸαž˜αžΆαž“ αžŸαž˜αž”αž» រ αž€αžΆαŸ†αŸαžŸαŸ‹ αž’αžΆαž™ αž›αŸαž‘ αŸ”
Ex2:αž˜αŸ‰αžΆαžΌ αžΌαž˜αžΆαž“ αž€αž„αŸ‹ αžαž€αž” αžŸαŸ’αžšαž…αžœαŸ‰αžΆ αž€αŸ‹ αž›αŸαž›αžΎαž„αžŸαžΈαž‰αŸ‰αžΆαžΌβ€¦αŸ”
οƒΌ Behavior αž‡αžΆαž’αŸ’αžœαžΈαž αž› Object αž’αžΆαž…αž›αŸ’αžœαžΎαž”αžΆαž“αŸ”
Ex1: αž˜αž“αžŸαžŸαž˜αžΆαž“ αž› ើរ αž› αž€ αž’αŸ’αž„αž‚ αž»αž™ ឈរ αž™αžΆαŸ† αž›αžŸαžΎαž…β€¦.αŸ”
Ex2:αž‘αžΌαžš ATM αž˜αžΆαž“ αž€αžŸαŸ’αžšαž”αžΆαž€αŸ‹ αž€αŸ‹αžŸαŸ’αžšαž”αžΆαž€αŸ‹ …. αŸ”
9
OOP
αžšαž›αž”αŸ€αž”αž”αž›αž„αž€αžΎ Objects
Vehicle minivan = new Vehicle();
β€’ αž›αž™αžΎαž„αžŸαŸ’αžšαž”αž€αžΆαžŸαž’αŸ’αž‰αŸ’αž‰ αž·αžαž˜αž½αž™αž˜αžΆαž“αž›αŸ’αž˜ αŸ„αŸ‡αžαžΆ minivan ត αž›αž˜αžΆαž“ Vehicle αž‡αžΆαžŸαŸ’αžšαž”αž›αŸαž‘
αž‘αž·αž“αž“αž“αŸαž™ (Reference of Object)αŸ”
β€’ new operator αž”αž›αž„αž€αžΎ αž‘αžΈαžΆαžΆαŸ†αž„αž€αž“ αž»αž„β€‹Memory αžšαž”αžŸαŸ‹ Object αž˜αž½αž™αž›αž αžΎαž™αž•αŸ’αžαž›αŸ‹
Reference αž›αŸ…αž²αŸ’αž™ Object αž› αŸ„αŸ‡ αŸ”
β€’ Reference αž‡αžΆαž’αžΆαžŸαŸαž™ អ αž“αž“αž“ Object αž›αŸ…αž€αž“ αž»αž„ Memory ត αž›αž”αž›αž„αž€αžΎ αž‘αžΈαžΆαžΆαŸ†αž„αž› αž™
new αŸ”
10
OOP
αžšαž›αž”αŸ€αž”αž”αž›αž„αž€αžΎ Methods
ret-type methName(param-list){
//body of method
}
οƒΌ ret-type αž”αž‰αŸ’αž αžΆ αž€αŸ‹αŸαžΈαžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™αž αž›αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž²αŸ’αž™ Method αŸ” αžœαž’αžΆαž…αž‡αžΆ
αžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™αŸ’αž˜αž˜αžΆ ឬ αž’αžΆαž…αžŸαŸ’αžšαž”αž›αŸαž‘αž‡αžΆβ€‹Class ត αž›αž›αž™αžΎαž„αž”αžΆαž“αž”αž›αž„αž€αžΎ αž› αžΎαž„ αŸ”
οƒΌ Method αž˜αž·αž“αžŸαŸ’αžš αžΌαžœαž€αžΆαžšαž•αŸ’αžαž›αŸ‹ αž“αž˜αž›αž›αž‘ αž› αŸ„αŸ‡αžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™αžšαž”αžŸαŸ‹αžœαžŸαŸ’αžš ូវត αž‡αžΆ void αŸ”
οƒΌ methName αž‡αžΆαž›αŸ’αž˜ αŸ„αŸ‡αžšαž”αžŸαŸ‹ Method αŸ”
οƒΌ param-list αž‡αžΆαžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™ αž“αž·αž„ αž›αŸ’αž˜ αŸ„αŸ‡αž’αŸ’αž‰αŸ’αž‰ αžαž·αž›αžŸαŸ’αžšαž”αžΎαž”αž“αžαž‚αŸ’αž“αž“ ត αž›αž“αžšαž„αžαž‰αž€αž•αŸ’αžαž αž…αŸ‹αŸαžΈαž‚αŸ’αž“αž“ αž› αž™
αžŸαž‰αŸ’αž αž‰ αž›αž€αž”αŸ€αžŸ (,)αŸ”
11
Encapsulation
 Encapsulation αžœαž‡αžΆαžαž•αŸ’αž“αž€αž˜αž½αž™αž“αž“ OOP conceptαŸ”
 αž›αŸ’αžœαžΎαž›αž’αžΆαž™ Field αž›αŸ…αž€αž“ αž»αž„ class αž˜αž½αž™αž˜αž·αž“αž’αŸ’αž“αž‰αŸ’αž αž‰ αž›αž’αžΆαž™αž˜αžΆαž“αž€αžΆαžš access
αž…αžΌαž›αŸαžΈ class αž“αž‘αŸ”αž›αž–αžΆαž›αž‚αžΊ variable αž›αŸ…αž€αž“ αž»αž„ class αž˜αž½αž™αžŸαŸ’αžš αžΌαžœαž”αžΆαž“ hide
េី class αž“αž‘ αž›αž αžΎαž™αžœαž’αžΆαž… access αž”αžΆαž“αžΆαž˜αžšαž™αŸˆ method getter()
αž“αž·αž„β€‹setter()αŸ”
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Encapsulation ?
12
Encapsulation
 Field αžšαž”αžŸαŸ‹ class αž’αžΆαž…αž€αžΆαŸ†αžŽ αŸ‹αž‡αžΆ Read-only ឬ Write-only.
 Class αž’αžΆαž…αžŸαŸ’αžšαž‚αž”αŸ‹αžŸαŸ’αžšαž‚αž„αž‘αžΆαŸ†αž„αžŸαŸ’αžšαžŸ αž»αž„αž›αŸ…αž›αž›αžΎ data αž› αž™αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹ codeαŸ”
αž‚αžŽαžŸαž˜αž” αžαž·αžšαž”αžŸαŸ‹ Encapsulation ?
 αž’αŸ’αž“αž€αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹ class αž˜αž·αž“ αžšαž„αŸαžΈαžšαž›αž”αŸ€αž”αž αž› class αž•αŸ’αž‘ αž»αž€(store) αž‘αž·αž“αž‘αž“αŸαž™αŸ”
 Class αž’αžΆαž…αž”αž αžΌαžšαž“αžΌαžœ data type αž“αž“ field αž˜αž½αž™αŸ”
 αž’αŸ’αž“αž€αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αŸ‹ class αž˜αž·αž“αžŸαŸ’αžš αžΌαžœαž€αžΆαžšαž”αž ូរ code αžšαž”αžŸαŸ‹αž›αž‚αŸ”
13
Encapsulation
public class TestJava {
private int id;
private String name;
private int age;
public void setId(int id){this.id=id;}
public int getID(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
public void setAge(int age){
if(age<0){
this.age=1;
}else{
this.age=age;
}
}
public int getAge(){return age;}
}
Public class DemoEncap{
public static void main(String args[]){
TestJava obj= new TestJava();
obj.setId(100);
obj.setName("Hong");
obj.setAge(-1);
System.out.println("ID:"+
obj.getID() + "nName:"+ obj.getName()+
"nAge:"+ obj.getAge());
}
}
14
Inheritance
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Inheritance?
 Inheritance αž‚αžΊαž‡αžΆαž›αž•αŸ’αž‘αžšαž˜αžš αž€αŸαžΈ Class αž˜αž½αž™αž›αŸ… Class αž˜αž½αž™αž›αž•αŸ’αžŸαž„αž›αž‘αŸ€ αŸ”
 Class ត αž›αž›αž•αŸ’αž‘αžš αž“αž˜αž›αž›αž’αžΆαž™αž›αž‚αž›αŸ…αžαžΆ Parent Class αž›αž αžΎαž™ Class ត αž›αž‘αž‘αž½αž› αž“αž˜αž›αž›αŸ…αžαžΆ Child ClassαŸ”
 Inheritance αž’αŸ’αž“αž‰αŸ’αž αž‰ αž·αž²αŸ’αž™ Class αž˜αž½αž™αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αž›αŸ… properties αž“αž·αž„ methods αž“αž“ class αž›αž•αŸ’αžŸαž„αž›αž‘αŸ€ αž”αžΆαž“ αŸ”
 Inheritance αž‚αžΊαž‡αžΆ compile-time mechanism αŸ” super-class αž’αžΆαž…αž˜αžΆαž“ subclasses αž“αž“αž˜αž½αž™αž…αžΆαŸ†αž“αž½αž“αŸ”
αž”αŸ‰αžΆαžαž“αž subclass αž˜αžΆαž“αžŸαŸ’αžš រមត super-class αž˜αž½αž™αž”αŸ‰αžΆαž›αŸ’αžŽ αŸ„αŸ‡ αŸ”
15
Inheritance
αž› ើ Inheritance αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž…?
 Extends: αž›αŸ…αž€αž“ αž»αž„Java αž› αžΎαž˜αž”αžΈαž›αŸ’αžœαžΎαž€αžΆαžš Inheritance េី Parent Class αž›αŸ…Child Class αž›αž™αžΎαž„αžŸαŸ’αžš αžΌαžœαž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹
Keyword ExtendsαŸ”
 Super: αž›αž‚αž›αžŸαŸ’αžšαž”αžΎαžœαžŸαžŸαŸ’αžšαž˜αžΆαž”αŸ‹αž€αžΆαŸ†αžŽ αŸ‹αž›αž€αžαžŽαŸˆMemberαžšαž”αžŸαŸ‹ Classαž›αž˜ αž€αžšαžŽαžΈ ត αž› Method αž“αž·αž„ Variable
αžšαž”αžŸαŸ‹ Classαž›αž˜αž“αž·αž„ Classαž€αžΌαž“ αžΌαž…αž‚αŸ’αž“αž“ αŸ”
 Implement: αž‡αžΆKeyword αž˜αž½αž™αž αž›αž’αŸ’αž“αž‰αŸ’αž αž‰ αž›αž’αžΆαž™ Class αž›αŸ’αžœαžΎαž€αžΆαžšαž‘αž‘αž½αž›αž˜αžš αž€αŸαžΈ InterfaceαŸ”
16
Inheritance
class ClassA{
int a=;
public void add(int x, int y){
a = x+y;
System.out.println("The sum of the given numbers:"+a);
}}
public class ClassB extends ClassA{
public void multiplication(int x, int y){
a= x*y;
System.out.println("The product of the given
numbers:"+a);
}
public static void main(String args[]){
int a = 20, b = 10;
ClassB sub = new ClassB ();
sub.add(a, b);
sub.multiplication(a, b);
}
}
 αž§αž‘αž αžšαžŽαŸˆ αž›αž™αžΎαž„αž˜αžΆαž“ Classេីរ αž‚αžΊ
ClassA αž“αž·αž„ClassB ត αž›ClassBαž›αŸ’αžœαžΎαž€αžΆαžš
Inheritannce αž“αž˜αž›αŸαžΈ ClassAαŸ”
17
Inheritance
 αž§αž‘αž αžšαžŽαŸˆ
public class Sub_class extends Super_class {
int num = 10; //display method of sub class
public void display(){
System.out.println("This is the display method of subclass");}
public void my_method(){
Sub_class sub = new Sub_class();//Instantiating subclass
sub.display();//Invoking the display() method of sub class
super.display();//Invoking the display() method of superclass
System.out.println("value of the variable named num in sub class:"+ sub.num);
//printing the value of variable num of superclass
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]){
Sub_class obj = new Sub_class();
obj.my_method();
}
class Super_class{
int num = 20; //display method of superclass
public void display(){
System.out.println("This is the display method
of superclass");}}
18
Inheritance
 αž§αž‘αž αžšαžŽαŸˆ Public Interface Motor{
void speedUp(int increment);
}
class MyMotor implement Motor{
int speed = 0;
void speedUp(int increment) {
speed = speed + increment;
}
void printStates() {
System.out.println("My motor speed" +speed);
}
}
19
Inheritance
αžŸαŸ’αžšαž”αž›αŸαž‘αž“αž“ Inheritance
 Single inheritance in java
 multilevel inheritance
 multiple inheritance
 hybrid inheritance
 hierarchical inheritance
20
Abstraction
 αžšαžΆαž›αŸ‹ programming languages ត αž„αž αž•αŸ’αžαž›αŸ‹ abstractions αŸ”
 αžœαž‡αž½αž™αž•αŸ’αžαž›αŸ‹αž›αŸ… αžΆαŸ†αž›αŸ’αŸ„αŸ‡αžŸαŸ’αžšαžŸαž™αž αž›αž˜αžΆαž“αž—αžΆαŸαžŸαž˜ αž»αž‚αžŸαž˜ αž‰αž“αž“αž”αž‰αŸ’αž αžΆ (αž› αŸ„αŸ‡αžŸαŸ’αžšαžŸαž™ αž›αŸ‹αž•αŸ’αžαž‘ αž›αŸ‹αž›αŸ…αžΆαž˜αžŸαŸ’αžšαž”αž›αŸαž‘
αž“αž·αž„ αž‚αžŽαžŸαž˜αž” αžαž·αž“αž“ abstraction αŸ”
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Abstraction?
 Abstraction αžœαž‡αžΆαž’αžΆ αžŸαžΆαŸ†αžαžΆαž“αŸ‹αž˜αž½αž™αž“αž“ object-oriented programming
 Abstraction αž‡αžΆ αžΆαŸ†αž›αžŽαžΎαžšαž€αžΆαžšαž“αž“αž€αžΆαžšαŸ„αž€αŸ‹αž”αžΆαžΆαŸ†αž„αž›αžŸαž…αž€αžαžΈαž›αžΆαŸ†αž’αŸ’αž· αž“αž“αž€αžΆαžšαž’αŸ’αž“αžœ αžαž“αŸ( implementation)αž›αž αžΎαž™αž”αž„αŸ’αž‚αžΆ αž‰αž
ួ αž‘αžΈ ត αž˜αž½αž™αž‚ αŸ‹αž›αŸ… αž›αŸ‹αž’αŸ’αž“αž€αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αŸ”
21
Abstraction
 Abstract class αž‡αžΆβ€‹class αž˜αž½αž™αž αž›αžŸαŸ’αžšαž”αž€αžΆαžŸαž‡αžΆ abstract αŸ”
 Abstract method αŸ’αžΊαž‡αžΆ methodαž˜αž½αž™αž αž›αžŸαŸ’αžšαž”αž€αžΆαžŸαž‡αžΆ abstract αŸ”
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Abstract class?
 Abstract Class αž€αžšαž€αžΆαž‘αž€αž›αŸ… abstract method αž˜αž½αž™αž¬αž›αžŸαŸ’αžšαž…αžΎαž“αŸ”
Syntax: abstract class (class name){
//abstract methods
}
22
Abstraction
αž…αžΆαŸ†αŸ’αžΆαŸ†αŸ–
o αž‚αž”αžŸαž·αž“αž‡αžΆαž˜αžΆαž“ abstract method αž…αž‚αžšαžΎαž“αž…αŸ…αž€αŸ’αž“αž»αž„ class αžαŸ‚αž˜αž½αž™
αž… αžΉαŸ‡ class αž‚αŸ‚αžΌαžœαžαŸ‚αž‡αžΆ abstract αŸ”
o αž‚αž”αžŸαž·αž“αž‡αžΆαž’αŸ’αž“αž€αŸ’αž€αŸ’αŸ†αž–αž»αž„αžαŸ‚ extend abstract classαž…αž‚αžšαžΎαž“αžαŸ‚αž›αž˜αžΆαž“ abstract method
αž’αŸ’αž“αž€αŸ’αž˜αž·αž“αž‚αŸ‚αžΌαžœαž•αŸ’αžαž›αŸ‹αž€αžΆαžš implementationαž…αŸ…αž…αŸ„αž™ method ឬ αž…αŸ’αžœαžΎαž…αŸ„αž™ class αž… αžΉαŸ‡
αž€αžΆαžΆ αž™αž‡αžΆ abstract classαŸ”
23
Abstraction
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Interface ?
 Interface αž›αž˜αžΎαž›αž›αŸ… αžΌαž…αž‡αžΆ class αž”αŸ‰αžΆαžαž“αžαžœαž˜αž·αž“αžαž˜αž“αž‡αžΆ class αž›αž‘αŸ”
 Interface αž‚αžΊαž›αžŸαŸ’αžšαž”αžΎαž›αŸ… default abstract (αž˜αžΆαž“αžŸαŸ’αžš រមត method signature αž‚αŸ’αž“αž˜ αž“ body αž›αž‘)αŸ”
 Interface αž› ើរ ួ αž‘αžΈαž‡αžΆ blueprint (αž”αž›αž„αŸ‹)αžŸαžΆαŸ†αžšαžΆαž”αŸ‹ class αŸ”
 Interface αž˜αž·αž“αž’αžΆαž…αž”αž›αž„αž€αžΎ object αž”αžΆαž“αž›αž‘αŸ”
24
Abstraction
αž›αž  αž’αŸ’αžœαžΈαž›αž™αžΎαž„αžŸαŸ’αžš αžΌαžœαž›αžŸαŸ’αžšαž”αžΎ Interface ?
 αž›αŸ’αžœαžΎαž²αŸ’αž™ abstract αž˜αž½αž™β€‹β€‹β€‹αž˜αžΆαž“αž›αž€αžαžŽαŸˆαž›αŸαž‰αž›αž›αž‰ αŸ”
 αžΆαž˜αžšαž™αŸˆ interface αž›αž™αžΎαž„αž’αžΆαž…αžŸαŸ’αžšαž” αž·αž” αžαž·αž€αžΆαžšαž˜αžαž„αŸ’αž‚αžšαžšαž”αžŸαŸ‹ multi-inheritanceαŸ”
 αžœαž’αžΆαž…αžŸαŸ’αžš αžΌαžœαž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αž› αžΎαž˜αž”αžΈαž”αžΆαŸ†αž›αŸαž‰ loose coupling αŸ”
25
Abstraction
1. Abstract class VS Interface:
αž—αžΆαŸαžŠαžΌαž…αž‚αŸ’αž“αžΆ αžœαžΆαž„β€‹Abstract class & Interface:
 αž’αžΆαž…αž²αŸ’αž™ abstraction αž“αž·αž„αž’αžΆαž…αžŸαŸ’αžšαž”αž€αžΆαžŸ abstract method αŸ”
 Abstract class & Interface αž˜αž·αž“αž’αžΆαž…αž‡αžΆ instantiatedαŸ”
26
Abstraction
Abstract class Interface
1) Abstract class αž’αžΆαž…αž˜αžΆαž“αž¬αž‚αŸ’αž“αž˜ αž“(0%-100%)
abstract methodαŸ”
Interface αžŸαŸ’αžš ូវត αž˜αžΆαž“ (100%) abstract methodαŸ”
2) Abstract class αž˜αž·αž“β€‹β€‹support multiple
inheritanceαŸ”
Interface supports multiple inheritance.
3) Abstract class αž’αžΆαž…αž˜αžΆαž“ final, non-final,
static and non-static variablesαŸ”
Interface αž’αžΆαž…αž˜αžΆαž“ static and final variables αž”αŸ‰αžΆαž›αŸ’αžŽ αŸ„αŸ‡αŸ”
4) Abstract class αž’αžΆαž…αž˜αžΆαž“ static methods,
main method and constructorαŸ”
Interface αž˜αž·αž“αž˜αžΆαž“ static methods, main method and
ConstructorαŸ”
5) Abstract class αž’αžΆαž…αž•αŸ’αžαž›αŸ‹β€‹implementation
αžšαž”αžŸαŸ‹interfaceαŸ”
Interface αž˜αž·αž“αž’αžΆαž…αž•αŸ’αžαž›αŸ‹β€‹implementation αžšαž”αžŸαŸ‹ abstract class
αž”αžΆαž“αž›αž‘αŸ”
6) The abstract keyword αž‚αžΊαž›αžŸαŸ’αžšαž”αžΎαž› αžΎαž˜αž”αžΈαžŸαŸ’αžšαž”αž€αžΆαžŸαŸ‹
abstract classαŸ”
The Interface keyword αž‚αžΊαž›αžŸαŸ’αžšαž”αžΎαž› αžΎαž˜αž”αžΈαžŸαŸ’αžšαž”αž€αžΆαžŸαŸ‹ InterfaceαŸ”
7) Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
αž—αžΆαŸαžαžŸαž‚αŸ’αž“αžΆ αžœαžΆαž„β€‹Abstract class & Interface:
27
Polymorphism
 Polymorphism αž˜αž€αŸαžΈαž—αžΆαžŸαž›αžŸαŸ’αžšαž€αž€β€‹αŸαžΈαžšαž–αžΆαž€αž™αž‚αžΊ poly αž“αž·αž„ morphs αŸ”
 Poly αž˜αžΆαž“αž“αŸαž™αžαžΆβ€‹αž›αžŸαŸ’αžšαž…αžΎαž“ αž“αž·αž„ morphs αž˜αžΆαž“αž“αŸαž™αžαžΆ αž‘αžΆαŸ†αžšαž„αŸ‹ αŸ”
 polymorphism αž˜αžΆαž“αŸαžΈαžšαžŸαŸ’αžšαž”αž›αŸαž‘αž›αŸ…αž€αž“ αž»αž„ java αž‚αžΊ
1) compile time polymorphism(static polymorphism)
2) runtime polymorphism (Dynamic polymorphism )
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Polymorphism?
 αž›αž™αžΎαž„αž’αžΆαž…αž’αŸ’αž“αžœ ត polymorphism αž›αŸ…αž€αž“ αž»αž„ java αž› αž™
method overloading αž“αž·αž„ method overriding αŸ”
28
Polymorphism
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Runtime Polymorphism ?
 Method overriding αž‚αžΊαž‡αžΆαž§αž‘αž αžšαžŽαŸ αž›αž’ αž…αžΆαŸ†αž›αž–αžΆαŸ„αŸ‡ runtime polymorphism αŸ”
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
 αž›αžΆαŸ„αŸ‡αž›αž™αžΎαž„αž›αŸ…αž›αž˜αžΎαž›αž§αž‘αž αžšαžŽαŸαž› αžΎαž˜αž”αžΈαž™αž›αŸ‹αž€αžΆαž“αŸ‹αž…αžΆαžŸαŸ‹
public class X {
public void methodA() //Base class method
{ System.out.println
("hello, I'm methodA of class X");
}
}
public class Y extends X {
public void methodA() //Derived Class method
{ System.out.println ("hello, I'm methodA of class Y");
}
}
public class Z {
public static void main (String args []) {
X obj1 = new X(); // Reference and object X
X obj2 = new Y(); // X reference but Y object
obj1.methodA();
obj2.methodA();
}
}
Output:
hello, I'm
methodA of
class X
hello, I'm
methodA of
class Y
29
Polymorphism
αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Compile time Polymorphism ?
 Method overloading αž‚αžΊαž‡αžΆαž§αž‘αž αžšαžŽαŸ αž›αž’ αž…αžΆαŸ†αž›αž–αžΆαŸ„αŸ‡ compile time polymorphism αŸ”
Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();
 αž›αžΆαŸ„αŸ‡αž›αž™αžΎαž„αž›αŸ…αž›αž˜αžΎαž›αž§αž‘αž αžšαžŽαŸαž› αžΎαž˜αž”αžΈαž™αž›αŸ‹αž€αžΆαž“αŸ‹αž…αžΆαžŸαŸ‹
class X {
void methodA(int num) {
System.out.println ("methodA:" + num);
}
void methodA(int num1, int num2)
{
System.out.println ("methodA:" + num1 + "," + num2);
} double methodA(double num) {
System.out.println("methodA:" + num); return num;
}
} class Y {
public static void main (String args []) {
X Obj = new X(); double result; Obj.methodA(20);
Obj.methodA(20, 30); result = Obj.methodA(5.5);
System.out.println("Answer is:" + result);
}
}
Output:
methodA:20
methodA:20,30
methodA:5.5
Answer is:5.5
30
Polymorphism
 αž’αŸ’αž“αž‰αŸ’αž αž‰ αž·αž²αŸ’αž™ interface αž˜αž½αž™αž’αžΆαž…αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αž‡αžΆαž‘αžΌαž›αŸ…αž›αŸ…αž€αž“ αž»αž„ classαŸ”
 αžŸαŸ’αžšαž” αž·αž” αžαž·αž€αžΆαžšαž˜αž½αž™αž’αžΆαž…αž˜αžΆαž“ behavior αžαžŸαž‚αŸ’αž“αž“ αž›αŸ…αž€αž“ αž»αž„αž€αžšαžŽαžΈαž›αž•αŸ’αžŸαž„αž‚αŸ’αž“αž“ αŸ”
αž› ើ Polymorphism αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž…?
 Behavior αŸαžšαž„αžαž•αŸ’αž’αž€αž›αŸ…αž›αž›αžΎαžŸαŸ’αžšαž”αž›αŸαž‘αž“αž“ data ត αž›αž”αžΆαž“αž›αžŸαŸ’αžšαž”αžΎαž›αŸ…αž€αž“ αž»αž„αž›αŸαž›αž’αŸ’αž“αžœ αžαŸ”
 αžœαž› ើរ ួ αž‘αžΈαž™αŸ‰αžΆ αž„αžŸαžΆαŸ†αžαžΆαž“αŸ‹αž›αŸ…αž€αž“ αž»αž„αž€αžΆαžšαž’αŸ’αž“αž‰αŸ’αž αž‰ αž·αž²αŸ’αž™ object ត αž›αž˜αžΆαž“ internal structures
αž› αžΎαž˜αž”αžΈαžαž…αž€αžšαŸ†αžαž›αž€ external interface αŸ”
 Polymorphism αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αž‚αž›αžŸαŸ’αžšαž”αžΎαž™αŸ‰αžΆ αž„αž‘αžΌαž›αžΆαŸ†αž‘αžΌαŸ„αž™αž€αž“ αž»αž„αž€αžΆαžš implement inheritance αŸ”
31
Polymorphism
αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆβ€‹Method Overloading ?
 Method Overloading αž˜αžΆαž“αž›αŸ’αž˜ αŸ„αŸ‡ αžΌαž…αž‚αŸ’αž“αž“ β€‹αž αžαžŸαž‚αŸ’αž“αž“ αž›αŸ…αž…αžΆαŸ†αž“αž½αž“ αž“αž·αž„ data type αž“αž“ parameter αŸ”
class Overload
{
void demo (int a) {
System.out.println ("a: " + a);
}
void demo (int a,int b){
System.out.println ("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a); return a*a;
}
}
class MethodOverloading {
public static void main (String args []) {
Overload Obj = new Overload(); double result; Obj .demo(10); Obj .demo(10, 20);
result = Obj .demo(5.5); System.out.println("O/P : " + result);
}
}
Output:
a: 10
a and b: 10,20
double a: 5.5
O/P : 30.25
32
Polymorphism
αž› ើ Method Overloading αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž… ?
1. Method Overloading αž’αžΆαž…αž›αŸ’αžœαžΎαž€αžΆαžšαž›αŸ…αž€αž“ αž»αž„ class αžΌαž…αž‚αŸ’αž“αž“ ឬ αž›αŸ…αž€αž“ αž»αž„ sub-class αžšαž”αžŸαŸ‹αžœ αŸ”
2. Constructor αž›αŸ…αž€αž“ αž»αž„ Java αž’αžΆαž…αž‡αžΆ Overloaded
3. Overloaded methods αž‚αž½αžšαž αž˜αžΆαž“ argument list αžαžŸαž‚αŸ’αž“αž“
4. Parameter αž’αžΆαž…αžαžŸαž‚αŸ’αž“αž“ αž›αŸ…αž›αž›αžΎ type ឬ number αž¬αž€ αž‘αžΆαŸ†αž„αŸαžΈαžšαž αž˜αžαž„ αŸ”
5. αŸαž½αž€αžœαž’αžΆαž…αž˜αžΆαž“ return types αžαžŸαž‚αŸ’αž“αž“ ឬ αžΌαž…αž‚αŸ’αž“αž“ αŸ”
6. αžœαž€αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αž‚αž›αŸ…αžαžΆαž‡αžΆ compile time polymorphism αŸ”
33
Polymorphism
αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆβ€‹Method Overriding ?
 Method Overriding αž˜αžΆαž“αž›αŸ’αž˜ αŸ„αŸ‡ αžΌαž…αž‚αŸ’αž“αž“ β€‹αž…αžΆαŸ†αž“αž½αž“ αž“αž·αž„ data type αž“αž“ parameter αž€ αžΌαž…αž‚αŸ’αž“αž“ αŸ”
public class BaseClass { public void methodToOverride() //Base class method
{
System.out.println ("I'm the method of BaseClass");
}
}
public class DerivedClass extends BaseClass {
public void methodToOverride() //Derived Class method
{
System.out.println ("I'm the method of DerivedClass");
}
}
public class TestMethod {
public static void main (String args []) { // BaseClass reference and object
BaseClass obj1 = new BaseClass(); // BaseClass reference but DerivedClass object
BaseClass obj2 = new DerivedClass(); // Calls the method from BaseClass class
obj1.methodToOverride(); //Calls the method from DerivedClass class obj2.methodToOverride();
}
}
Output:
I'm the method of BaseClass I'm the method
of DerivedClass
34
Polymorphism
αž› ើ Method Overriding αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž… ?
1. αž’αŸ’αž“αž‰αŸ’αž αž‰ ិ ត αž…αžΆαŸ†αž›αž–αžΆαŸ„αŸ‡ inherited methods
2. Object type (αž˜αž·αž“αžαž˜αž“αž‡αžΆ reference variable type)αž› αž™αž€αžΆαŸ†αžŽ αŸ‹
overridden method αž“αž·αž„αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αžŸαŸ’αžšαž”αžΎαž›αŸ…αž›αŸαž› runtime
3. Overriding method αž’αžΆαž…αž˜αžΆαž“ return type αžαžŸαž‚αŸ’αž“αž“
4. Overriding method αž˜αž·αž“αžŸαŸ’αžš αžΌαžœαž˜αžΆαž“ restrictive access modifier αŸ”
5. Abstract methods αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž’αžΆαž… overridden
6. Static αž“αž·αž„ final method αž˜αž·αž“αž’αžΆαž… overridden
7. αžœαžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αž‚αžŸαžΆ αž›αŸ‹αžαžΆαž‡αžΆ Runtime polymorphism αŸ”
10. αž€αŸ’αž”αž—αŸ’αŸαž―αž€αžŸ
35
αžŸαžΌαž˜αž’αŸ’αžšαŸ’αž»αžŽ!!!
36

More Related Content

OOP Concept

  • 1. Java Presentation αž˜αž‡αŸ’αžˆαž˜αžŽαŸ’αžŒ αž›αž€αžΌαžš αŸ‰αŸ αžŸαž αŸ’αžœ αžœαŸ‚ αžšαŸαž… េ ឌី Korea Software HRD Center www.kshrd.com.kh αž€αŸ’αž€ αž»αž˜αž‘αžΈαŸ€αŸ– αžšαŸ„αž€ αž‡αŸ’αžΉαž˜ αž˜αž·αž“αžΆ αžšαŸ„αž€ αž‡αžΆαž‚αžΈαž˜αž αžΆαŸ‚ αžšαŸ„αž€β€‹αž αŸ’αŸŠαž»αŸ† αž‡αŸ’αžΈαŸ‚αŸαž“ αžšαŸ„αž€ αžŸαž„ αž‚αž„αŸ’αž‚αžΆ αžšαŸ„αž€ αž‚αžΈαž˜ αž”αž“αž αŸ’αž„
  • 2. αžαŸ’αž“αžΆ αž€αŸ‹ αž—αŸ’αžΆαŸ†αžšαŸαž‰ αž€αŸ’αž€ αž»αž˜αž‘αžΈ ៀ αž€αŸ’αž”αž’αžΆαž“αž”αž‘αŸ– αž˜αžΌαž›αžŠαŸ’αž‹αžΆ αž“αž‚αŸ’αžšαžΉαŸ‡αžšαž”αžŸαŸ‹αž—αžΆαžŸαžΆJava αžŸαž˜αžΆαž‡αŸ’αž·αž€ αž›αŸ„αž€ αž‡αžΆαž‚αžΈαž˜αž αžΆαžœ αž›αŸ„αž€β€‹αž αŸŠαž»αŸ† αž‡αžΈαžœαŸαž“ αž›αŸ„αž€ αžŸαž„ αž‚αž„αŸ’αž‚αžΆ αž›αŸ„αž€ αž‚αžΈαž˜ αž”αž“αž αž„
  • 3. αž˜αžΆαžαž·αž€αžΆ 1. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ OOP? 2. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆβ€‹Class αž“αž·αž„ Object ? 3. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Encapsulation ? 4. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Inheritance? 5. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Abstraction (Abstract class Interface) 6. αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆ Polymorphism ? 3
  • 4. 4 OOP  αž–αžΆαž€αž™αžαžΆ OOP αž˜αžΆαž“αž“αŸαž™αžαžΆ Object Oriented Programming αž› αžΎαž˜αž”αžΈαž‡αž½αž™αž› αŸ„αŸ‡αžŸαŸ’αžšαžŸαž™αž›αŸ…αž”αž‰αŸ’αž αžΆ αž‘αžΆαŸ†αž„αž‘αžΆαž™αŸ”  αž˜αžΌαž› អ αž“αžŸαŸ’αžšαž‚αžšαŸ„αŸ‡αžšαž”αžŸαŸ‹ OOP αž‚αžΊαž‡αžΆβ€‹class αž› αžΎαž˜αž”αžΈαžŸαžΆ αž›αŸ‹αžαžΆ Program αž˜αž½αž™αž˜αžΆαž“αž›αž€αžαžŽαŸˆαž‡αžΆ OOP αž›αŸ„αŸ‡αžŸαŸ’αžšαžΆαž Program αž› αŸ„αŸ‡αž”αžΆαŸ†αž›αŸαž‰αž“αžΌαžœαž›αž€αžαžαžŽαžŒ αŸ£αž™αŸ‰αžΆ αž„αž‚αžΊαŸ– αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ OOP ? 1. Encapsulation 2. Inheritance 3. Polymorphism
  • 5. 5 OOP  Class αž‚αžΊαž‡αžΆαŸαž˜αž–αž‚αžΆαŸ†αžšαžΌαžŸαžŸαŸ’αžšαž˜αžΆαž”αŸ‹αž”αž›αž„αž€αžΎ Object αž¬β€‹αž€αžΆαŸ†αžŽ αŸ‹αž‘αžŸαŸ’αžšαž˜αž„αŸ‹αžšαž”αžŸαŸ‹ Object αŸ”  Java αž›αžŸαŸ’αžšαž”αžΎαž›αž€αžαžŽαŸˆαŸαž·αž›αžŸαžŸαžšαž”αžŸαŸ‹ Class αž› αžΎαž˜αž”αžΈαž”αž›αž„αž€αžΎ Object αŸ” αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Class ?  Instance Variable ( Data Member ) αž“αž·αž„ Method ត αž› Instance Variable αž“αž·αž„ Method αž‚αžΊαž‡αžΆ Member αžšαž”αžŸαŸ‹ ClassαŸ”
  • 6. 6 OOP class class-name{ type instance-variable1; type instance-variable2; //…. type instance-variableN; type methodname 1(parameter-list){ //body of method } type methodname 2(parameter-list){ //body of method } type methodname N(parameter-list){ //body of method } αž‘αžΆαŸ†αžšαž„αŸ‹αž‘αžΌαž›αŸ…αžšαž”αžŸαŸ‹ Class
  • 7. 7 OOP Class Vehicle{ int passengers; //number of passengers int fuelcap; //fuel capacity in liters int kpl; //fuel consumption in kilometers per liter } αž€αžΆαžšαž”αž›αž„αž€αžΎ Class o Class αž’αžΆαž…αž”αž›αž„αž€αžΎ αž“αžΌαžœαžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™ (Data type) αžαŸ’αž˜αžΈαž˜αž½αž™ αŸ” αž€αž“ αž»αž„αž€αžšαžŽαžΈαž›αž“αŸ„αŸ‡ αžŸαŸ’αžšαž”αž›αŸαž‘ αž‘αž·αž“αž“αž“αŸαž™αžαŸ’αž˜αžΈαž› αŸ„αŸ‡αž›αŸ’αž˜ αŸ„αŸ‡αžαžΆβ€‹ Vehicle αž›αž αžΎαž™αž›αž™αžΎαž„αž“αžšαž„αž›αžŸαŸ’αžšαž”αžΎαž›αŸ’αž˜ αŸ„αŸ‡αž›αž“αŸ„αŸ‡αž› αžΎαž˜αž”αžΈαžŸαŸ’αžšαž”αž€αžΆαžŸ Objects αŸ” Vehicle minivan = new Vehicle();
  • 8. OOP  Object αž‚αžΊαž‡αžΆ Instance αžšαž”αžŸαŸ‹ ClassαŸ”  Object αž‡αžΆαž’αŸ’αžœαžΈαŸ—αž αž›αž›αŸ…αž‡αžΆαŸ†αžœ αž·αž‰αžαž› αž½αž“αž›αž™αžΎαž„ αžΌαž…αž‡αžΆ αž€αž„αŸ‹ αž˜αŸ‰αžΆαžΌ αžΌβ€‹αž‘αžΆαž“ αž›αžŸαŸ€αžœαž›αŸ… αž” αŸŠαž·αž… αž€αžΆαŸ†αŸαž™αžΌ αž‘αŸαžšβ€¦αŸ” αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Object ?  Object αž˜αžΆαž“ State αž“αž·αž„ BehaviorαŸ” οƒΌ State αž‡αžΆαž›αž€αžαžŽαŸˆ ឬ αž’αžΆ αž•αŸ’αžŸαžΆαŸ†αžŸαžŸαŸ’αžšαž˜αžΆαž”αŸ‹αž”αž›αž„αž€αžΎ Object αŸ” Ex1: αž˜αž“αžŸαžŸαž˜αžΆαž“ αžŸαž˜αž”αž» រ αž€αžΆαŸ†αŸαžŸαŸ‹ αž’αžΆαž™ αž›αŸαž‘ αŸ” Ex2:αž˜αŸ‰αžΆαžΌ αžΌαž˜αžΆαž“ αž€αž„αŸ‹ αžαž€αž” αžŸαŸ’αžšαž…αžœαŸ‰αžΆ αž€αŸ‹ αž›αŸαž›αžΎαž„αžŸαžΈαž‰αŸ‰αžΆαžΌβ€¦αŸ” οƒΌ Behavior αž‡αžΆαž’αŸ’αžœαžΈαž αž› Object αž’αžΆαž…αž›αŸ’αžœαžΎαž”αžΆαž“αŸ” Ex1: αž˜αž“αžŸαžŸαž˜αžΆαž“ αž› ើរ αž› αž€ αž’αŸ’αž„αž‚ αž»αž™ ឈរ αž™αžΆαŸ† αž›αžŸαžΎαž…β€¦.αŸ” Ex2:αž‘αžΌαžš ATM αž˜αžΆαž“ αž€αžŸαŸ’αžšαž”αžΆαž€αŸ‹ αž€αŸ‹αžŸαŸ’αžšαž”αžΆαž€αŸ‹ …. αŸ”
  • 9. 9 OOP αžšαž›αž”αŸ€αž”αž”αž›αž„αž€αžΎ Objects Vehicle minivan = new Vehicle(); β€’ αž›αž™αžΎαž„αžŸαŸ’αžšαž”αž€αžΆαžŸαž’αŸ’αž‰αŸ’αž‰ αž·αžαž˜αž½αž™αž˜αžΆαž“αž›αŸ’αž˜ αŸ„αŸ‡αžαžΆ minivan ត αž›αž˜αžΆαž“ Vehicle αž‡αžΆαžŸαŸ’αžšαž”αž›αŸαž‘ αž‘αž·αž“αž“αž“αŸαž™ (Reference of Object)αŸ” β€’ new operator αž”αž›αž„αž€αžΎ αž‘αžΈαžΆαžΆαŸ†αž„αž€αž“ αž»αž„β€‹Memory αžšαž”αžŸαŸ‹ Object αž˜αž½αž™αž›αž αžΎαž™αž•αŸ’αžαž›αŸ‹ Reference αž›αŸ…αž²αŸ’αž™ Object αž› αŸ„αŸ‡ αŸ” β€’ Reference αž‡αžΆαž’αžΆαžŸαŸαž™ អ αž“αž“αž“ Object αž›αŸ…αž€αž“ αž»αž„ Memory ត αž›αž”αž›αž„αž€αžΎ αž‘αžΈαžΆαžΆαŸ†αž„αž› αž™ new αŸ”
  • 10. 10 OOP αžšαž›αž”αŸ€αž”αž”αž›αž„αž€αžΎ Methods ret-type methName(param-list){ //body of method } οƒΌ ret-type αž”αž‰αŸ’αž αžΆ αž€αŸ‹αŸαžΈαžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™αž αž›αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž•αŸ’αžαž›αŸ‹αž²αŸ’αž™ Method αŸ” αžœαž’αžΆαž…αž‡αžΆ αžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™αŸ’αž˜αž˜αžΆ ឬ αž’αžΆαž…αžŸαŸ’αžšαž”αž›αŸαž‘αž‡αžΆβ€‹Class ត αž›αž›αž™αžΎαž„αž”αžΆαž“αž”αž›αž„αž€αžΎ αž› αžΎαž„ αŸ” οƒΌ Method αž˜αž·αž“αžŸαŸ’αžš αžΌαžœαž€αžΆαžšαž•αŸ’αžαž›αŸ‹ αž“αž˜αž›αž›αž‘ αž› αŸ„αŸ‡αžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™αžšαž”αžŸαŸ‹αžœαžŸαŸ’αžš ូវត αž‡αžΆ void αŸ” οƒΌ methName αž‡αžΆαž›αŸ’αž˜ αŸ„αŸ‡αžšαž”αžŸαŸ‹ Method αŸ” οƒΌ param-list αž‡αžΆαžŸαŸ’αžšαž”αž›αŸαž‘αž‘αž·αž“αž“αž“αŸαž™ αž“αž·αž„ αž›αŸ’αž˜ αŸ„αŸ‡αž’αŸ’αž‰αŸ’αž‰ αžαž·αž›αžŸαŸ’αžšαž”αžΎαž”αž“αžαž‚αŸ’αž“αž“ ត αž›αž“αžšαž„αžαž‰αž€αž•αŸ’αžαž αž…αŸ‹αŸαžΈαž‚αŸ’αž“αž“ αž› αž™ αžŸαž‰αŸ’αž αž‰ αž›αž€αž”αŸ€αžŸ (,)αŸ”
  • 11. 11 Encapsulation  Encapsulation αžœαž‡αžΆαžαž•αŸ’αž“αž€αž˜αž½αž™αž“αž“ OOP conceptαŸ”  αž›αŸ’αžœαžΎαž›αž’αžΆαž™ Field αž›αŸ…αž€αž“ αž»αž„ class αž˜αž½αž™αž˜αž·αž“αž’αŸ’αž“αž‰αŸ’αž αž‰ αž›αž’αžΆαž™αž˜αžΆαž“αž€αžΆαžš access αž…αžΌαž›αŸαžΈ class αž“αž‘αŸ”αž›αž–αžΆαž›αž‚αžΊ variable αž›αŸ…αž€αž“ αž»αž„ class αž˜αž½αž™αžŸαŸ’αžš αžΌαžœαž”αžΆαž“ hide េី class αž“αž‘ αž›αž αžΎαž™αžœαž’αžΆαž… access αž”αžΆαž“αžΆαž˜αžšαž™αŸˆ method getter() αž“αž·αž„β€‹setter()αŸ” αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Encapsulation ?
  • 12. 12 Encapsulation  Field αžšαž”αžŸαŸ‹ class αž’αžΆαž…αž€αžΆαŸ†αžŽ αŸ‹αž‡αžΆ Read-only ឬ Write-only.  Class αž’αžΆαž…αžŸαŸ’αžšαž‚αž”αŸ‹αžŸαŸ’αžšαž‚αž„αž‘αžΆαŸ†αž„αžŸαŸ’αžšαžŸ αž»αž„αž›αŸ…αž›αž›αžΎ data αž› αž™αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹ codeαŸ” αž‚αžŽαžŸαž˜αž” αžαž·αžšαž”αžŸαŸ‹ Encapsulation ?  αž’αŸ’αž“αž€αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹ class αž˜αž·αž“ αžšαž„αŸαžΈαžšαž›αž”αŸ€αž”αž αž› class αž•αŸ’αž‘ αž»αž€(store) αž‘αž·αž“αž‘αž“αŸαž™αŸ”  Class αž’αžΆαž…αž”αž αžΌαžšαž“αžΌαžœ data type αž“αž“ field αž˜αž½αž™αŸ”  αž’αŸ’αž“αž€αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αŸ‹ class αž˜αž·αž“αžŸαŸ’αžš αžΌαžœαž€αžΆαžšαž”αž ូរ code αžšαž”αžŸαŸ‹αž›αž‚αŸ”
  • 13. 13 Encapsulation public class TestJava { private int id; private String name; private int age; public void setId(int id){this.id=id;} public int getID(){return id;} public void setName(String name){this.name=name;} public String getName(){return name;} public void setAge(int age){ if(age<0){ this.age=1; }else{ this.age=age; } } public int getAge(){return age;} } Public class DemoEncap{ public static void main(String args[]){ TestJava obj= new TestJava(); obj.setId(100); obj.setName("Hong"); obj.setAge(-1); System.out.println("ID:"+ obj.getID() + "nName:"+ obj.getName()+ "nAge:"+ obj.getAge()); } }
  • 14. 14 Inheritance αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Inheritance?  Inheritance αž‚αžΊαž‡αžΆαž›αž•αŸ’αž‘αžšαž˜αžš αž€αŸαžΈ Class αž˜αž½αž™αž›αŸ… Class αž˜αž½αž™αž›αž•αŸ’αžŸαž„αž›αž‘αŸ€ αŸ”  Class ត αž›αž›αž•αŸ’αž‘αžš αž“αž˜αž›αž›αž’αžΆαž™αž›αž‚αž›αŸ…αžαžΆ Parent Class αž›αž αžΎαž™ Class ត αž›αž‘αž‘αž½αž› αž“αž˜αž›αž›αŸ…αžαžΆ Child ClassαŸ”  Inheritance αž’αŸ’αž“αž‰αŸ’αž αž‰ αž·αž²αŸ’αž™ Class αž˜αž½αž™αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αž›αŸ… properties αž“αž·αž„ methods αž“αž“ class αž›αž•αŸ’αžŸαž„αž›αž‘αŸ€ αž”αžΆαž“ αŸ”  Inheritance αž‚αžΊαž‡αžΆ compile-time mechanism αŸ” super-class αž’αžΆαž…αž˜αžΆαž“ subclasses αž“αž“αž˜αž½αž™αž…αžΆαŸ†αž“αž½αž“αŸ” αž”αŸ‰αžΆαžαž“αž subclass αž˜αžΆαž“αžŸαŸ’αžš រមត super-class αž˜αž½αž™αž”αŸ‰αžΆαž›αŸ’αžŽ αŸ„αŸ‡ αŸ”
  • 15. 15 Inheritance αž› ើ Inheritance αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž…?  Extends: αž›αŸ…αž€αž“ αž»αž„Java αž› αžΎαž˜αž”αžΈαž›αŸ’αžœαžΎαž€αžΆαžš Inheritance េី Parent Class αž›αŸ…Child Class αž›αž™αžΎαž„αžŸαŸ’αžš αžΌαžœαž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹ Keyword ExtendsαŸ”  Super: αž›αž‚αž›αžŸαŸ’αžšαž”αžΎαžœαžŸαžŸαŸ’αžšαž˜αžΆαž”αŸ‹αž€αžΆαŸ†αžŽ αŸ‹αž›αž€αžαžŽαŸˆMemberαžšαž”αžŸαŸ‹ Classαž›αž˜ αž€αžšαžŽαžΈ ត αž› Method αž“αž·αž„ Variable αžšαž”αžŸαŸ‹ Classαž›αž˜αž“αž·αž„ Classαž€αžΌαž“ αžΌαž…αž‚αŸ’αž“αž“ αŸ”  Implement: αž‡αžΆKeyword αž˜αž½αž™αž αž›αž’αŸ’αž“αž‰αŸ’αž αž‰ αž›αž’αžΆαž™ Class αž›αŸ’αžœαžΎαž€αžΆαžšαž‘αž‘αž½αž›αž˜αžš αž€αŸαžΈ InterfaceαŸ”
  • 16. 16 Inheritance class ClassA{ int a=; public void add(int x, int y){ a = x+y; System.out.println("The sum of the given numbers:"+a); }} public class ClassB extends ClassA{ public void multiplication(int x, int y){ a= x*y; System.out.println("The product of the given numbers:"+a); } public static void main(String args[]){ int a = 20, b = 10; ClassB sub = new ClassB (); sub.add(a, b); sub.multiplication(a, b); } }  αž§αž‘αž αžšαžŽαŸˆ αž›αž™αžΎαž„αž˜αžΆαž“ Classេីរ αž‚αžΊ ClassA αž“αž·αž„ClassB ត αž›ClassBαž›αŸ’αžœαžΎαž€αžΆαžš Inheritannce αž“αž˜αž›αŸαžΈ ClassAαŸ”
  • 17. 17 Inheritance  αž§αž‘αž αžšαžŽαŸˆ public class Sub_class extends Super_class { int num = 10; //display method of sub class public void display(){ System.out.println("This is the display method of subclass");} public void my_method(){ Sub_class sub = new Sub_class();//Instantiating subclass sub.display();//Invoking the display() method of sub class super.display();//Invoking the display() method of superclass System.out.println("value of the variable named num in sub class:"+ sub.num); //printing the value of variable num of superclass System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]){ Sub_class obj = new Sub_class(); obj.my_method(); } class Super_class{ int num = 20; //display method of superclass public void display(){ System.out.println("This is the display method of superclass");}}
  • 18. 18 Inheritance  αž§αž‘αž αžšαžŽαŸˆ Public Interface Motor{ void speedUp(int increment); } class MyMotor implement Motor{ int speed = 0; void speedUp(int increment) { speed = speed + increment; } void printStates() { System.out.println("My motor speed" +speed); } }
  • 19. 19 Inheritance αžŸαŸ’αžšαž”αž›αŸαž‘αž“αž“ Inheritance  Single inheritance in java  multilevel inheritance  multiple inheritance  hybrid inheritance  hierarchical inheritance
  • 20. 20 Abstraction  αžšαžΆαž›αŸ‹ programming languages ត αž„αž αž•αŸ’αžαž›αŸ‹ abstractions αŸ”  αžœαž‡αž½αž™αž•αŸ’αžαž›αŸ‹αž›αŸ… αžΆαŸ†αž›αŸ’αŸ„αŸ‡αžŸαŸ’αžšαžŸαž™αž αž›αž˜αžΆαž“αž—αžΆαŸαžŸαž˜ αž»αž‚αžŸαž˜ αž‰αž“αž“αž”αž‰αŸ’αž αžΆ (αž› αŸ„αŸ‡αžŸαŸ’αžšαžŸαž™ αž›αŸ‹αž•αŸ’αžαž‘ αž›αŸ‹αž›αŸ…αžΆαž˜αžŸαŸ’αžšαž”αž›αŸαž‘ αž“αž·αž„ αž‚αžŽαžŸαž˜αž” αžαž·αž“αž“ abstraction αŸ” αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Abstraction?  Abstraction αžœαž‡αžΆαž’αžΆ αžŸαžΆαŸ†αžαžΆαž“αŸ‹αž˜αž½αž™αž“αž“ object-oriented programming  Abstraction αž‡αžΆ αžΆαŸ†αž›αžŽαžΎαžšαž€αžΆαžšαž“αž“αž€αžΆαžšαŸ„αž€αŸ‹αž”αžΆαžΆαŸ†αž„αž›αžŸαž…αž€αžαžΈαž›αžΆαŸ†αž’αŸ’αž· αž“αž“αž€αžΆαžšαž’αŸ’αž“αžœ αžαž“αŸ( implementation)αž›αž αžΎαž™αž”αž„αŸ’αž‚αžΆ αž‰αž ួ αž‘αžΈ ត αž˜αž½αž™αž‚ αŸ‹αž›αŸ… αž›αŸ‹αž’αŸ’αž“αž€αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αŸ”
  • 21. 21 Abstraction  Abstract class αž‡αžΆβ€‹class αž˜αž½αž™αž αž›αžŸαŸ’αžšαž”αž€αžΆαžŸαž‡αžΆ abstract αŸ”  Abstract method αŸ’αžΊαž‡αžΆ methodαž˜αž½αž™αž αž›αžŸαŸ’αžšαž”αž€αžΆαžŸαž‡αžΆ abstract αŸ” αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Abstract class?  Abstract Class αž€αžšαž€αžΆαž‘αž€αž›αŸ… abstract method αž˜αž½αž™αž¬αž›αžŸαŸ’αžšαž…αžΎαž“αŸ” Syntax: abstract class (class name){ //abstract methods }
  • 22. 22 Abstraction αž…αžΆαŸ†αŸ’αžΆαŸ†αŸ– o αž‚αž”αžŸαž·αž“αž‡αžΆαž˜αžΆαž“ abstract method αž…αž‚αžšαžΎαž“αž…αŸ…αž€αŸ’αž“αž»αž„ class αžαŸ‚αž˜αž½αž™ αž… αžΉαŸ‡ class αž‚αŸ‚αžΌαžœαžαŸ‚αž‡αžΆ abstract αŸ” o αž‚αž”αžŸαž·αž“αž‡αžΆαž’αŸ’αž“αž€αŸ’αž€αŸ’αŸ†αž–αž»αž„αžαŸ‚ extend abstract classαž…αž‚αžšαžΎαž“αžαŸ‚αž›αž˜αžΆαž“ abstract method αž’αŸ’αž“αž€αŸ’αž˜αž·αž“αž‚αŸ‚αžΌαžœαž•αŸ’αžαž›αŸ‹αž€αžΆαžš implementationαž…αŸ…αž…αŸ„αž™ method ឬ αž…αŸ’αžœαžΎαž…αŸ„αž™ class αž… αžΉαŸ‡ αž€αžΆαžΆ αž™αž‡αžΆ abstract classαŸ”
  • 23. 23 Abstraction αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Interface ?  Interface αž›αž˜αžΎαž›αž›αŸ… αžΌαž…αž‡αžΆ class αž”αŸ‰αžΆαžαž“αžαžœαž˜αž·αž“αžαž˜αž“αž‡αžΆ class αž›αž‘αŸ”  Interface αž‚αžΊαž›αžŸαŸ’αžšαž”αžΎαž›αŸ… default abstract (αž˜αžΆαž“αžŸαŸ’αžš រមត method signature αž‚αŸ’αž“αž˜ αž“ body αž›αž‘)αŸ”  Interface αž› ើរ ួ αž‘αžΈαž‡αžΆ blueprint (αž”αž›αž„αŸ‹)αžŸαžΆαŸ†αžšαžΆαž”αŸ‹ class αŸ”  Interface αž˜αž·αž“αž’αžΆαž…αž”αž›αž„αž€αžΎ object αž”αžΆαž“αž›αž‘αŸ”
  • 24. 24 Abstraction αž›αž  αž’αŸ’αžœαžΈαž›αž™αžΎαž„αžŸαŸ’αžš αžΌαžœαž›αžŸαŸ’αžšαž”αžΎ Interface ?  αž›αŸ’αžœαžΎαž²αŸ’αž™ abstract αž˜αž½αž™β€‹β€‹β€‹αž˜αžΆαž“αž›αž€αžαžŽαŸˆαž›αŸαž‰αž›αž›αž‰ αŸ”  αžΆαž˜αžšαž™αŸˆ interface αž›αž™αžΎαž„αž’αžΆαž…αžŸαŸ’αžšαž” αž·αž” αžαž·αž€αžΆαžšαž˜αžαž„αŸ’αž‚αžšαžšαž”αžŸαŸ‹ multi-inheritanceαŸ”  αžœαž’αžΆαž…αžŸαŸ’αžš αžΌαžœαž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αž› αžΎαž˜αž”αžΈαž”αžΆαŸ†αž›αŸαž‰ loose coupling αŸ”
  • 25. 25 Abstraction 1. Abstract class VS Interface: αž—αžΆαŸαžŠαžΌαž…αž‚αŸ’αž“αžΆ αžœαžΆαž„β€‹Abstract class & Interface:  αž’αžΆαž…αž²αŸ’αž™ abstraction αž“αž·αž„αž’αžΆαž…αžŸαŸ’αžšαž”αž€αžΆαžŸ abstract method αŸ”  Abstract class & Interface αž˜αž·αž“αž’αžΆαž…αž‡αžΆ instantiatedαŸ”
  • 26. 26 Abstraction Abstract class Interface 1) Abstract class αž’αžΆαž…αž˜αžΆαž“αž¬αž‚αŸ’αž“αž˜ αž“(0%-100%) abstract methodαŸ” Interface αžŸαŸ’αžš ូវត αž˜αžΆαž“ (100%) abstract methodαŸ” 2) Abstract class αž˜αž·αž“β€‹β€‹support multiple inheritanceαŸ” Interface supports multiple inheritance. 3) Abstract class αž’αžΆαž…αž˜αžΆαž“ final, non-final, static and non-static variablesαŸ” Interface αž’αžΆαž…αž˜αžΆαž“ static and final variables αž”αŸ‰αžΆαž›αŸ’αžŽ αŸ„αŸ‡αŸ” 4) Abstract class αž’αžΆαž…αž˜αžΆαž“ static methods, main method and constructorαŸ” Interface αž˜αž·αž“αž˜αžΆαž“ static methods, main method and ConstructorαŸ” 5) Abstract class αž’αžΆαž…αž•αŸ’αžαž›αŸ‹β€‹implementation αžšαž”αžŸαŸ‹interfaceαŸ” Interface αž˜αž·αž“αž’αžΆαž…αž•αŸ’αžαž›αŸ‹β€‹implementation αžšαž”αžŸαŸ‹ abstract class αž”αžΆαž“αž›αž‘αŸ” 6) The abstract keyword αž‚αžΊαž›αžŸαŸ’αžšαž”αžΎαž› αžΎαž˜αž”αžΈαžŸαŸ’αžšαž”αž€αžΆαžŸαŸ‹ abstract classαŸ” The Interface keyword αž‚αžΊαž›αžŸαŸ’αžšαž”αžΎαž› αžΎαž˜αž”αžΈαžŸαŸ’αžšαž”αž€αžΆαžŸαŸ‹ InterfaceαŸ” 7) Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); } αž—αžΆαŸαžαžŸαž‚αŸ’αž“αžΆ αžœαžΆαž„β€‹Abstract class & Interface:
  • 27. 27 Polymorphism  Polymorphism αž˜αž€αŸαžΈαž—αžΆαžŸαž›αžŸαŸ’αžšαž€αž€β€‹αŸαžΈαžšαž–αžΆαž€αž™αž‚αžΊ poly αž“αž·αž„ morphs αŸ”  Poly αž˜αžΆαž“αž“αŸαž™αžαžΆβ€‹αž›αžŸαŸ’αžšαž…αžΎαž“ αž“αž·αž„ morphs αž˜αžΆαž“αž“αŸαž™αžαžΆ αž‘αžΆαŸ†αžšαž„αŸ‹ αŸ”  polymorphism αž˜αžΆαž“αŸαžΈαžšαžŸαŸ’αžšαž”αž›αŸαž‘αž›αŸ…αž€αž“ αž»αž„ java αž‚αžΊ 1) compile time polymorphism(static polymorphism) 2) runtime polymorphism (Dynamic polymorphism ) αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Polymorphism?  αž›αž™αžΎαž„αž’αžΆαž…αž’αŸ’αž“αžœ ត polymorphism αž›αŸ…αž€αž“ αž»αž„ java αž› αž™ method overloading αž“αž·αž„ method overriding αŸ”
  • 28. 28 Polymorphism αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Runtime Polymorphism ?  Method overriding αž‚αžΊαž‡αžΆαž§αž‘αž αžšαžŽαŸ αž›αž’ αž…αžΆαŸ†αž›αž–αžΆαŸ„αŸ‡ runtime polymorphism αŸ” Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();  αž›αžΆαŸ„αŸ‡αž›αž™αžΎαž„αž›αŸ…αž›αž˜αžΎαž›αž§αž‘αž αžšαžŽαŸαž› αžΎαž˜αž”αžΈαž™αž›αŸ‹αž€αžΆαž“αŸ‹αž…αžΆαžŸαŸ‹ public class X { public void methodA() //Base class method { System.out.println ("hello, I'm methodA of class X"); } } public class Y extends X { public void methodA() //Derived Class method { System.out.println ("hello, I'm methodA of class Y"); } } public class Z { public static void main (String args []) { X obj1 = new X(); // Reference and object X X obj2 = new Y(); // X reference but Y object obj1.methodA(); obj2.methodA(); } } Output: hello, I'm methodA of class X hello, I'm methodA of class Y
  • 29. 29 Polymorphism αž› αžΎαž’αŸ’αžœαžΈαž›αŸ…αž‡αžΆ Compile time Polymorphism ?  Method overloading αž‚αžΊαž‡αžΆαž§αž‘αž αžšαžŽαŸ αž›αž’ αž…αžΆαŸ†αž›αž–αžΆαŸ„αŸ‡ compile time polymorphism αŸ” Y obj = new Y(); //Parent class reference can be assigned to child object X obj = new Y();  αž›αžΆαŸ„αŸ‡αž›αž™αžΎαž„αž›αŸ…αž›αž˜αžΎαž›αž§αž‘αž αžšαžŽαŸαž› αžΎαž˜αž”αžΈαž™αž›αŸ‹αž€αžΆαž“αŸ‹αž…αžΆαžŸαŸ‹ class X { void methodA(int num) { System.out.println ("methodA:" + num); } void methodA(int num1, int num2) { System.out.println ("methodA:" + num1 + "," + num2); } double methodA(double num) { System.out.println("methodA:" + num); return num; } } class Y { public static void main (String args []) { X Obj = new X(); double result; Obj.methodA(20); Obj.methodA(20, 30); result = Obj.methodA(5.5); System.out.println("Answer is:" + result); } } Output: methodA:20 methodA:20,30 methodA:5.5 Answer is:5.5
  • 30. 30 Polymorphism  αž’αŸ’αž“αž‰αŸ’αž αž‰ αž·αž²αŸ’αž™ interface αž˜αž½αž™αž’αžΆαž…αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αžŸαŸ’αžšαž”αžΎαžŸαŸ’αžšαž”αžΆαžŸαŸ‹αž‡αžΆαž‘αžΌαž›αŸ…αž›αŸ…αž€αž“ αž»αž„ classαŸ”  αžŸαŸ’αžšαž” αž·αž” αžαž·αž€αžΆαžšαž˜αž½αž™αž’αžΆαž…αž˜αžΆαž“ behavior αžαžŸαž‚αŸ’αž“αž“ αž›αŸ…αž€αž“ αž»αž„αž€αžšαžŽαžΈαž›αž•αŸ’αžŸαž„αž‚αŸ’αž“αž“ αŸ” αž› ើ Polymorphism αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž…?  Behavior αŸαžšαž„αžαž•αŸ’αž’αž€αž›αŸ…αž›αž›αžΎαžŸαŸ’αžšαž”αž›αŸαž‘αž“αž“ data ត αž›αž”αžΆαž“αž›αžŸαŸ’αžšαž”αžΎαž›αŸ…αž€αž“ αž»αž„αž›αŸαž›αž’αŸ’αž“αžœ αžαŸ”  αžœαž› ើរ ួ αž‘αžΈαž™αŸ‰αžΆ αž„αžŸαžΆαŸ†αžαžΆαž“αŸ‹αž›αŸ…αž€αž“ αž»αž„αž€αžΆαžšαž’αŸ’αž“αž‰αŸ’αž αž‰ αž·αž²αŸ’αž™ object ត αž›αž˜αžΆαž“ internal structures αž› αžΎαž˜αž”αžΈαžαž…αž€αžšαŸ†αžαž›αž€ external interface αŸ”  Polymorphism αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αž‚αž›αžŸαŸ’αžšαž”αžΎαž™αŸ‰αžΆ αž„αž‘αžΌαž›αžΆαŸ†αž‘αžΌαŸ„αž™αž€αž“ αž»αž„αž€αžΆαžš implement inheritance αŸ”
  • 31. 31 Polymorphism αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆβ€‹Method Overloading ?  Method Overloading αž˜αžΆαž“αž›αŸ’αž˜ αŸ„αŸ‡ αžΌαž…αž‚αŸ’αž“αž“ β€‹αž αžαžŸαž‚αŸ’αž“αž“ αž›αŸ…αž…αžΆαŸ†αž“αž½αž“ αž“αž·αž„ data type αž“αž“ parameter αŸ” class Overload { void demo (int a) { System.out.println ("a: " + a); } void demo (int a,int b){ System.out.println ("a and b: " + a + "," + b); } double demo(double a) { System.out.println("double a: " + a); return a*a; } } class MethodOverloading { public static void main (String args []) { Overload Obj = new Overload(); double result; Obj .demo(10); Obj .demo(10, 20); result = Obj .demo(5.5); System.out.println("O/P : " + result); } } Output: a: 10 a and b: 10,20 double a: 5.5 O/P : 30.25
  • 32. 32 Polymorphism αž› ើ Method Overloading αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž… ? 1. Method Overloading αž’αžΆαž…αž›αŸ’αžœαžΎαž€αžΆαžšαž›αŸ…αž€αž“ αž»αž„ class αžΌαž…αž‚αŸ’αž“αž“ ឬ αž›αŸ…αž€αž“ αž»αž„ sub-class αžšαž”αžŸαŸ‹αžœ αŸ” 2. Constructor αž›αŸ…αž€αž“ αž»αž„ Java αž’αžΆαž…αž‡αžΆ Overloaded 3. Overloaded methods αž‚αž½αžšαž αž˜αžΆαž“ argument list αžαžŸαž‚αŸ’αž“αž“ 4. Parameter αž’αžΆαž…αžαžŸαž‚αŸ’αž“αž“ αž›αŸ…αž›αž›αžΎ type ឬ number αž¬αž€ αž‘αžΆαŸ†αž„αŸαžΈαžšαž αž˜αžαž„ αŸ” 5. αŸαž½αž€αžœαž’αžΆαž…αž˜αžΆαž“ return types αžαžŸαž‚αŸ’αž“αž“ ឬ αžΌαž…αž‚αŸ’αž“αž“ αŸ” 6. αžœαž€αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αž‚αž›αŸ…αžαžΆαž‡αžΆ compile time polymorphism αŸ”
  • 33. 33 Polymorphism αž› αžΎαž’αŸ’αžœαžΈαž‡αžΆβ€‹Method Overriding ?  Method Overriding αž˜αžΆαž“αž›αŸ’αž˜ αŸ„αŸ‡ αžΌαž…αž‚αŸ’αž“αž“ β€‹αž…αžΆαŸ†αž“αž½αž“ αž“αž·αž„ data type αž“αž“ parameter αž€ αžΌαž…αž‚αŸ’αž“αž“ αŸ” public class BaseClass { public void methodToOverride() //Base class method { System.out.println ("I'm the method of BaseClass"); } } public class DerivedClass extends BaseClass { public void methodToOverride() //Derived Class method { System.out.println ("I'm the method of DerivedClass"); } } public class TestMethod { public static void main (String args []) { // BaseClass reference and object BaseClass obj1 = new BaseClass(); // BaseClass reference but DerivedClass object BaseClass obj2 = new DerivedClass(); // Calls the method from BaseClass class obj1.methodToOverride(); //Calls the method from DerivedClass class obj2.methodToOverride(); } } Output: I'm the method of BaseClass I'm the method of DerivedClass
  • 34. 34 Polymorphism αž› ើ Method Overriding αž›αŸ’αžœαžΎαž€αžΆαžšαž™αŸ‰αžΆ αž„ αžΌαž…αž›αž˜αžαž… ? 1. αž’αŸ’αž“αž‰αŸ’αž αž‰ ិ ត αž…αžΆαŸ†αž›αž–αžΆαŸ„αŸ‡ inherited methods 2. Object type (αž˜αž·αž“αžαž˜αž“αž‡αžΆ reference variable type)αž› αž™αž€αžΆαŸ†αžŽ αŸ‹ overridden method αž“αž·αž„αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αžŸαŸ’αžšαž”αžΎαž›αŸ…αž›αŸαž› runtime 3. Overriding method αž’αžΆαž…αž˜αžΆαž“ return type αžαžŸαž‚αŸ’αž“αž“ 4. Overriding method αž˜αž·αž“αžŸαŸ’αžš αžΌαžœαž˜αžΆαž“ restrictive access modifier αŸ” 5. Abstract methods αžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž’αžΆαž… overridden 6. Static αž“αž·αž„ final method αž˜αž·αž“αž’αžΆαž… overridden 7. αžœαžŸαŸ’αžš αžΌαžœαž”αžΆαž“αž›αž‚αžŸαžΆ αž›αŸ‹αžαžΆαž‡αžΆ Runtime polymorphism αŸ”