Beruflich Dokumente
Kultur Dokumente
To inherit a class we use extends keyword. Here class XYZ is child class and class
ABC is parent class. The class XYZ is inheriting the properties and methods of ABC
class.
class XYZ extends ABC
{
}
The derived class inherits all the members and methods that are declared as public
or protected. If the members or methods of super class are declared as private then
the derived class cannot use them directly. The private members can be accessed
only in its own class. The child class is able to access the private members of
parent class through protected methods of parent class. When we make a instance
variable(data member) or method protected, this means that they are accessible only
in the class itself and in child class.
Consider this method that we have already been using from Breezy;
int number = Console.readInt("Enter a number"); //returns a value
The method name is "readInt" which is defined in the class "Console". Since the
method is defined in the class Console, the word Console becomes the calling
object. This particular method returns an integer value which is assigned to an
integer variable named number.
You invoke (call) a method by writing down the calling object followed by a dot,
then the name of the method, and finally a set of parentheses that may (or may not)
have information for the method.
In Java, every method must be part of some class which is different from languages
like C, C++, and Python.The method needs to be called for using its functionality.