Docsity

Java Inheritance: Hierarchy, Reusing Classes, Overriding Methods, Abstract, Lecture notes of Information Technology

Partial preview of the text

Download Java Inheritance: Hierarchy, Reusing Classes, Overriding Methods, Abstract and more Lecture notes Information Technology in PDF only on Docsity!

LESSON 5: INHERITANCE

By the end of this lesson you will be able to Mastering the inheritance hierarchy Reuse existing classes Know how to override methods Mastering abstract classes and methods

INHERITANCE HIERARCHY class Bicycle{…} class MountainBike extends Bicycle{…} class RoadBike extends Bicycle{…} class TandemBike extends Bicycle{…}

DEMO

Hình Hình đa giác Hình chữ nhật Hình tròn Hình vuông Build classes according to structure inheritance hierarchy as diagram Tam giác

INHERIT package poly.ho; public class NhanVien{ public String hoTen; protected double luong; public NhanVien(String hoTen, double luong){…} void xuat(){…} private double thueThuNhap(){…} } package poly.hcm; public class TruongPhong extends NhanVien{ public double trachNhiem; public TruongPhong (String hoTen, double luong, double trachNhiem){…} public void xuat(){ // Mã ở đây có thể sử dụng những tài sản nào của lớp cha } } A. super.hoTen B. super.luong C. super.xuat() D. super.thueThuNhap()

DEMO

Realize the example of the previous slide

USE SUPER Access to super class members using super. keyword Super can be used to call the constructor of the parent class public class Parent{ public String name; public void method(){} } public class Child extends Parent{ public String name; public void method(){ this.name = super .name; super .method() } }

USE SUPER package poly.ho; public class NhanVien{ public NhanVien(String hoTen, double luong){…} public void xuat(){…} } package poly.hcm; public class TruongPhong extends NhanVien{ public double trachNhiem; public TruongPhong (String hoTen, double luong, double trachNhiem){ super (hoTen, luong); this.trachNhiem = trachNhiem } public void xuat(){ super .xuat() System.out.println(trachNhiem) } }

LESSON 5: INHERITANCE

OVERRIDING Overriding is the case where the subclass and the superclass have the same method and syntax. Parent and Child class both have method() method with same syntax, so method() in Child will override method() in Parent public class Parent{ public void method (){…} } public class Child{ public void method (){…} } Parent o = new Child(); o.method() Mặc dù o có kiểu là Parent nhưng o.method() thì method() của lớp Child sẽ chạy do bị đè

DEMO

Salary of NhanVien, Head of Department, Labor… is calculated according to the formula difference. For example, employee is salary month, labor is hourly wage, chief The room also has a responsible salary NhanVien

  • hoTen
  • luong
  • getThuNhap() TruongPhong
  • trachNhiem
  • getThuNhap() LaoCong
  • soGioLamViec
  • getThuNhap()

ABSTRACT CLASS Hình Chữ nhật Tròn Tam giác Vuông Sinh viên SV Biz SV IT Hình và Sinh viên là các lớp trừu tượng Chữ nhật, Tròn, Tam giác, Vuông, SV IT, SV Biz là các lớp cụ thể

ABSTRACT CLASS DEFINITION abstract public class SinhVien{ abstract public double getDiemTB (); } abstract public class Hinh{ abstract public double getChuVi (); abstract public double getDienTich (); } abstract public class MyClass{ abstract public type MyMethod(); } Use abstract keyword to define abstract classes and methods