1 / 25

JAVA PROGRAMMING

JAVA PROGRAMMING. PART IV. INHERITANCE. Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and adding new capabilities the new classes require.

wilbur
Download Presentation

JAVA PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA PROGRAMMING PART IV

  2. INHERITANCE • Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and adding new capabilities the new classes require. • Newly created classes of objects are derived by absorbing characteristics of existing classes and adding unique characteristics of their own

  3. KEYWORD • Given Class A, and we want to define class B that derive from Class A we used keyword “extends” as Class B extends A { //definition of class B }

  4. Super class & Sub Class • Class A : parent class/base class/super class • Class B : child class/extended class/sub class class A { void printA() {System.out.println(‘A’);} } class B extends A { void printB() {System.out.println(‘B’);} }

  5. Relationship • Sub Classes inherit all member (data&method) from its Super Class except • The private member of Super Class • Constructor of Super Class A A B class InheritTest1 { public static void main(String args[]) { A x = new A(); x.printA(); B y = new B(); y.printA(); y.printB(); } }

  6. KEYWORD “FINAL” • For break extends class class A { int a = 1;} class B extends A { int b = 2; } final class C extends B { int c = 3; } class Inherit2 { public static void main(String args[]){ C z = new C(); System.out.println(z.a+z.b+z.c); } } 6

  7. REFERENCE class A {int x;} class B {int y;} A a = new A(); B b = new B(); a b x y x

  8. Try it! class A { int x = 1;} class B extends A { int y = 2; } class Inherit3 { public static void main(String args[]) { A a = new A(); System.out.println(a.x); B b = new B(); System.out.println(b.x + “,” + b.y); b.x--; // b = a; a = b; System.out.println(a.x); } } 1 1, 2 0

  9. Package • class โดยปรกติจะจัดเก็บต่อหนึ่งไฟล์ .class อยู่แล้วเมื่อตอน compile โปรแกรม • เป็นแหล่งจัดเก็บ class ต่าง ๆ ที่เกี่ยวข้องกันไว้ที่เดียวกัน • ใน package หนึ่งสามารถมี class ไม่จำกัด • ใน package สามารถมี subpackage อีกเท่าใดก็ได้ ปรกติที่เราจัดเก็บไฟล์จาวาไว้ใน ไดเรกทอรี่ Myjava ดังนั้นเรียกได้ว่าไฟล์ได้อยู่ใน package Myjava

  10. Packages • Standard Packages ใน JDK 1.2.x อยู่ในไฟล์ rt.jar และ i18n.jar ซึ่งอยู่ใน C:\JDK1.2x\JRE\LIB java.applet java.net java.awt java.rmi java.beans java.security java.io java.sql java.lang java.util java.math

  11. Package • เราสามารถใช้ class จาก package อื่นได้โดยการ import ไว้ที่ส่วนหัวโปรแกรม เช่น import java.awt.Button; import java.awt.Graphics; import javax.swing.JApplet; import java.awt.*;

  12. Keyword • เราใช้ keyword “package” ในการระบุว่า class ของเราอยู่ใน package ใด package myPackage; public class A { public void print() { System.out.println(“This’s class A”); } } จัดเก็บไฟล์ไว้ที่ C:\Myjava\myPackage

  13. Package • สามารถเรียกใช้ class A ได้ 2 แบบ คือ class PackageTest1{ public static void main(String args[]){ new myPackage.A().print(); } } import myPackage.A; class PackageTest2{ public static void main(String args[]){ new A().print(); } }

  14. Scope of Visibility in Extended Classes

  15. Same Class, Same Package package p1; Class One { public String pub = “pub”; protected String pro = “pro”; String def = “def”; private String pri = “pri”; public void print() { System.out.println(pub+pro+def+pri); } }

  16. Sub Class, Same Package package p1; Class SubCsameP extends One { public void print() { System.out.println(pub+pro+def); } }

  17. Other Class, Same Package package p1; Class OtherCsameP extends One { public void print() { One o = new One(); System.out.println(o.pub+o.pro+o.def); } }

  18. Sub Class, Other Package package p1; Class SubCotherP extends p1.One { public void print() { System.out.println(pub+pro); } }

  19. Other Class, Other Package package p1; Class OtherCotherP { public void print() { p1.One o = new p1.One(); System.out.println(o.pub); } }

  20. Constructor Chaining • เมื่อมีการสร้าง instance ของคลาสลูกขึ้น constructors ของคลาสบรรพบุรุษทั้งฟมก จะถูกทำงาน class A {A() {System.out.println(“A”);}} class B extends A { B() {System.out.println(“B”);} } Class ConstructorChain { public static void main(String args[]) { new B();} } A B

  21. Super Reference class A { int a; void print() { System.out.println(a);} } class B extends A { int a; B(int x, int y){super.a = x; this.a = y;} void print() { super.print(); System.out.println(a);} } class Super1{ public static void main(String args[]){ B b = new B(1,2); b.print(); } } 1 2

  22. Super Reference • ใช้ keyword ว่า “super” แทน class ที่ inherite เพื่อใช้ในการอ้างถึง member ของ super class • ในการอ้าง super จะหมายถึงตัว data member ตัวแรกที่เจอในสายของบรรพบุรุษ เช่น A B C int x Ext A Ext B int x This.x Super.x

  23. Super Constructors class A { A() { System.out.println(‘A’);} A(char c) { System.out.println(c); } } class B extends A { B() { // super(‘a’); System.out.println(‘B’); } } class SuperConstructor { public static void main(String args[]){new B();} }

  24. Shadowing • ถ้าเรากำหนด data member ในคลาสลูกมีชื่อเหมือนกับ data member ในคลาสแม่ ชื่อของลูกจะบัง(shadow)ชื่อในคลาสแม่ class A {int x = 1;} class B extends A {float x = 2.0f;} class shadowing { public static void main(String args[]) { B b = new B(); System.oout.println(b.x); } }

  25. overriding • ถ้าเรากำหนด method ในคลาสลูกมี signature เหมือนกับ method ในคลาสแม่ พฤติกรรมของลูกจะลบล้าง(override)พฤติกรรมในคลาสแม่ class A {void print() {print.out.println(‘A’);}} class B extends A { void print() {print.out.println(‘B’); } class Overriding { public static void main(String args[]) { new B().print(); } }

More Related