Skip to content

Commit 2888af4

Browse files
committed
Add a few blogpost
1 parent 1a7e85e commit 2888af4

4 files changed

+284
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
layout: post
3+
title: "Difference Between Abstract Class and Interface In Java"
4+
author: gaurav
5+
categories: [Java, Java Interview Questions]
6+
toc: true
7+
description: "In this tutorial, we will see difference between abstract class and interface in Java."
8+
---
9+
10+
In this tutorial, we will see difference between abstract class and interface in Java.
11+
12+
## Introduction
13+
14+
Interfaces form a contract between **the class and the outside world**, and this contract is enforced at build time by the compiler.
15+
16+
An *abstract class* is a class that is **declared with `abstract` keyword**—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
17+
18+
We all know that Defaults and Static methods are introduced in Java 8.
19+
20+
We can have business logic in the interface with default and static methods. So the measure difference that interface can not have concrete methods and abstract class have concrete methods is no more. But still there are many differences in the interface and abstract class as discussed below.
21+
22+
## Difference Between Interface And Abstract Class After Java 8 i.e. Interface Vs Abstract Class After Java 8
23+
24+
| Property | Interface | Abstract Class |
25+
| -------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
26+
| Variables | **Every variable present inside the interface is always public, static, and final,** whether we are declaring or not. | The variables present inside the abstract class **need not be public static and final.** We can have other types of variables too. |
27+
| Variables | We **can not declare protected, transient and volatile variables** in the interface. | There are **no restrictions on abstract class variable modifiers**. We can have protected, transient and volatile variables in the Abstract class. |
28+
| Default Methods | After Java 8, an i**nterface can have default methods.** | **Abstract Class can not have default methods**. |
29+
| Final Methods | As we know, an interface can have default and static methods along with abstract methods. But **Interfaces don’t support final methods**. | **Abstract classes support final as well as non-final methods** and static as well as non-static methods along with abstract methods. |
30+
| Constructors | We **can not have constructors** inside the interface. | We **can declare a constructor inside an abstract class**, that can be used at the time of child object creation. |
31+
| Multiple Inheritance | **A class can implement multiple interfaces**. So multiple inheritances are possible with Interfaces. | **A class can extend only one abstract class**. So multiple inheritances are not possible with abstract classes. |
32+
33+
## Difference Between Interface And Abstract Class Before Java 8 i.e. Interface Vs Abstract Class Before Java 8
34+
35+
Here we will see difference between interface and abstract class before Java 8 version.
36+
37+
| Property | Interface | Abstract Class |
38+
| ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
39+
| When to use? | If we **don't know anything about implementation and we just have requirement specifications** then we should go for an interface. | If we do not have a complete idea about implementation then we should go for the Abstract class. i.e **In case of partial implementation**, we should go for Abstract Class. |
40+
| Methods | Inside the interface, **all methods are public and abstract** by default. Hence we can achieve 100% abstraction with an interface. | Every method present in the abstract class need not be public and abstract. **In addition to abstract methods, we can have concrete methods too**. |
41+
| Modifiers | Since all the methods are public and abstract, we **can not use any other modifiers. (Other than public and abstract)** | Since we can concrete methods in the Abstract class, **there are no restrictions on abstract class method modifiers.** |
42+
| Variables | **Every variable present inside the interface is always public, static, and final,** whether we are declaring or not. | The variables present inside the abstract class **need not be public static and final.** We can have other types of variables too. |
43+
| Variables | We **can not declare protected, transient and volatile variables** in interface. | There are **no restrictions on abstract class variable modifiers**. We can have protected, transient and volatile variables in Abstract class. |
44+
| Variable Initialisation | At the time of declaration, **we need to initialize the variables compulsorily**. Otherwise, we will get compile time error. | For Abstract class variables, **it is not necessary to initialize them at the time of declaration**. |
45+
| Blocks | Inside the interface, **we can not declare instances and static blocks**. Otherwise, we will get compile time error. | Inside the abstract class, we **can declare instances and static blocks**. |
46+
| Constructors | We **can not have constructors** inside the interface. | We **can declare a constructor inside an abstract class** that can be used at the time of child object creation. |
47+
| Multiple Inheritance | A **class can implement multiple interfaces**. So multiple inheritances are possible with Interfaces. | A **class can extend only one abstract class**. So multiple inheritances are not possible with abstract classes. |
48+
| Example | Servlet | GenericServlet , HttpServlet |
49+
50+
---
51+
52+
Please write your thoughts in the comment section below.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
layout: post
3+
title: "Java OOPs Concepts"
4+
author: gaurav
5+
categories: [Java, Java Interview Questions]
6+
toc: true
7+
description: "In this tutorial, we will see the basic object-oriented programming concepts."
8+
---
9+
10+
In this tutorial, we will see the basic object-oriented programming concepts.
11+
12+
## Introduction
13+
14+
Object-oriented programming (OOP) is *a computer programming model that organizes software design around data, or objects, rather than functions and logic*.
15+
16+
There are many opps concepts but here we will see the most important 4. I.e Abstraction, Encapsulation, Inheritance and Polymorphism.
17+
18+
## Java OOPs Concepts
19+
20+
### 1. Abstraction
21+
22+
**Hiding internal implementation** and just **hightailing the functionalities offered** is known as abstraction.
23+
24+
**Example:** ATM functions are a practical example of abstraction. They just highlight the functionality i.e withdrawing a money, checking balance or printing the mini statement. It does not show the internal implementation of that functions.
25+
26+
**Method in a Java class** is an example of Abstraction.
27+
28+
### 2. Encapsulation
29+
30+
The **process of grouping the data members and corresponding methods in a single unit** is called encapsulation.
31+
32+
**Every java class is the example of encapsulation.** Java bean is the fully encapsulated class because all the data members are private here.
33+
34+
**Example:**
35+
36+
```java
37+
public class Student {
38+
39+
private int id;
40+
41+
private String name;
42+
43+
public int getId() {
44+
return id;
45+
}
46+
47+
public void setId(int id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
}
60+
```
61+
62+
In the above code the data members like `id` , `name` and corresponding methods such as `setId()`, `getId` etc. are grouped together in a java class `Student`. So Java class is a good example of *Encapsulation*.
63+
64+
### 3. Inheritance
65+
66+
***When a child object acquires all the properties and behaviours of a parent object***, it is known as inheritance. It provides code re-usability. It is a `IS-A ` relationship.
67+
68+
```java
69+
public class Animal {
70+
71+
private String name;
72+
73+
public void run() {
74+
75+
}
76+
77+
public void feed() {
78+
79+
}
80+
81+
}
82+
83+
class Dog extends Animal{
84+
85+
private String breed;
86+
87+
public void bark() {
88+
89+
}
90+
}
91+
```
92+
93+
In the above example `Animal` class is a Parent class and `Dog` class is `child` class.
94+
95+
Animal can have name, it can run.
96+
97+
We have created a child class `Dog` which `extends` the `Animal` class. That means `Dog` class inherits the properties and behaviours of the parent class `Animal`.
98+
99+
Dog can have a name, it can run and in addition it can have it's own property like `breed` and behaviours like `bark()`.
100+
101+
### 4. Polymorphism
102+
103+
If *one task is performed in different ways*, it is known as polymorphism.
104+
105+
The word ***poly*** means ***many*** and ***morphs*** means ***forms or ways***.
106+
107+
In Java, we use method overloading and method overriding to achieve polymorphism.
108+
109+
## Conclusion
110+
111+
Abstraction, Encapsulation, Inheritance and Polymorphism are the most important oops concepts in java.
112+
113+
1. In Abstraction we **hide the internal implementation** and **highlight the functionalities offered**. Ex. method in java
114+
115+
2. Encapsulation is the **process of grouping data members and corresponding methods in a single unit**. Ex. Java Class
116+
117+
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**.
119+
120+
---
121+
122+
Please write your thoughts in the comment section below.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
layout: post
3+
title: "Check If An Array Is Sorted In Java"
4+
author: gaurav
5+
categories: [Java Programs, Array]
6+
toc: true
7+
description: "In this tutorial, we will see a Java program to check if an array is sorted in java."
8+
---
9+
10+
In this tutorial, we will see a Java program to check if an array is sorted in java.
11+
12+
## Introduction
13+
14+
We will write a Java program to check if the array is sorted or not.
15+
16+
We will take a sample integer array `arr = {1, 3 , 5, 9};` We will return `true` if the array is sorted or `false` if the array is not sorted.
17+
18+
## Check if an array is sorted or not in Java
19+
20+
```java
21+
/**
22+
* A Java program to check if the array of integers is sorted
23+
* or not using for loop iteration.
24+
*
25+
* @author coderolls.com
26+
*
27+
*/
28+
public class SortedArrayTester {
29+
30+
public static void main(String[] args) {
31+
32+
int[] arr = {2, 5, 9, 8, 11, 18, 13};
33+
34+
boolean isSorted = isArraySorted(arr);
35+
36+
// use this to check the sorted case
37+
38+
/*
39+
* int[] arr2 = {2, 5, 9, 11, 18};
40+
* boolean isSorted = isArraySorted(arr2);
41+
*/
42+
43+
if(isSorted) {
44+
System.out.println("The array is sorted.");
45+
}
46+
else {
47+
System.out.println("The array is not sorted.");
48+
}
49+
}
50+
51+
private static boolean isArraySorted(int[] arr) {
52+
int n = arr.length;
53+
54+
for(int i=0; i<n-1; i++) {
55+
56+
//in sorted array current number should be
57+
//always less than the next number in array
58+
if(arr[i]>arr[i+1]) {
59+
return false;
60+
}
61+
}
62+
return true;
63+
}
64+
}
65+
```
66+
67+
Output:
68+
69+
```
70+
The array is not sorted.
71+
```
72+
73+
### Explanation:
74+
75+
1. In the `main` method we are taking a sample array `arr` and passing it to the `isArraySorted()` method to check if the array is sorted or not. This method returns a `boolean` value. `true` - if an array is sorted, `false` - if an array is not sorted
76+
2. In the `isArraySorted()` method, we are iterating over an array from index `0` to `n-1`, (`n`-length of the array) and we will check if the current number (number at index `i`) is greater than the next number ( number at index `i+1`).
77+
3. If the current number (number at index `i`) is greater than the next number ( number at index `i+1`), that means the array is not sorted, and we will return `false`.
78+
4. If the control came out of for loop, that means the array is sorted and we will return `true`.
79+
5. When we receive the `boolean` result `isSorted` we will print the output.
80+
81+
82+
83+
## For array of custom object
84+
85+
1. if custom object implements comparable (Coming Soon)
86+
2. if custom object does not implement comparable (Coming Soon)
87+
88+
## Conclusion
89+
90+
we can check if the primitive arrays are sorted or not by comparing the adjacent array elements.
91+
92+
---
93+
94+
The example java programs used in the above article can be found at this GitHub repository, [blogpost-coding-examples/java-programs/second-largest-element-in-an-array/](https://github.com/coderolls/blogpost-coding-examples/tree/main/java-programs/second-largest-element-in-an-array).
95+
96+
Please write your thoughts in the comment section below.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
layout: post
3+
title: "How To Reverse an Array in Java?"
4+
author: gaurav
5+
categories: [Java Programs, Array]
6+
toc: true
7+
description: "In this tutorial, we will see how to reverse an array in Java?"
8+
---
9+
10+
11+
12+
---
13+
14+
Please write your thoughts in the comment section below.

0 commit comments

Comments
 (0)