Lecture Introduction to Java programming - Chapter 11: Inheritance and polymorphism

Object-oriented programming allows you to derive new classes from existing classes. This is called inheritance. Inheritance is an important and powerful feature in Java for reusing software. Suppose you are to define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy and make the system easy to comprehend and easy to maintain? The answer is to use inheritance. | Chapter 11 Inheritance and Polymorphism Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The answer is to use inheritance. Objectives To develop a subclass from a superclass through inheritance (§). To invoke the superclass’s constructors and methods using the super keyword (§). To override instance methods in the subclass (§). To distinguish differences between overriding and overloading (§). To explore the toString() method in the Object class (§). To discover polymorphism and dynamic binding (§§). To describe casting and explain why explicit downcasting is necessary (§). To explore the equals() method in the Object class (§). To store, retrieve, and manipulate objects in an ArrayList (§). To implement a Stack class using ArrayList (§). To restrict access to data and methods to subclasses using the protected visibility modifier (§). To prevent class extending and method overriding using the final modifier (§). Superclasses and Subclasses GeometricObject1 Circle4 Rectangle1 TestCircleRectangle Run Are superclass’s Constructor Inherited? No. They are not inherited. They are invoked explicitly or implicitly. Explicitly using the super keyword. A constructor is used to construct an instance of a class. Unlike properties and methods, a superclass's constructors are not inherited in the subclass. They can only be invoked from the subclasses' constructors, using the keyword super. If the keyword super is not explicitly used, the superclass's no-arg constructor is automatically invoked. Superclass’s Constructor Is Always Invoked A constructor may invoke an overloaded constructor or its superclass’s constructor. If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor. For example, Using the . | Chapter 11 Inheritance and Polymorphism Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design these classes so to avoid redundancy? The answer is to use inheritance. Objectives To develop a subclass from a superclass through inheritance (§). To invoke the superclass’s constructors and methods using the super keyword (§). To override instance methods in the subclass (§). To distinguish differences between overriding and overloading (§). To explore the toString() method in the Object class (§). To discover polymorphism and dynamic binding (§§). To describe casting and explain why explicit downcasting is necessary (§). To explore the equals() method in the Object class (§). To store, retrieve, and manipulate objects in an ArrayList (§). To implement a Stack class using ArrayList (§). To restrict access to data and methods to .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã 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.