tailieunhanh - Lecture 3: Extending ClassesInheritance in Java
Tham khảo bài thuyết trình 'lecture 3: extending classes inheritance in java', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Lecture 3: Extending Classes Inheritance in Java 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 . | Lecture 3: Extending Classes Inheritance in Java 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
đang nạp các trang xem trước