





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive lesson on Java inheritance, covering topics such as mastering the inheritance hierarchy, reusing existing classes, knowing how to override methods, and understanding abstract classes and methods. It includes examples and demos to help students grasp these concepts.
Typology: Lecture notes
1 / 29
This page cannot be seen from the preview
Don't miss anything!






















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{…}
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()
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) } }
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ị đè
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
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