Java Abstract class

Any class that contains one or more abstract method must also be declared abstract.

We can declare an abstract class by using the abstract keyword in front of class keyword at the beginning of the class declaration.

An abstract method is a method which declares in super class and defines by the associate subclass.

To declare abstract method we a place abstract method keyword before method declaration.


      abstract type name(parameter list);

  • Abstract class contains associate is a class in which we can declare an abstract method but can’t define.
  • An abstract class must have at least one method of the abstract type.
  • An abstract class can’t have any object.
  • Abstract class can’t be directly instantiated with the new operator.
  • We can’t declare abstract constructor or abstract static method.

Method Overriding in Java

Method overriding is one of the importance concept in Java Programming.

What is Method Overriding?

In Inheritance hierarchy when a child class defines exact same method that is available in parent class then the method is called overridden method.

The process of creating overridden method is called method overriding.


We have created a class Shape.

Also created child classes Triangle, Rectangle and Circle.

On child class we have created same method draw(). Creating exact same method in child is known as method overriding.

In child class method signature and return type is exact same as parent but child class provides new definition to overridden method.

Why method overriding?

When defined method in parent class is not suitable for child class then we override method.

As in above example method draw() is overridden in all child class.

Because draw() behaves differently for different classes.

Note: If we want to access the super class overridden method, we can do so by using super keyword.

Note: Overriding occurs only when the method name and parameter list of the method are identical.  

Method with same name and different parameter list are simply overloaded not overridden.


Java Command line Arguments (CLA)

In java programming, sometimes we want to pass information into a program during run time. We can do so by using command line argument.

All command line argument are passed as string

                                                                CLA are stored as string in string array passed to the args parameter of main(). The first CLA is stored at args[0], the second at args[1] and so on.

Example: Display all command line argument.

Command Line argument


Java Nested and Inner Class

In java programming it is possible to define a class within another class, such classes are known as nested class.

There are two types of nested classes:-                                                                                                                                1. Non-static inner class                                                                                                2.  Static inner class

Non-static inner class: Non-static inner class has a access to all of the variables and member of its outer class and can refer them directly.

Static inner class

static inner class can not access non-static member of its outer class. A static inner class can only access static member of its outer class.


                This program will not compile because static inner class can not access non-static member of its outer class. But if we  will declare “x” as static then 

static int x = 100; 

the above program will run because x is a static member.

Note: In the above program, object of static inner class  created in outer class method but if we want to create object of static inner class in main() method. We can create by using syntax:

OuterClass.InnerClass  obj_name =new    OuterClass.InnerClass();  

JavaAccess Control (Access modifier)

There are there types of access modifiers in Java.

They are: public private and protected.

public:  A public data can be accessed by outside of the code in which it is defined.

private: A private data can not be accessed by outside of the code in which it is defined. The private data can be accessed by only member function 

protected: A protected access control is similar to the private only difference is that it has a access to their derived classes. Protected applies only when inheritance outside.

default : By default access control is public means when we have not specified access control it will be private.

Difference between public and private


If you comment line obj.c = 30;// error, c can’t be accessed here. then following result will produce

Private data can not be directly accessible outside class

Java Argument Passing

In Java, there are two ways of argument passing.

  1. Call by value
  2. Call by reference

Call by value

Whenever we call a method and passes the value of the variable to the called method, it’s called call by value.   


Note: After multiplication and division value of a and b remain unchanged.

Explanation : In the above program, we are passing the value of a and b 15 & 20 respectively and  set the value of i and j. After setting the value of i and j value of variable a and b remain unchanged because modifications occurs in variable i and j.   

Call by reference

Whenever we call a method and passes the reference of the variable to the called method, it’s called call by reference.   

Explanation:  In the above program, in the  lineobj.set(obj1);               “ we are passing the object obj to the method “set (Demo  o)” and reference “o “ of Demo class will also refer to object obj.

Call by reference

In the above figure

Explanation: In the above program, when “Demo obj =  new   Demo(10,20); “ will execute it will pass the value of a and b 10 & 20 respectively for object obj.

And when “obj.set(obj);” will execute it will pass object obj and reference of class Demo “o” refer to Explanation: “obj”. Reference “o” will modify/update the value of object obj.

Method Overloading in Java

In Java programming, Defining two or more methods with same name with different parameter list are called Method Overloading in Java.


void add(int a,int b);
void add(double a, double b);
void add(String a,String b);
int add(int a,int b,int c);

In method overloading method return type may be different.

Above methods are overloaded.

Java inbuilt overloaded methods

There are many overloaded methods available in java.

You already used many of them.

To print on console, we use System.out.println()

method println has many overloaded methods

These are

You can also find overloaded method for println() and print() here

Why method overloading required?

We name methods according to their work.

So there may be situation that same work is performed for different argument list .

To perform same task for different type, order and number of parameters method overloading requires.

as above to add two numbers method add is created.

Methods with the same name are known as an overloaded method and this process is called method overloading.

How to overload method in java?

Following is the syntax to overload method

  • Overloaded access specifier may differ
  • Overloaded methods return type may differ
  • Overloaded methods parameters must be differ by type order and number
  • Overloaded methods can throw different exceptions

Method overloading program in java

Method overloading example in java to add two numbers.

In above class we created three methods with same name to add integers, double and String.


How method overloading Works?

When there is a overloaded method a method call is done based on number and type of arguments.

c.add(4,5) Here both arguments are integer so add(int ,int) is get called.

similar, c.add(4.0,5.0) here both arguments are double so add(double,double) gets called.

Resolving which method is getting called is done at compile time.

Here one name is performing many task.

So Method overloading is example of compile time poly morphism.

Type Casting in Method Overloading

Type casting is conversion of one data type to another.

In java method call type casting is also possible

Type promotion in java method call is as below

In above program we are passing integer arguments in first add. There is no add(int,int) in program.

In this case both arguments are converted to long.

So there is a automatic promotion(widening) is done during method call.

similar for c.add(4.0f, 5.0f).

Lets see again another example

In first method call c.add(4,5) second argument is converted to long.

In second argument c.add(4,5, 6) first argument is converted to long and third argument is converted to double.

There may be chance we have to narrow our data type.

In case of narrowing user has to implicit cast the parameter.

Here to call integer version of max() long arguments are explicitly type casted in to int.

simiar for double values.


Method Overloading in child class

Method overloading can also be used in inheritance.

A method can also be overload in child class

Here add method is overloaded in inheritance hierarchy.

There are few questions related to overloading lets see them.

Can we overload static methods in java?

Yes We can overload static methods in java as below

In above program we have overloaded multiply(...) with static and non static method.


You can find inbuilt static overloaded method in Math Class

overloaded max method in Math class is as below

static double max(double a, double b)
static float max(float a, float b)
static int max(int a, int b)
static long max(long a, long b)

Overloaded min method in Math class is as below

static double min(double a, double b)
static float min(float a, float b)
static int min(int a, int b)
static long min(long a, long b)

Can we overload final methods in java?

Yes we can overload final methods in java

Lets understand by example

Here method

final void show()
final void show(int a )
void show(int a,int b)

are overloaded.


Can we overload private method in java?

In above program

private int min(int a,int b)
private float min(float a,float b )
double min(double a,double b)

are overloaded

Above two private methods can not be accessible out side the class.


Can we overload main method in Java?

Yes we can also overload main method in java.

Although there may be many main overloaded methods, but execution starts from

public static void main(String args[])

following is the example


Is method overloading is polymorphism


Polymorphism is one name many form.

In method overloading a single name is used for overloaded work.

so name is same and form is different.

Overloaded method is resolved by compiler so this is a compile time polymorphism.

Can we overload Java Constructors ?

Yes Java constructors can be overloaded.

this keyword in java

Local Variable Hides Instance Variable

In java when the local variable has the same name as an instance variable, the local variable hides the instance variable.


In above program we have two instance variable with value a=10 and b=10. in method display there is a local variable a with value 5.

Inside method display() local variable a hides the instance variable a and it prints local variable’s value that is 5.

To resolve this problem we use this keyword

this keyword

“this” keyword can be used inside any method to refer to current object.     

In Java, we can’t declare two local variables with the same name within the same scope.

When the local variable has Java same name as an instance variable, the local variable hides the instance variable.                                                                                                                                                          “this” keyword solve the namespace collision that might occur between instance variable and local variable.

above problem can solve as given here


Les see another example where we pass parameter to constructor and assign that value to instance variable


Constructors in Java- Type Overloading and examples

A Constructor in a special method that is used to initialize the object.

Constructor name should be same as class name. Constructors does not have any return type.

While creating object of above class we use

Here new is used to create object and after then the constructor Student() is get called it provide initial value to object

When an object is created using new operator then to initialize the object constructor is called immediately.

A class can contain more than one constructor.  If a class does The constructor the constructor, the system provides the default constructor which is known as system default constructor resides.

The default constructor  automatically initialize all instance variable to zero

1 Characteristics of Constructor

  • The constructor has the same name as the class in which it resides.                        
  • The constructor is automatically called immediately after the object of that class created in which it resides.
  • Constructor neither returns value nor void.    
  • Access specifiers public protected,default and private can be used with constructors.                                                                         
  • The constructor can be overloaded.
  • A constructor can also call another constructor.                                                 
  • Default constructor automatically initialize all instance variable to zero  
  • Constructors can not be abstract, final ,native synchronized or static.

2 Type of Constructors

In Java Constructors are categorized in three types

  • Default (No Argument) Constructor
  • Parameterized Constructor
Type of Constructors in java

Default (No Argument) Constructor

A default constructor in no argument constructor it initialize object value to initial value.

In above program no argument constructor in provided student() on executing above program it will show following result


If a java programmer has written a program and didn’t provided any constructor in program in that case during compile time compiler will include a no argument constructor.

Compiler will only include this constructor it there is no constructor available in program.

If there is already a parameterised constructor and not a no argument constructor then compiler will not include default constructor it will raise compile a time error.

Program to show use of constructor

No constructor provided in class Student. while creating object we are calling Student() (default constructor) of Student class.

The default constructor is provided by the compiler.

3 Constructors with Arguments

In Java, we can create a create a constructor which can take zero, one or more than arguments.


Note: In the above program we have created class Student and three constructors  Student(), Student(long r) and Student(long r, String n ).

When the line Student obj1 = new Student();executes and immediately after the object “obj1” is create, Student() constructor will be called.When The constructor line Student obj2 = new Student (1); executes and immediately after the object “obj2” is create, Student(long r) constructor will be called and passes the value 1 to the constructor.When line “ Student obj3 = new Student(1,"Ram"); executes and immediately after the object “obj3” is create, Student(long r, String n) constructor will be called and passes the value 1 and “Ram” to the constructor.

4 Java Constructor Overloading

Like a method’s, we can create a more than one constructor with same name as the class name.

But each constructor has a different parameter. Such constructor is dfcalled overloaded constructor and this procedure is known as constructor overloading.



Explanation: In the above program inside class “Employee”, we have created five constructors and each constructor have the different parameter list.

Such constructors are called the overloaded constructor and the process of creating multiple constructors with the same name with different parameter list called constructor overloading.

Methods in Java-

Introducing Method

In java programming language class usually consist of two things instance variable and methods.

What is a method in java                                                                 

Method are the block of statement that perform a special task.

Using methods we can avoid the rewriting the same code over and over in a sane program.

Method increase the efficiency and reduce the complexity of the program. 

Syntax of Method Definition

following is the Java method syntax:

Write a program to demonstrate method in class.


Creating a method in java

to create a method we have to give method heading and method body.

As you can already seen here

how to call a method in java

In above program you can see how method area is called in main().

Object.methodName(parameter list);

is used to call methods from another/same class.

A static method can be called with Class name or with object.

ClassName.methodName(parameter list);

Parameterized(overloaded) Method

Write a program to demonstrate parameterized method in class.


Java Program to illustrate Method call with and without agrument and with and without return type


Java Program to pass a object to a method contains main method and updateStudent method


Passing array as argument in java method