Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
90 views

Java Differences

The document compares key differences between abstract classes and interfaces in Java. It notes that abstract classes can contain both abstract and non-abstract methods while interfaces can only contain abstract methods. Abstract classes support single inheritance while interfaces support multiple inheritance. Abstract classes can have variables of any type while interface variables are static and final by default.

Uploaded by

Kalyani Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Java Differences

The document compares key differences between abstract classes and interfaces in Java. It notes that abstract classes can contain both abstract and non-abstract methods while interfaces can only contain abstract methods. Abstract classes support single inheritance while interfaces support multiple inheritance. Abstract classes can have variables of any type while interface variables are static and final by default.

Uploaded by

Kalyani Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Difference between Abstract Class and Interface

Abstract class Interface


Abstract class can have abstract and non- Interface can have only abstract methods. Since
abstract methods. Java 8, it can have default and static methods
also.
Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
Abstract class can have final, non-final, static and Interface has only static and final variables.
non-static variables.
Abstract class can provide the implementation of Interface can't provide the implementation of
interface. abstract class.
The abstract keyword is used to declare abstract The interface keyword is used to declare
class. interface.
An abstract class can extend another class and An interface can extend another interface only.
can implement multiple interfaces.
7) An abstract class can be extended using An interface class can be implemented using
keyword "extends". keyword "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 Array List and Linked List

ArrayList LinkedList
ArrayList internally uses a dynamic array LinkedList internally uses a doubly linked list to
(growable/resizable) to store the elements. store the elements.
Data is stored in the form of array. Data is stored in the form of node.
Initial Capacity and Incremental Capacity is Initial Capacity and Incremental Capacity is not
applicable. applicable.
Involves shift operations. That is manipulation Does not involves any shift operations. That is
with ArrayList is slow because it internally uses manipulation with LinkedList is faster than
an array and If any element is removed from the ArrayList because it uses a doubly linked list, so
array, all the bits are shifted in memory. no bit shifting is required in memory.
Has three overloaded constructors. Has two overloaded constructors.
Memory is continuous. Memory may not be continuous.
An ArrayList class can act as a list only because it LinkedList class can act as a list and queue both
implements List only. because it implements List and Deque interfaces.
ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.
(Insertion or removal of data in between)
Consumes less memory. Consumes more memory.
Difference between Array List and Vector

ArrayList Vector
ArrayList is not synchronized. That is ArrayList is Vector is synchronized. That is Vector is single
multi-threaded. threaded.
If the total number of elements exceeds than its Vector increments 100% which means it doubles
capacity, the incremental capacity of the the array size if the total number of elements
ArrayList is exceeds than its capacity. That is Current
((Current Capacity * 3)/2) + 1 Capacity * 2
ArrayList is not a legacy class. It is introduced in Vector is a legacy class. It is present since JDK 1.0.
JDK 1.2.
ArrayList is fast because it is non-synchronized. Vector is slow because it is synchronized, i.e., in a
multithreading environment, it holds the other
threads in runnable or non-runnable state until
current thread releases the lock of the object.
ArrayList uses the Iterator interface to traverse A Vector can use the Iterator interface or
the elements. Enumeration interface to traverse the elements.
Has three overloaded constructors. Has four overloaded constructors.
Cannot control the growth. That is the Can control the growth. That is the incremental
incremental capacity is fixed. capacity can be explicitly mentioned by the
programmer. It is done using one of the
overloaded constructors.
public vector (int initialCapacity, int
incrementalCapacity)

Difference between Comparable and Comparator

Comparable Comparator
Comparable affects the original class, i.e., the Comparator doesn't affect the original class, i.e.,
actual class is modified. the actual class is not modified.
Comparable provides compareTo() method to Comparator provides compare() method to sort
sort elements. elements.
Comparable is present in java.lang package. A Comparator is present in the java.util package.
We can sort the list elements of Comparable We can sort the list elements of Comparator type
type by Collections.sort(List) method. by Collections.sort(List, Comparator) method.

Difference between final, finally and finalize

final finally finalize


final is used to apply restrictions finally is used to place finalize is used to perform clean up
on class, method and variable. important code, it will be processing just before object is
Final class can't be inherited, executed whether exception is garbage collected.
final method can't be handled or not.
overridden and final variable
value can't be changed.
final is a keyword. finally is a block. finalize is a method.
Difference between HashMap and Hashtable

HashMap Hashtable
HashMap is non-synchronized. It is not-thread safe Hashtable is synchronized. It is thread-safe and
and can't be shared between many threads can be shared with many threads.
without proper synchronization code.
HashMap allows one null key and multiple null Hashtable doesn't allow any null key or value.
values.
HashMap is a new class introduced in JDK 1.2. Hashtable is a legacy class.
HashMap is fast. Hashtable is slow.
We can make the HashMap as synchronized by Hashtable is internally synchronized and can't be
calling this code unsynchronized.
Map m =
Collections.synchronizedMap(hashMap);
HashMap is traversed by Iterator. Hashtable is traversed by Enumerator and
Iterator.
Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.
HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.

Difference between Method Overloading and Method Overriding

Method Overloading Method Overriding


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

Difference between StringBuffer and StringBuilder

StringBuffer StringBuilder
StringBuffer is synchronized i.e. thread safe. It StringBuilder is non-synchronized i.e. not thread
means two threads can't call the methods of safe. It means two threads can call the methods
StringBuffer simultaneously. of StringBuilder simultaneously.
StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.
Difference between Object and Class

Object Class
Object is an instance of a class. Class is a blueprint or template from which
objects are created.
Object is any real-world entity that is physically Class is a group of similar objects.
present or that can be experienced.
Object is a physical entity. Class is a logical entity.
Object is created through new keyword mainly Class is declared using class keyword e.g.
e.g. class Student{}
Student s1=new Student();
Object is created many times as per requirement. Class is declared once.
Object allocates memory when it is created. Class doesn't allocated memory when it is
created.
There are many ways to create object in java such There is only one way to define class in java using
as new keyword, newInstance() method, clone() class keyword.
method, factory method and deserialization.

Difference between String and StringBuffer

String StringBuffer
String class is immutable. StringBuffer class is mutable.
String is slow and consumes more memory when StringBuffer is fast and consumes less memory
you concatenate too many strings because every when you concatenate strings.
time it creates new instance.
String class overrides the equals() method of StringBuffer class doesn't override the equals()
Object class. So you can compare the contents of method of Object class.
two strings by equals() method.

Difference between throw and throws

Throw throws
Java throw keyword is used to explicitly throw an Java throws keyword is used to declare an
exception. exception.
Checked exception cannot be propagated using Checked exception can be propagated with
throw only. throws.
Throw is followed by an instance. Throws is followed by class.
Throw is used within the method. Throws is used with the method signature.
You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws IOException,
SQLException.
Difference between == and equals()

== equals()
It is an operator. It is a method.
In case of non-primitive, == operator compares the In case of non-primitive, the .equals() method is
reference (address). That is == checks if both used for content comparison. That is, .equals()
objects point to the same memory location. evaluates to the comparison of values in the
objects.
No such things with related to == If a class does not override the equals method,
then by default it uses equals(Object o) method
of the closest parent class that has overridden
this method.
== is used mostly to compare the primitive values. Equals() method is used to compare the contents
of the object.

Difference between static and dynamic binding

Early/Static Binding Late/Dynamic Binding


Occurs at compile time. Occurs at run time.
Used to resolve overloaded methods. Used to resolve overridden methods.
Decision about which implementation has to be Decision about which implementation has to be
executed is taken by the compiler based on the executed is taken by the JVM based on the
arguments. invoking objects.
private, static and final methods are resolved by Virtual/instance/non-static methods are resolved
static binding as they cannot be overridden. by dynamic binding as they can be overridden.
Actual object us not used to locate the method. Actual object is used to locate the method.
Instead, they type information of the variable is
used to locate the method.
Possible even if there is no [is-a] relationship. Possible only if there is a [is-a] relationship.
Method’s signature has to vary in the event of Method’s signature must not vary where in the
using method overloading also the method method implementation must definitely vary in
implementation may vary. case of method overriding.

You might also like