2 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Now that we know how to make subclasses and their constructors, it is time to write their methods. A subclass will inherit all public methods from the superclass. These methods will remain public in the subclass. Most methods that are inherited from the superclass do not need to be rewritten.
Any method that we call on an object must be defined within the class associated with that object or within the superclass.
However, some methods will have to be rewritten because their implementations will be different. This can be done by overloading, where the parameters for the methods are different but the methods achieve the same thing.
The other way of doing this is overriding, which is when the method will have the same parameters, but their implementations will be different. Above the method header, we have to write @Override to show that this is an overridden method. For an overridden method, we usually don't have to write Javadoc comments since those should be inherited from the superclass.
A subclass can override methods inherited from its superclass. A subclass can also introduce new instance variables and methods that are not defined in the superclass.
In the Rectangle class, we will override the area method as follows:
/** Represents a rectangle
*/
public class Rectangle extends Quadrilateral {
/** Makes a rectangle given a length and width
*/
public Rectangle(double length, double width) {
super(length, width, length, width);
}
@Override
public double Area() {
//from the constructor, length is sideOne and width is sideTwo
return sideOne * sideTwo;
}
}
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 7
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 7
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Term 1 of 7
A subclass is a class that inherits properties and behaviors from another class, called the superclass. It can add new features or modify existing ones.
Superclass: The superclass is the class from which the subclass inherits properties and behaviors.
Inherit: Inheritance is the process by which one class acquires properties and behaviors from another class.
Polymorphism: Polymorphism allows objects of different classes to be treated as objects of the same superclass, enabling flexibility in programming.
Public methods are functions or procedures in a class that can be accessed and called from anywhere in the program. They are accessible to all other classes and objects.
Private Methods: These are methods that can only be accessed within the same class. It's like an employee-only area in a store where customers cannot enter.
Static Methods: Static methods belong to the class itself rather than an instance of the class. They can be called without creating an object of that class, similar to using a vending machine without needing to own it.
Method Overloading: This is when multiple methods have the same name but different parameters. It's like having different flavors of ice cream with different toppings but all served in cones.
Overloading refers to defining multiple methods with the same name but different parameters within a single class. The methods must have different parameter lists or different types of parameters.
Method Signature: Similar to overriding, method signature is important in overloading as well. It includes the method name and parameter list, which must be unique for each overloaded method.
Compile-time Polymorphism: Overloading is an example of compile-time polymorphism because the decision on which overloaded method to call is made by the compiler at compile time.
Default Arguments: In some programming languages, default arguments allow you to specify default values for parameters in case they are not provided when calling an overloaded method.
Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The overridden method in the subclass replaces the implementation inherited from the superclass.
Method Signature: The method signature consists of the method name and parameter list. When overriding a method, it must have the same signature as the original method in order to replace it.
Dynamic Binding: Dynamic binding refers to determining at runtime which implementation of an overridden method will be called based on the actual type of object being referred to.
Virtual Function: In languages like C++, virtual functions allow dynamic binding at runtime by using pointers or references to base-class objects.
The @Override annotation is used in Java to indicate that a method in a subclass is intended to override a method with the same name in its superclass. It helps ensure that the method signature and return type are correct.
Inheritance: Inheritance is when one class inherits properties and behaviors from another class. For example, a Dog class can inherit traits from an Animal class.
Polymorphism: Polymorphism allows objects of different classes to be treated as objects of the same parent class. It enables flexibility and reusability in code.
Method Overloading: Method overloading occurs when multiple methods have the same name but different parameters. This allows you to perform similar operations with different inputs.
Javadoc comments are special comments in Java that begin with /** and end with */. They are used to generate documentation for classes, methods, and fields, making it easier for other developers (including yourself) to understand how to use them.
API Documentation: API documentation provides information about how to use specific software libraries or frameworks. It includes details about classes, methods, parameters, and return values.
Code Documentation: Code documentation refers to any form of written explanation or commentary within source code files that helps developers understand the purpose and functionality of the code.
Commenting Style Guide: A commenting style guide is a set of guidelines or rules that dictate how comments should be written in a particular programming language or project. It ensures consistency and readability across the codebase.
Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.