Oops 2
Oops 2
A method is a block of statements under a name that gets executes only when it is
called. Every method is used to perform a specific task. The major advantage of
methods is code re-usability (define the code once, and use it many times).
Calling a method
In java, a method call precedes with the object name of the class to which it belongs
and a dot operator. It may call directly if the method defined with the static modifier.
Every method call must be made, as to the method name with parentheses (), and it
must terminate with a semicolon.
2)Constructor:
A constructor is a special method of a class that has the same name as the class name.
The constructor gets executes automatically on object creation. It does not require the
explicit method call. A constructor may have parameters and access specifiers too. In
java, if you do not provide any constructor the compiler automatically creates a default
constructor. A constructor can not have return value.
In java, the default constructor of a parent class called automatically by the constructor
of its child class. That means when we create an object of the child class, the parent
class constructor executed, followed by the child class constructor executed.
However, if the parent class contains both default and parameterized constructor, then
only the default constructor called automatically by the child class constructor.
In Java, the access specifiers (also known as access modifiers) used to restrict the
scope or accessibility of a class, constructor, variable, method or data member of class
and interface. There are four access specifiers, and their list is below.
• default (or) no modifier
• public
• protected
• private
When a method defined with the final keyword, it does not allow it to override. The final
method extends to the child class, but the child class can not override or re-define it. It
must be used as it has implemented in the parent class.
final with class
When a class defined with final keyword, it can not be extended by any other class.