Compare Method Overloading and Method Overriding
[Link] Method overloading Method overriding
1 Method overloading is one way of Method overriding is used to provide the specific
implementing ‘One interface, multiple implementation of the method that is already
methods’ paradigm. It allows related methods provided by its super class.
to be accessed by using a common name.
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 In case of method overriding, parameter must be
be different. same.
4 Method resolution is taken care by java Method resolution is taken care by JVM based on
compiler based on reference type. runtime object.
5 Method overloading is the example of compile Method overriding is the example of run time
time polymorphism or early binding. polymorphism, dynamic polymorphism, or late
binding.
6 In java, method overloading can't be Return type must be same or covariant in method
performed by changing return type of the overriding.
method only. Return type can be same or
different in method overloading. But you must
have to change the parameter.
7 Constructors can be overloaded Constructors cannot be overridden
8 Private/Static/Final methods can be overloaded Private/Static/Final methods cannot be overridden
9 Access modifiers can be anything or different. Subclass method’s access modifier must be same or
higher than superclass method access modifier.
10 Any exception can be thrown If child class method throws any checked exception
compulsory parent class method should throw the
same exception is its parent otherwise we will get
compile-time error but there is no restriction for an
unchecked exception.
Distinguish between inheritance and polymorphism.
[Link] Inheritance Polymorphism
1 Inheritance is creating a new class using the Polymorphism is basically a common interface for
properties of the already existing class. multiple form.
2 Inheritance is basically implemented on Polymorphism is basically implemented on
classes. function/methods.
3 It supports the concept of reusability in OOP It allows object to decide which form of the
and reduces the length of code. function to be invoked when, at compile
time(overloading) as well as run time(overriding).
4 Inheritance may be a single inheritance, Polymorphism may be a compile time
multilevel inheritance, hierarchical polymorphism (overloading) or run-time
inheritance and hybrid inheritance. polymorphism (overriding).
5 Example : Example :
The class bike can be inherit from the class of The class bike can have method name set_color(),
two-wheel vehicles, which is turn could be a which changes the bike’s color based on the name
subclass of vehicles. of color we have entered.
Similarities and Differences between classes and interfaces
Similarities
1. Both are used to create new reference types
2. An interface is syntactically similar to a class
3. Both can use extends keyword
Differences
[Link] Class Interfaces
1 Class will be used to describe the behaviour of Interface will carry those behaviours of the objects.
the objects in the program
2 Classes does not support multiple inheritance. Interface supports multiple inheritance
3 The keyword used to create a class is “class” The keyword used to create an interface is
“interface”
4 A class can be instantiated i.e, objects of a An Interface cannot be instantiated i.e, objects
class can be created. cannot be created.
5 It can inherit another class. It cannot inherit a class.
6 It can be inherited by another class using the It can be inherited by a class by using the keyword
keyword ‘extends’. ‘implements’ and it can be inherited by an interface
using the keyword ‘extends’.
7 It can contain constructors. It cannot contain constructors.
8 It cannot contain abstract methods. It contains abstract methods only.
9 Variables and methods in a class can be
All variables and methods in an interface are
declared using any access specifier (public,
declared as public.
private, default, protected)
10 Variables in a class can be static, final or
All variables are static and final.
neither.
Similarities and Differences between abstract classes and interfaces
Similarities
Both Interfaces and Abstract Classes can have methods and variables but neither of them can be
instantiated. All the variables declared within the interface are final. The variables declared in Abstract
Classes can be non-final and can be modified by the user-defined classes.
Differences
[Link] Abstract Classes Interfaces
1 Abstract class can have abstract and non- Interface can have only abstract methods. Since Java
abstract methods. 8, it can have default and static methods also
2 Abstract class doesn't support multiple Interface supports multiple inheritance
inheritance.
3 Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
4 Abstract class can provide the implementation Interface can't provide the implementation of
of interface abstract class.
5 The abstract keyword is used to declare
The interface keyword is used to declare interface.
abstract class.
6 An abstract class can extend another Java class
An interface can extend another Java interface only.
and implement multiple Java interfaces.
7 An abstract class can be extended using An interface can be implemented using keyword
keyword "extends". "implements".
8 A Java abstract class can have class members
Members of a Java interface are public by default.
like private, protected, etc.
9 Example: Example:
public abstract class Shape public interface Drawable
{ {
public abstract void draw(); void draw();
} }
Difference between Multiprocessing and Multithreading:
[Link] Multiprocessing Multithreading
In Multiprocessing, CPUs are added for While In Multithreading, many threads are created
1.
increasing computing power. of a single process for increasing computing power.
In Multiprocessing, Many processes are While in multithreading, many threads of a process
2.
executed simultaneously. are executed simultaneously.
Multiprocessing are classified While Multithreading is not classified in any
3.
into Symmetric and Asymmetric. categories.
In Multiprocessing, Process creation is a While in Multithreading, process creation is
4.
time-consuming process. according to economical.
In Multiprocessing, every process owned a While in Multithreading, a common address space
5.
separate address space. is shared by all the threads.
Difference between Checked Exceptions and Unchecked Exceptions
[Link]
Checked Exceptions Unchecked Exceptions
1 They occur at compile time. They occur at runtime.
The compiler checks for a checked The compiler doesn’t check for these kinds of
2
exception. exceptions.
They can’t be caught or handled during compilation
These exceptions can be handled at the
3 time. This is because the exceptions are generated due
compilation time.
to the mistakes in the program.
These are not a part of the ‘Exception’ class since they
4 It is a sub-class of the exception class.
are runtime exceptions.
The JVM doesn’t require the exception to be caught
The JVM requires that the exception be
5 and handled.
caught and handled.
[Link]
Checked Exceptions Unchecked Exceptions
Example of Checked exception- ‘File Example of Unchecked Exceptions- ‘No Such Element
6
Not Found Exception’ Exception’
Differences between Exception and Error
Exception Error
Exception can be recovered by using the try-catch block. An error cannot be recovered.
It can be classified into two categories i.e. checked and All errors in Java are unchecked.
unchecked.
It occurs at compile time or run time. It occurs at run time.
It belongs to [Link] package. It belongs to [Link] package.
Only checked exceptions are known to the compiler. Errors will not be known to the
compiler.
It is mainly caused by the application itself. It is mostly caused by the environment
in which the application is running.
Checked Exceptions: SQLException, IOException [Link],
UncheckedExceptions: ArrayIndexOutOfBoundException, [Link]
NullPointerException, ArithmaticException