tailieunhanh - Lecture 4: Extending Classes

Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class’s methods and fields, and you can also add new methods and fields to adapt the new classes to new situations Subclass and superclass Subclass and superclass have a IsA relationship: an object of a subclass IsA(n) object of its superclass | Lecture 4: Extending Classes Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class’s methods and fields, and you can also add new methods and fields to adapt the new classes to new situations Subclass and superclass Subclass and superclass have a IsA relationship: an object of a subclass IsA(n) object of its superclass Superclass public class Person{ private String name; public Person ( ) { name = “no_name_yet”; } public Person ( String initialName ) { = initialName; } public String getName ( ) { return name; } public void setName ( String newName ) { name = newName; } sample classes Subclass public class Student extends Person { private int studentNumber; public Student ( ) { super( ); studentNumber = 0; } public Student (String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; } public int getStudentNumber ( ) { return studentNumber; } public void setStudentNumber (int newStudentNumber ) { studentNumber = newStudentNumber; } Classes hierarchy Every class is an extended (inherited) class, whether or not it’s declared to be. If a class does not declared to explicitly extend any other class, then it implicitly extends the Object class Class hierarchy of previous example Object Person Student Fields/Methods in Extended Classes An object of an extended class contains two sets of variables and methods fields/methods which are defined locally in the extended class fields/methods which are inherited from the superclass What are the fields for a Student object in the previous example ? Constructors in extended classes A constructor of the extended class can invoke one of the superclass’s constructors by using the super method. If no superclass constructor is invoked explicitly, then the superclass’s no-arg constructor super( ) is invoked automatically as the first statement of the extended class’s . | Lecture 4: Extending Classes Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class’s methods and fields, and you can also add new methods and fields to adapt the new classes to new situations Subclass and superclass Subclass and superclass have a IsA relationship: an object of a subclass IsA(n) object of its superclass Superclass public class Person{ private String name; public Person ( ) { name = “no_name_yet”; } public Person ( String initialName ) { = initialName; } public String getName ( ) { return name; } public void setName ( String newName ) { name = newName; } sample classes Subclass public class Student extends Person { private int studentNumber; public Student ( ) { super( ); studentNumber = 0; } public Student (String initialName, int initialStudentNumber) { super(initialName); studentNumber = initialStudentNumber; } public int getStudentNumber ( ) { return .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.