tailieunhanh - Object-Oriented Programming: Inheritance

Object-Oriented Programming: Inheritance includes Inheritance, Overriding and hiding methods, Polymorphism, Final methods and classes, Abstract methods and classes; In Java, excepting Object, every class has one and only one direct superclass. | Lecture 5: Inheritance Lê Hồng Phương phuonglh@ Department of Mathematics, Mechanics and Informatics, Vietnam National University, Hanoi Content ● Inheritance ● Overriding and hiding methods ● Polymorphism ● Final methods and classes ● Abstract methods and classes 2012-2013 Object-Oriented Programming: Inheritance 2 Inheritance ● ● ● A class that is derived from another class is called a subclass (derived class, extended class, child class). The superclass is also called base class or parent class. In Java, excepting Object, every class has one and only one direct superclass. 2012-2013 Object-Oriented Programming: Inheritance 3 Inheritance ● ● ● A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses. The constructor of the superclass can be invoked from the subclass. 2012-2013 Object-Oriented Programming: Inheritance 4 Inheritance public class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; } The fields represent the state of the object. void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { The methods define its gear = newValue; interaction with the outside world. } void speedUp(int increment) { speed = speed + increment; } void applyBrakes(int decrement) { speed = speed - decrement; } void printStates() { ("(" + cadence + ", " + speed + ", " + gear + “)”); } 2012-2013 Object-Oriented Programming: .

TÀI LIỆU MỚI ĐĂNG
10    179    3    28-12-2024
11    168    2    28-12-2024