Skip to content

Commit a78a8f8

Browse files
committed
Add drafts folder and a blogpost
1 parent 309b52c commit a78a8f8

6 files changed

+133
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: post
3+
title: "Method Overriding In Java"
4+
author: gaurav
5+
categories: [Java, Java Interview Questions]
6+
toc: false
7+
description: "In this tutorial, we will see the method overriding in Java."
8+
---
9+

‎_posts/java-interview-questions/2022-10-23-java-oops-concepts.md renamed to ‎_posts/java-interview-questions/core-java-interview-questions/2022-10-23-java-oops-concepts.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ If *one task is performed in different ways*, it is known as polymorphism.
104104

105105
The word ***poly*** means ***many*** and ***morphs*** means ***forms or ways***.
106106

107-
In Java, we use method overloading and method overriding to achieve polymorphism.
107+
In Java, we use [method overloading](/method-overloading-in-java/) and [method overriding](/method-overriding-in-java/) to achieve polymorphism.
108+
109+
There are two types of polymorphism in Java:
110+
111+
1. Compile-time Polymorphism
112+
2. Runtime Polymorphism.
113+
114+
Compile-time polymorphism can be achieved by [**overloading the static method**](/method-overloading-in-java/). While Runtime Polymorphism can be achieved by [**overriding the parent class method in the child class**](/method-overriding-in-java/).
108115

109116
## Conclusion
110117

@@ -115,7 +122,7 @@ In Java, we use method overloading and method overriding to achieve polymorphism
115122
2. Encapsulation is the **process of grouping data members and corresponding methods in a single unit**. Ex. Java Class
116123

117124
3. In inheritance, **a child class inherits all the properties and behaviour of parent class**. It can be done by extending the parent class. Ex. `class Dog extends Animal{...}`
118-
4. **Doing one task in many ways** is called polymorphism. In java, polymorphism can be done using **method overloading and method overriding**.
125+
4. **Doing one task in many ways** is called polymorphism. In java, polymorphism can be done using [method overloading](/method-overloading-in-java/) and [method overriding](/method-overriding-in-java/).
119126

120127
---
121128

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
layout: post
3+
title: "Method Overloading In Java"
4+
author: gaurav
5+
categories: [Java, Java Interview Questions]
6+
toc: false
7+
description: "In this tutorial, we will see the method overloading in Java."
8+
---
9+
10+
In this tutorial, we will see the method overloading in Java.
11+
12+
## Introduction
13+
14+
It is known as **Method Overloading** when a class has the same name but a different method signature.
15+
16+
For example, when we have a method `add()`, we can have it in multiple forms.
17+
18+
For example `add(int a, int b)` or `add(int a, int b, int c)` or `add(double a, double b)`
19+
20+
---
21+
22+
> **What Is Method Signature In Java**
23+
>
24+
> In Java, method signature contains method name and type of arguments.
25+
>
26+
> <method-name>(<argument-type-1>, <argument-type-2>...)
27+
>
28+
> Ex. `getData(int, String)`
29+
>
30+
> **Note:** In java, the method signature does not include the name of parameter, it only includes it's datatype.
31+
32+
---
33+
34+
Compile-time polymorphism can be achieved by Method overloading.
35+
36+
In Java, there are two ways to overload the method.
37+
38+
1. By changing number of arguments
39+
2. By changing the data type of arguments
40+
41+
## 1. By changing number of arguments
42+
43+
We can overload a method **by changing the number of arguments i.e parameters it receives**.
44+
45+
As shown in the example from the introduction, we can overload the method `add(int a, int b)` by making the parameters three. i.e `add(int a, int b, int c)`
46+
47+
```java
48+
public class Operation{
49+
50+
public static int add(int a, int b){
51+
return a+b;
52+
}
53+
54+
public static int add(int a, int b, int c){
55+
return a+b+c;
56+
}
57+
58+
}
59+
```
60+
61+
## 2. By changing the data type of arguments
62+
63+
We can overload a method **by changing the data type of the arguments i.e parameters it receives**.
64+
65+
As shown in the example from the introduction, we can overload the method `add(int a, int b)` by changing the data type of the parameters. i.e `add(double a, double b)`
66+
67+
```java
68+
public class Operation{
69+
70+
public static int add(int a, int b){
71+
return a+b;
72+
}
73+
74+
public static double add(double a, double b){
75+
return a+b;
76+
}
77+
78+
}
79+
```
80+
81+
Method overloading improves the readability of the code.
82+
83+
## Conclusion
84+
85+
It is known as **Method Overloading** when a class has the same name but a different method signature.
86+
87+
We can overload the method by changing the number of arguments or changing the data type of arguments.
88+
89+
---
90+
91+
Please write your thoughts in the comment section below.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
layout: post
3+
title: "ArrayList VS LinkedList"
4+
author: gaurav
5+
categories: [Java, ArrayList, Java Interview Questions]
6+
toc: false
7+
description: "In this article, we will see the difference between ArrayList and LinkedList."
8+
---
9+
10+
In this article, we will see the difference between ArrayList and LinkedList.
11+
12+
## ArrayList Vs LinkedList
13+
14+
{:class="table table-bordered"}
15+
| Property | ArrayList | LinkedList |
16+
| ------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
17+
| Internal Implementation | ArrayList internally uses a **dynamic array** to store the elements. | LinkedList internally uses a **doubly linked list** to store the elements. |
18+
| Preference | ArrayList is **preferred for storing and retrieving the data** from the list. | LinkedList is **preferred for inserting and deleting the data** in a list. |
19+
| Store and Retrieve Data | Storing and retrieving the data from the ArrayList is faster since it uses a **dynamic array** to store the elements.<br /><br /><br /> Time complexity for retrieving an element by id from the ArrayList is **O(1)**. | Storing and retrieving the data from the LinkedList is slower in comparison since it uses a **doubly linked list** to store the elements.<br /><br /><br /> Time complexity for retrieving an element by id from the LinkedList is **O(n)**. |
20+
| Data Manipulation ( Insert or delete data) | Manipulation with ArrayList is **slower** because it internally uses an array. <br />In case of removing an element from array or inserting an element in between array, all the other elements are shifted in memory. | Manipulation with LinkedList is **faster** than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. |
21+
| Interface implementation | An ArrayList class can **act as a list** only because it implements List interface only.<br /><br />`ArrayList implements List` | LinkedList class can **act as a list and queue** both because it implements List and Deque interfaces.<br /><br />`LinkedList implements List, Deque` |
22+
| Memory Location | The memory location for the elements of an ArrayList is **contiguous**. | The location for the elements of a linked list is **not contagious**. |
23+
| Default Capacity | On initialisation, a **default capacity of 10** is assigned to the ArrayList. | There is no case of default capacity in a LinkedList. An empty list is created when a LinkedList is initialized. |
24+

0 commit comments

Comments
 (0)