งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Chapter 6 Abstract Class and Interface

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Chapter 6 Abstract Class and Interface"— ใบสำเนางานนำเสนอ:

1 Chapter 6 Abstract Class and Interface
ความหมายของ Abstraction วัตถุประสงค์การสร้าง Abstract Class และ Interface การใช้งาน Abstract Class และ Interface การทำ multiple inheritance ข้อแตกต่างระหว่าง Abstract Class และ Interface

2 Abstract Class หมายถึงคลาสที่ประกาศนำหน้าด้วย abstract
สามารถมีทั้ง abstract และ non-abstract methods (method ที่มีแต่โครงของ method body).

3 Abstraction in Java Abstraction เป็นซ่อนการรายละเอียดของคำสั่งใน method จะอนุญาตให้ผู้ใช้ทราบเพียงว่า method ทำอะไรได้เท่านั้น เช่นการส่ง sms เราเพียงแต่พิมพ์ข้อความและส่งเท่านั้น โดยไม่รู้ขบวนการภายในในการส่ง message เลย Abstraction จะมุ่งเน้นเฉพาะว่า วัตถุทำอะไรได้บ้าง โดยไม่รู้ว่าทำอย่างไร

4 การสร้าง Abstraction ในจาวา
สามารถสร้างได้ 2 อย่างคือ Abstract class (ทำได้ตั้ง 0 to 100%) Interface (ทำได้สมบูรณ์แบบ 100%) ทั้ง 2 วิธีเราจะเรียนในบทนี้

5 Abstract class in Java ก็คือ class ใดๆที่ประกาศว่าเป็น abstract
ในการนำไปใช้จำเป็นต้องทำการขยาย(extend)และเขียนคำสั่งเพิ่มใน method ให้ทำงานตามต้องการ ตัวอย่างการประกาศ abstract class A{}  

6 abstract method เป็น method ประกาศให้เป็น abstract ซึ่งจะต้องไม่มีการเขียนคำสั่งใดๆเลยใน method นั้น เช่น abstract void printStatus();

7 Example of abstract class that has abstract method
abstract class Bike{     abstract void run();   }      class Honda4 extends Bike{   void run(){System.out.println("running safely..");}   public static void main(String args[]){    Bike obj = new Honda4();    obj.run();   Ountput :running safely..

8 ตัวอย่างการใช้งานงานจริง Abstract Class
abstract class Shape{ abstract void draw(); } class Rectangle extends Shape{ void draw(){System.out.println("drawing rectangle");}

9 -ต่อ- class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}   }   class TestAbstraction1{   public static void main(String args[]){   Shape s=new Circle1(); s.draw();   Output : drawing circle

10 Another example of abstract class in java
abstract class Bank{ abstract int getRateOfInterest(); } class SBI extends Bank{ int getRateOfInterest(){return 7;} } class PNB extends Bank{ int getRateOfInterest(){return 9;} class TestBank{ public static void main(String args[]){ Bank b=new SBI(); int interest=b.getRateOfInterest(); System.out.println("Rate of Interest is: "+interest+" %"); }} Output: Rate of Interest is: 7 %

11 Abstract class having constructor, data member, methods etc.
//example of abstract class that have method body    abstract class Bike{      Bike(){System.out.println("bike is created");}      abstract void run();      void changeGear(){System.out.println("gear changed");}    }    class Honda extends Bike{    void run(){System.out.println("running safely..");}    class TestAbstraction2{    public static void main(String args[]){     Bike obj = new Honda();     obj.run();     obj.changeGear();   }   Output : bike is created running safely.. gear changed

12 กฎเกี่ยวกับ Abstract กฎ: ถ้ามี method ใดๆในคลาสที่ระบุเป็น abstract method เกิดขึ้น คลาสนั้นต้องระบุเป็น abstract ด้วย เช่น class Bike12{   abstract void run();   }   Output: compile time error กฎ : ถ้าเรา extend คลาสใดๆที่เป็น abstract class ที่มีabstract method เราต้องทำการ implement เมธอดนั้น มิฉะนั้นเราต้องให้คลาสที่ extend นี้เป็น class abstract ด้วย

13 Interface interface ในภาษา java จะถือว่าเป็นแม่แบบ(blueprint)การทำงาน ของ class จะมีสมาชิกที่เป็นเพียง static constants abstract methods เป็นกลไกในการสร้าง abstraction โดยแท้จริง. abstract methods จะประกาศแต่เพียงส่วนหัวเท่านั้นไม่ต้องระบุส่วนของ body สามารถสนับให้เกิด multiple inheritance สนับสนุนความสัมพันธ์แบบ IS-A relationship. ไม่สามารถสร้างวัตถุได้เหมือน abstract class.

14 เหตุผลที่ต้องใช้ interface
เพื่อให้ได้ fully abstraction. ต้องการใช้พฤติกรรมต่างๆที่ผสมกันมาจากหลายที่ด้วยการทำ multiple inheritance. ทำให้เกิดการขึ้นต่อกันของคำสั่งน้อยที่สุด (loose coupling) เพื่อการแก้ไข เพิ่มเติมที่ไม่ส่งผลกระทบต่อกัน Note : java compiler จะเติมคีย์เวิร์ด public ม abstract หน้า interface method และเติมคีย์เวิร์ด public, static และ final หน้า data members ให้โดยอัตโนมัติ : หรือ Interface fields จะเป็น public, static และ final โดยอัตโนมัติ ส่วน methods จะเป็น public และ abstract โดยอัตโนมัติ

15

16 ความสัมพันธ์ระหว่าง classes และ interfaces
class extends another class an interface extends another interface a class implements an interface.

17 Simple example of Java interface
interface printable{   void print();   }      class A6 implements printable{   public void print(){System.out.println("Hello");}   public static void main(String args[]){   A6 obj = new A6();   obj.print();    }   Output:Hello

18 Multiple inheritance in Java by interface
สามารถทำได้ 2 รูปแบบคือ class ต่อเติม(implements)method จากหลาย interfaces interface ขยาย(extends) method จากหลาย interfaces

19 interface Printable{   void print();   }      interface Showable{   void show();   class A7 implements Printable , Showable{   public void print(){System.out.println("Hello");}   public void show(){System.out.println("Welcome");}   public static void main(String args[]){   A7 obj = new A7();   obj.print();   obj.show();    }   Output:Hello Welcome

20 เหตุผลว่าทำไม class จึงทำ multiple inheritance ไม่ได้
เพราะ method ที่ถูก inherit จาก interface มาจะถูก implement ในตัวclass interface Printable{   void print();   }    interface Showable{   }   class testinterface1 implements Printable,Showable{   public void print(){System.out.println("Hello");}   public static void main(String args[]){   testinterface1 obj = new testinterface1();   obj.print();    }   Output: Hello

21 Interface inheritance
interface Printable{   void print();   }   interface Showable extends Printable{   void show();   class Testinterface2 implements Showable{      public void print(){System.out.println("Hello");}   public void show(){System.out.println("Welcome");}   public static void main(String args[]){   Testinterface2 obj = new Testinterface2();   obj.print();   obj.show();    }   Output : Hello Welcome

22 marker or tagged interface
interface ที่ไม่มีสมาชิกเลยเรียกว่า “marker” หรือ “tagged” interface. เช่น: Serializable, Cloneable, Remote ฯลฯ. ใช้เพื่อบอกให้ JVM ทำงานเพิ่มเติมที่สำคัญบางอย่าง เช่น การเขียน Serializable interface    public interface Serializable{   }  

23 การใช้ abstract class ขยาย Interface
เราอาจใช้ abstract class ทำการ implement เพิ่มเติมให้กับ interfaceinterface A{   void a();   void b();   void c();   void d();   }      abstract class B implements A{   public void c(){System.out.println("I am C");}  

24 class M extends B{   public void a(){System.out.println("I am a");}   public void b(){System.out.println("I am b");}   public void d(){System.out.println("I am d");}   }   class Test5{   public static void main(String args[]){   A a=new M();   a.a();   a.b();   a.c();   a.d();   }}   Output:I am a I am b I am c I am d

25 การใช้ instanceof กับ interface
interface Printable{}   class A implements Printable{   public void a(){System.out.println("a method");}   }   class B implements Printable{   public void b(){System.out.println("b method");}   }     class Call{   void invoke(Printable p){//upcasting   if(p  instanceof A){   A a=(A)p;//Downcasting    a.a();   if(p  instanceof B){   B b=(B)p;//Downcasting    b.b();   }  }  } //end of Call class     

26 -ต่อ- class Test4{ public static void main(String args[]){
Printable p=new B();   Call c=new Call();   c.invoke(p);   }   Output: b method

27 Difference between abstract class and interface

28 Example of abstract class and interface in Java
// มี  4 methods   interface A{   void a(); //bydefault, public and abstract   void b();   void c();   void d();   }      abstract class B implements A{   public void c(){System.out.println("I am C");}  

29 class M extends B{ public void a(){System. out
class M extends B{ public void a(){System.out.println("I am a");} public void b(){System.out.println("I am b");} public void d(){System.out.println("I am d");} } //Creating a test class that calls the methods of A interface class Test5{ public static void main(String args[]){ A a=new M(); a.a(); a.b(); a.c(); a.d(); }} Output: I am a I am b I am c I am d

30 Nested Interface interface printable{ void print();
 interface MessagePrintable{      void msg();    }   }  


ดาวน์โหลด ppt Chapter 6 Abstract Class and Interface

งานนำเสนอที่คล้ายกัน


Ads by Google