YOMEDIA
ADSENSE
Inheritance_Object-oriented programming
109
lượt xem 10
download
lượt xem 10
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Outline: What is inheritance?, Inheritance in Java, Reuse. Readings: Java how to program, chapter 9. Similar things (sharing same set of attributes/operations): a group / a concept, Motorola A910 is a smartphone, Nokia E72 is a smartphone, Lenovo G450 is a laptop.
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Inheritance_Object-oriented programming
- Inheritance Object-oriented programming
- Outline What is inheritance? Inheritance in Java Reuse Readings: Java how to program, chapter 9 Đại học Công nghệ. ĐHQG Hà Nội Inheritance 2
- "is-a" relationship Computer Similar things (sharing same set of PDA Laptop attributes/operations) a group / a concept Smartphone Motorola A910 is a smartphone Nokia E72 is a smartphone Nokia E72 Motorola A910 LenovoG450 Lenovo G450 is a laptop Similar groups (sharing a subset of attributes/operations) a bigger group / a more general concept A smartphone "is a" PDA (Personal Digital Assistant) A PDA "is a" computer A laptop "is a" computer An object of the subgroup "is-a" object of the supergroup Đại học Công nghệ. ĐHQG Hà Nội Inheritance 3
- Inheritance Person name birthday Based on "is-a" relationship getName() Objects of subclass also belongs to superclass Subclass: more specialized, superclass: more general Employee Subclass is derived or inherits from superclass name birthday hence, the terms 'derived class' and 'base class' getName() In essence: salary getSalary() 1. Objects in the same class have the same set of attributes (different values though) and operations 2. Objects of subclass have all members of superclass plus some more Objects of a subclass can also be treated as objects of its superclass Đại học Công nghệ. ĐHQG Hà Nội Inheritance 4
- Person Inheritance Arrow points name: String birthday: Date from + getName(): subclass to String superclass … An Employee “is a” Person, Employee apart salary: double from its own members, + getSalary(): salary and getSalary, double … it also has name, birthday, getName() without having to declare them Employee is the subclass (derived) of Person Person is the superclass (base) of Employee Đại học Công nghệ. ĐHQG Hà Nội Inheritance 5
- Person Arrow points name: String Inheritance birthday: Date + getName(): from subclass to String superclass … Inheritance tree can have many levels Student Employee A Manager object inherits id: String salary: double what an Employee has, … + getSalary(): double including what a Person … has. Manager assistant: Employee + setAssistant() Only new attributes/operations are listed, … inherited attributes/operations are not listed in the subclass's box Đại học Công nghệ. ĐHQG Hà Nội Inheritance 6
- Inheritance in Java Person name: String birthDate: Date + getName(): String How to? … 1. Subclass as an extension of superclass New attributes/operations Employee salary: double Redefine inherited operations + getSalary(): Method overriding double … 2. Treat subclass objects as superclass objects Access inherited data members and methods Information hiding Initialise inherited data members using constructor of superclass Đại học Công nghệ. ĐHQG Hà Nội Inheritance 7
- New attributes/operations Syntax: [public] class Subclass extends Superclass Person { - name: String /* new features go here */ - birthDate: Date } + getName(): String … Example: class Employee extends Person { private double salary; Employee public boolean setSalary(double sal) { - salary: double ... + getSalary(): double salary = sal; … return true; } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 8
- New attributes/operations public class Person { public class Employee extends Person { private String name; private double salary; private Date birthday; public boolean setSalary(double s) { public boolean setName(String n) { salary = s; name = n; return true; return true; } } public String getName() { public double getSalary() { return name; return salary; } } } } //application code calls to Person’s methods from an Employee object ... Employee e = new Employee(); e.setName("John"); System.out.print(e.getName()); e.setSalary(3.0); calls to Employee’s method from an Employee object Đại học Công nghệ. ĐHQG Hà Nội Inheritance 9
- Method overriding A subclass can redefine methods inherited from its superclass. To specialise these methods to suit the new problem Objects of the subclass will work with the new version of the methods Dynamic binding Superclass’s methods of the same name can be reused by using the keyword super Đại học Công nghệ. ĐHQG Hà Nội Inheritance 10
- Method overriding class Animal { String name; public void sayHello() { System.out.println ("Uh oh!"); } Define a new version of the inherited sayHello() } class Cow extends Animal { public void sayHello() { Which version gets to run? System.out.println ("Mooo..."); } It depends on which class the } object belongs to. NOT the class the reference belongs to //application code Animal a1 = new Animal("Bob"); C:\java>java WhoGetsToRun a1.sayHello(); Cow c1 = new Cow("Alice"); Uh oh! c1.sayHello(); Mooo... Animal a2 = c1; a2.sayHello(); Mooo... Đại học Công nghệ. ĐHQG Hà Nội Inheritance 11
- More example Call method of the superclass public class Person { from within the subclass. protected String name; Keyword super is the protected Date birthday; reference to the superclass public void display() { System.out.print (name + "," + birthday); } ... } public class Employee extends Person { ... public void display() { super.display(); System.out.print ("," + salary); } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 12
- Method overriding New and old version must have the same prototype: Same return type Same argument type Private methods cannot be overriden Private members are hidden from subclass Đại học Công nghệ. ĐHQG Hà Nội Inheritance 13
- More example Call method of the superclass public class Person { from within the subclass. protected String name; Keyword super is the protected Date birthday; reference to the superclass public void display() { System.out.print (name + "," + birthday); } ... } public class Employee extends Person { ... public void display() { super.display(); System.out.print ("," + salary); } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 14
- Information hiding Superclass programmer and subclass programmer might not be the same person. Simple reuse independent of specific implementations Employee does not have to care how name and birthday are stored and processed inside Person but can still use them Internal design and implementation of superclass can be modified without requiring changes in subclasses class Person could have three int attributes i instead of one Date attribute for birthday, or two instead of one String for name. Class Employee’s code is not affected. Hiding does not mean preventing source code from being seen by programmers. Đại học Công nghệ. ĐHQG Hà Nội Inheritance 15
- protected access level Protected members of a superclass are directly accessible from inside its subclasses. public class Person { protected String name; Subclass can directly access protected Date birthday; superclass‘s protected members … } public class Employee extends Person { ... public String toString() { String s; s = name + "," + birthday; //no error. s += "," + salary; return s; } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 16
- Access control levels Modifier accessible within same same subclasses universe class package private Yes package (default) Yes Yes protected Yes Yes Yes public Yes Yes Yes Yes Đại học Công nghệ. ĐHQG Hà Nội Inheritance 17
- In the same package package people; Default access level is “package”, which means public class Person { those with “package” access level can be accessed directly from within String name; the same package Date birthday; … package people; } public class Employee extends Person { ... public String toString() { String s; s = name + "," + birthday; //no error. s += "," + salary; return s; } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 18
- In different packages package abc; Members with “package” access level cannot be accessed directly by public class Person { subclasses from outside the package String name; Date birthday; … import abc.Person; } public class Employee extends Person { ... public String toString() { String s; s = name + "," + birthday; //Error. s += "," + salary; return s; } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 19
- In different packages package abc; Members with “protected” access level can be accessed directly by subclasses public class Person { from outside the package protected String name; protected Date birthday; … import abc.Person; } public class Employee extends Person { ... public String toString() { String s; s = name + "," + birthday; //no error. s += "," + salary; return s; } } Đại học Công nghệ. ĐHQG Hà Nội Inheritance 20
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn