0% found this document useful (0 votes)
22 views6 pages

Oops 2

The document discusses various Java concepts: 1. Methods allow code reusability and must be declared within a class with a return type, name, parameters, and block. They are called using object.method(). 2. Constructors are special methods that are called automatically to initialize an object and have the same name as the class but no return type. 3. Access modifiers like public, private, protected, and default restrict method/variable accessibility within and between classes.

Uploaded by

Anindya Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Oops 2

The document discusses various Java concepts: 1. Methods allow code reusability and must be declared within a class with a return type, name, parameters, and block. They are called using object.method(). 2. Constructors are special methods that are called automatically to initialize an object and have the same name as the class but no return type. 3. Access modifiers like public, private, protected, and default restrict method/variable accessibility within and between classes.

Uploaded by

Anindya Dey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1)Java Methods:

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).

Every method in java must be declared inside a class.

Every method declaration has the following characteristics.

• returnType - Specifies the data type of a return value.

• name - Specifies a unique name to identify it.

• parameters - The data values it may accept or recieve.

• { } - Defienes the block belongs to the method.

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.

The parameterized constructor of parent class must be called explicitly using


the super keyword.

3)Java Access Modifiers:

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

🔔 The public members can be accessed everywhere.


🔔 The private members can be accessed only inside the same class.
🔔 The protected members are accessible to every child class (same package or
other packages).
🔔 The default members are accessible within the same package but not outside
the package.
4)Java super keyword:
In java, super is a keyword used to refers to the parent class object. When both parent
class and child class have members with the same name, then the super keyword is
used to refer to the parent class version.
🔔 The super keyword is used inside the child class only.
To call the parameterized constructor of the parent class, the super keyword must be
the first statement inside the child class constructor, and we must pass the parameter
values.
In java, the super keyword is used for the following purposes:
super to refer parent class data members
When both parent class and child class have data members with the same name, then
the super keyword is used to refer to the parent class data member from child class.
super to refer parent class method
When both parent class and child class have method with the same name, then the
super keyword is used to refer to the parent class method from child class.
super to call parent class constructor
When an object of child class is created, it automatically calls the parent class default-
constructor before it's own. But, the parameterized constructor of parent class must be
called explicitly using the super keyword inside the child class constructor.
5) Java final keyword:
In java, the final is a keyword and it is used with the following things.

final with variables


When a variable defined with the final keyword, it becomes a constant, and it does not
allow us to modify the value. The variable defined with the final keyword allows only a
one-time assignment, once a value assigned to it, never allows us to change it again.

final with methods

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.

You might also like