You are on page 1of 6

Why Java is OOP?

Java is called Object Oriented Programming Language Because Java is a kind of


programming language that uses Object in each of its programs.
In each java program you have to create classes and in the main function of java
you have to create objects of the classes.
You can write a c++ program without creating a class but you have to create class
and objects in java program that why java is called "purely" object oriented
programming language.
All concepts like inheritance, modularity, polymorphism, and encapsulation in coop
are supported by java

Method Overloading in Java with examples


Method Overloading is a feature that allows a class to have more than one method
having the same name, if their argument lists are different. It is similar
to constructor overloading in Java that allows a class to have more than one
constructor having different argument lists.

Let’s get back to the point, when I say argument list it means the parameters that
a method has: For example the argument list of a method add(int a, int b) having
two parameters is different from the argument list of the method add(int a, int b, int
c) having three parameters.
Three ways to overload a method
In order to overload a method, the argument lists of the methods must differ in
either of these:

1. Number of parameters.
For example: This is a valid case of overloading

add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:

add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:

add(int, float)
add(float, int)

Method overriding in java with example


Declaring a method in sub class which is already present in parent class is
known as method overriding. Overriding is done so that a child class can give its
own implementation to a method which is already provided by the parent class.
In this case the method in parent class is called overridden method and the
method in child class is called overriding method. In this guide, we will see what
is method overriding in Java and why we use it.

Method Overriding Example


Lets take a simple example to understand this. We have two classes: A child
class Boy and a parent class Human. The Boy class extends Human class. Both
the classes have a common method void eat(). Boy class is giving its own
implementation to the eat() method or in other words it is overriding
the eat() method.

The purpose of Method Overriding is clear here. Child class wants to give its own
implementation so that when it calls this method, it prints Boy is eating instead of
Human is eating.
class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
Output:

Boy is eating
Advantage of method overriding
The main advantage of method overriding is that the class can give its own
specific implementation to an inherited method without even modifying the
parent class code.

This is helpful when a class has several child classes, so if a child class needs to
use the parent class method, it can use it and the other classes that want to have
different implementation can use overriding feature to make changes without
touching the parent class code
A. Introducing Class Concept in Java Programming
A class, in the context of Java, are templates that are used to create objects, and to
define object data types and methods. Core properties include the data types and
methods that may be used by the object. All class objects should have the basic class
properties.

B. Syntax : Class Concept

class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;

type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
C. Explanation of Syntax:
Class name

class classname {

1. class is Keyword in Java used to create class in java.

2. classname is Name of the User defined Class.

Class Instance Variable

type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;

1. Instance Variables are Class Variables of the class.

2. When a number of objects are created for the same class, the same copy

of instance variable is provided to all.

3. Instance variables have different value for different objects.

4. Access Specifiers can be applied to instance variable i.e public,private.

5. Instance Variable are also called as “Fields“

Class Methods

Type methodname1 (parameter-list) {


// body of method
}

8. Above syntax is of Class Methods.


9. These methods are equivalent to function in C Programming Language.

10. Class methods can be declared public or private.

11. These methods are meant for operating on class data i.e Class Instance Variables.

12. Methods have return type as well as parameter list. We are going to learn this

concept deeply in next few tutorials.

D. Live Example : Class concept

public class Rectangle {

// two fields
public int breadth;
public int length;

// two methods
public void setLength(int newValue) {
length = newValue;
}

public void setBreadth(int newValue) {


breadth = newValue;
}

You might also like