Difference between

Method overloading and method overriding in java

No.Method OverloadingMethod Overriding
1)Method overloading is used to increase the readability of the program.Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
2)Method overloading is performed within class.Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3)In case of method overloading, parameter must be different.In case of method overriding, parameter must be same.
4)Method overloading is the example of compile time polymorphism.Method overriding is the example of run time polymorphism.
5)In java, method overloading can’t be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.Return type must be same or
covariant in method overriding.


this and this()

thisthis()
this keyword is used with the objects only.this() is used with constructors only.
It refers to the current object.It refers to the constructor of the same class whose parameters matches with the parameters passed to this(parameters).
Dot(.) operator is used to access the members. 
For example, this.variableName;
No Dot(.) operator is used. Only the matching parameters are passed.
It is used to differentiate between the local variable and the instance variable in the method.It is used to refer to the constructor belonging to the same class.

object and class

No.ObjectClass
1)Object is an instance of a class.Class is a blueprint or template from which objects are created.
2)Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse, chair etc.Class is a group of similar objects.
3)Object is a physical entity.Class is a logical entity.
4)Object is created through new keyword mainly e.g.
Student s1=new Student();
Class is declared using class keyword e.g.
class Student{}
5)Object is created many times as per requirement.Class is declared once.
6)Object allocates memory when it is created.Class doesn’t allocated memory when it is created.
7)There are many ways to create object in java such as new keyword, newInstance() method, clone() method, factory method and deserialization.There is only one way to define class in java using class keyword.

 Constructors and Methods

ConstructorsMethods
A Constructor is a block of code that initializes a newly created object.A Method is a collection of statements which returns a value upon its execution.
A Constructor can be used to initialize an object.A Method consists of Java code to be executed.
A Constructor is invoked implicitly by the system.A Method is invoked by the programmer.
A Constructor is invoked when a object is created using the keyword new.A Method is invoked through method calls.
A Constructor doesn’t have a return type.A Method must have a return type.
A Constructor initializes a object that doesn’t exist.A Method does operations on an already created object.
A Constructor’s name must be same as the name of the class.A Method’s name can be anything.
A class can have many Constructors but must not have the same parameters.A class can have many methods but must not have the same parameters.
A Constructor cannot be inherited by subclasses.A Method can be inherited by subclasses.

Public vs Private Access Modifiers in Java:

Public Access ModifierPrivate Access Modifier
This modifier is applicable for both top-level classes and interfaces.This modifier is not applicable for both top-level classes and interfaces.
Public members can be accessed from the child class of the same package.Private members cannot be accessed from the child class of the same package.
Public member can be accessed from non-child class of same package.Private members cannot be accessed from non-child class of same package.
Public members can be accessed from child class of outside package.Private members cannot be accessed from child class of outside package.
Public members can be accessed from non-child class of outside package.Private members cannot be accessed from non-child class of outside package.
Public modifier is the most accessible modifier.Private modifier is the most restricted modifier.
Public modifier is the recommended modifier for method.
Private modifier is the recommended modifier for
data members.

Important:Access modifier

Static block and Instance block:

Static blockInstance block
Static block is also known as a static initialization blockinstance block is also known as instance initialization block or non-static block.
They execute before the instance blockinstance block executes after the static blocks.
Only static variables can be accessed inside the static blockboth static and non-static variables can be accessed inside the instance block.
Static blocks execute when the class is loaded into the memory instance blocks execute only when instance of the class is created.
 ‘this’ keyword cannot be used in the static block[TBD]this keyword can be used in the instance block.[TBD]

Interface vs Class:

InterfaceClass
Keyword used: interfaceKeyword used: class
Interfaces do not have a constructorClass includes a constructor
Interface stores only the signature of a methodClass stores complete method definition
Interfaces do not need Access SpecifiersIn Class, Access Specifiers are mandatory
Interfaces do not include Data MembersClass includes Data Members
Interfaces do not have Static MembersClass includes Static Members

Java Lambda Expression vs Regular Method in Java:

Lambda Expression Method
Lambda Expressions do not require namingMethods require the method name to be declared
Syntax: ([comma separated argument-list]) -> {body}Syntax: <classname> :: <methodname>
Lambda Expression may not include parametersMethods may not include parameters as well
Lambda Expression does not require a return typeThe return type for methods is mandatory
The Lambda Expression is itself the complete code segment The method body is just another code segment of the program

Leave a comment

Design a site like this with WordPress.com
Get started