How to create a Java Maven project using Command Line in IntelliJ

HOME

In the previous tutorial, I have explained How to create Maven Java project in Intellij without Command Line. In this tutorial, I will explain about creation of Java Maven Project using Command Line in IntelliJ.

Step 1 – Open IntelliJ IDEA IDE. Click on Terminal Button as shown in the below image.

Step 2 – Go to the path where you want to create the project. This can be done by typing command

cd path of folder

Step 3 – This tells Maven to generate a Java project from a Maven template.

mvn archetype:generate

Step 4 – We need to mention the number as displayed on your screen in Command Prompt to proceed further. Like here, Choose a number or apply filter has 1771, so I have also mentioned 1771 in command prompt.

Step 5 – We need to provide again input in command prompt. This time program wants to know which version we want to use. I prefer to use the latest version. Here, it is 8, so I have selected version 8.

Step 6 – We need to provide 2 input here

A) Value of groupId – This serves as the group identifier of your Maven project, it should be in a form similar to Java packages, such as com.example
B) Value of artifactId – This serves as the group-local identifier of my Maven project like MavenIntelliJFromCMD
C) Value of Version – The initial version of our project. The default is 1.0-SNAPSHOT
D) Value of package – The name of our root package. The default is groupId we have created earlier.
We will notice the INFO message about the properties.

If the displayed settings are correct, then just enter Y in :: prompt.

Step 7 – Successful Build – Below screenshot shows that the Maven IntelliJ Project built successfully.

Step 8 – Contents of Project Folder – Open folder MavenIntelliJFromCMD to see the contents of the folder. It should have POM file and src.

This project can be imported in IntelliJ and can be used further. To know how to import Java Maven project in Intellij, click here.

That’s it! We have created a Maven Project in IntelliJ using Command Line.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

How to import Java Maven project in IntelliJ

Home

In the previous tutorial, I have explained How to create a Maven Project in IntelliJ. In this tutorail, I will explain how we can import a Java Maven project in IntelliJ IDE.

Step 1 – Open IntelliJ IDEA and Welcome Screen appears. Click Open button present on Welcome Screen.

Step 2 – Navigate to your Maven project and select the top-level folder. Select the project you want to Import. As you can see, I have selected – MavenProjectFromCMD option. Select OK button to proceed to next screen.

Step 3 – A screen appears to Open or Import project. It will have all the possible configurations for the project. As this is a Maven project, select Maven project and click OK Button.

Step 4 – A warning message box will appears. Select Trust Project Button and move forward.

Step 5 -This screens shows that the project is imported.

Step 6 – Run the test present in the project. Here, I have run AppTest. Right click on AppTest ->Run ‘AppTest’. Below screen shows that the project is imported successfully.

That’s it! So Simple!!!

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to create Maven project in IntelliJ

HOME

In the previous tutorial, I explained how to create a Simple Java project in IntelliJ IDEA IDE. In this tutorial, I will explain about creating a Maven project in IntelliJ IDE.

Step 1 – Open the IntelliJ. It will look as shown below. To create a New Project, click on New Project Icon.

Step 2 – Click on File Option, hover on New Option, and click on Project Option as shown below.

Step 3 – Select New Project as Maven.
Project SDK should be current Java version available.
Select option for Create from archetype. This will enable the options to select archetype.
Select archetype : maven-archetype-quickstart.
Click on the Next Button.

Step 4 – Below screen will appear. Mention the Name, Group Id, Artifact Id, and Version. Click the Finish button

Name : MavenIntelliJDemo
Group Id : com.example
Artifact Id : MavenIntelliJDemo
Version : 1.0-SNAPSHOT

Step 5 – Verify the information in the below screen. Click on Finish Button.

Step 6 – This dialog box will appear on the screen. This provides you the option to open the project in the current window or will open a new window with this project. I’m selecting the option – New Window.

Step 7 – This screen shows the structure of the Maven project as well as the POM.xml file.

Step 8Project Folder Creation – We can see a folder with the name of the project – MavenIntelliJDemo in our Eclipse Workspace.

This is how we can create the Maven project – MavenIntelliJDemo in IntelliJ.

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to create a Java project in IntelliJ

HOME

In the previous tutorial, I explained how to download and install IntelliJ on Windows. In this tutorial, I will explain how to create a Java project in IntelliJ.

  1. Open the IntelliJ. It will look as shown below. To create a New Project, click on the “New Project” Icon.

2. Select Project SDK as the Java version present on your system and select the option as Java. Click on the Next” button.

3. The next screen provides options to choose Project Template as shown below. Click on the Next” Button.

4. I have mentioned the Project Name as Java Demo and the path where I want to save my project on my computer.

5. Click on the “Finish” Button to create the project. It will also ask to create the directory as shown below.

6. Click the “Create” button to grant permission. It will launch IDE as shown below.

Now, I have created a new Java Project named Java Demo. Let me create a new Java Class – HelloWorld.

  1. Expand the Project Hello Word and right-click on the src folder, hover on New Option, and click on Java Class Option as shown below.

2. I have provided the Java Filename with the package as shown in the below image. Now press the Enter Key to create the packages and Java file.

3. It will show a message to Add the File to Git. Depending on your requirement, select Add or Cancel. I don’t want to add the File to Git, so selected Cancel.

4. I have updated the “HelloWorld” class to print the first Java Class – Hello World on the console, as shown in the below image.

5. Now right-click on the “HelloWorld” class file and choose Run HelloWorld.main()” Option or press Ctrl + Shift + F10 to compile and execute the program as shown in the below image. It will also build the project.

6. The final output of the program will be displayed on the console as shown below.

This is how we can create the Java Demo project to execute our first program in Java – HelloWorld.

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to install IntelliJ on Windows

HOME

IntelliJ IDEA is one of the smartest IDE for Java development. It’s also the base of the official Android IDE i.e. Android Studio used for Android mobile application development. This tutorial provides all the steps required to install IntelliJ Idea on Windows 10 for Java development.

Download IntelliJ on Windows

  1. Click this link to download IntelliJ IDEA on your machine. It will open the download page as shown below. The IntelliJ IDEA provides two different installers i.e. Ultimate and Community. The Ultimate edition is commercial version (which has trial version for 30 days post which you needs license). The Community edition can be used for free. This tutorial shows the steps required to install the Community edition.

2. Download IntelliJ by clicking on Download button and use any one of the option either .exe or .jar. Below image shows IntelliJ.exe on your machine.

Installation of IntelliJ IDEA

  1. To start installation, double click on the idealC-2021.exe (this version can change in future).

2. It will show the welcome screen as shown below. Click on the Next Button to continue with the installation.

3. It will provide options to choose the installation location as shown in below image. Make sure that you have sufficient space available on the selected drive since it will consume around 1.6 GB storage. Click on the Next Button after choosing the appropriate installation path.

4. It will show the Installation options as shown below to configure system path, create 64-bit desktop launcher icon, Open Folder as Project, and file association for Java, Groovy, and Kotlin files. I have selected the option to create a desktop launcher icon. You may choose the options based on your requirements. Click on the Next Button after choosing the Installation Options.

5. After choosing the Installation Options, the next screen shows options to configure the Start Menu Folder as shown below.

6. Click on the Install Button after selecting the Start Menu Folder option. It will show the installation progress as shown below.

7. It will show the final screen after successfully installing IntelliJ IDEA as shown below.

8. Click on the Finish Button to close the installer. Below image shows that IntelliJ icon is placed at Desktop as we have selected in the previous step.

That’s it! We have downloaded and installed IntelliJ. Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

In the next tutorial, I’ll explain how to create a new project in IntelliJ.

Difference between Abstract Class and Interface

HOME

Abstract Class and Interface are used to achieve Abstraction where abstract methods are declared. We can’t instantiate Abstract Class and Interface.

Abstract ClassInterface
Abstract Class can have both abstract method and
non-abstract methods.
Interface can have only abstract methods.
Abstract Class does not support multiple Inheritance.Interface supports multiple Inheritance.
Abstract Class can be implemented by using keyword “extends”. Interface can be implemented by using keyword “implements”.
Abstract class can provide the implementation of interface.Interface can’t provide the implementation of abstract class.
Abstract Class can have static, non-static, final &
non-final variables.
Interface can have only final & static variables.
Abstract Class can have class members as private, protected, etc.Interface can have class members as public by default.
Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
An abstract class can extend another Java class and
implement multiple Java interfaces.
An interface can extend another Java interface only.
Abstract keyword is used to declared abstract classInterface keyword is used to declare interface.

Below is an example of Abstract Class. In the below example, there is an abstract class with abstract method. This abstract method is implemented in SubClass.

//Abstract Class
public abstract class Bank {
 
    // Abstract method
    abstract void getRateOfInterest();
}

SubClass

//Subclass (inherit from Bank)
public class SBI extends Bank {
 
    // The body of getRateOfInterest is declared here
    public void getRateOfInterest() {
 
        System.out.println("Interest Rate of SBI Bank :" + 5.3);
 
    }
}


Test Class

public class AbstractionTest {
 
    public static void main(String[] args) {
 
        Bank bank1 = new SBI();
        bank1.getRateOfInterest();
 
    }
 
}

Below is an example of Interface. In the below example, Interface has two abstract methods and the implementation of abstract methods are declared in Test Class.

public interface MyInterface {
 
    // Abstract methods
    public void method1();
 
    public void method2();
}

Test Class

public class InterfaceTest implements MyInterface {
 
    public void method1() {
        System.out.println("Implementation of Method 1");
    }
 
    public void method2() {
        System.out.println("Implementation of Method 2");
    }
 
    public static void main(String[] args) {
        MyInterface interTest = new InterfaceTest();
        interTest.method1();
        interTest.method2();
 
    }
}

Interface in Java

HOME

In this tutorial, I’ll explain about Interfaces in Java.

What is Interface?

An interface is a group of related methods with empty bodies (abstract methods).

  1. All the methods in Interface are public and abstract. 
  2. A class can implement more than one interface.
  3. An interface can extends another interface or interfaces 
  4. A class that implements interface must implements all the methods in interface. To implement interface use implements keyword.
  5. The variables declared in an interface are public, static & final by default.
  6. Java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .

Syntax of Interface

interface test {
//declare methods which are abstract by default
//declare variables which are public, static, final by default
}

Relationship between Class and Interfaces

Below is an example of implementation of Interface. I have declared a class as Interface which have two abstract methods.

public interface MyInterface {

	// Abstract methods
	public void method1();

	public void method2();
}

Interface Implementation

public class InterfaceTest implements MyInterface {

	public void method1() {
		System.out.println("Implementation of Method 1");
	}

	public void method2() {
		System.out.println("Implementation of Method 2");
	}

	public static void main(String[] args) {
		MyInterface interTest = new InterfaceTest();
		interTest.method1();
		interTest.method2();

	}
}

Output

Implementation of Method 1
Implementation of Method 2

Multiple Interface

In the below example, Interface 2 extends Interface 1 and class implements Interface 2.

Interface 1

public interface Infa1 {
	public void method1();

}

Interface 2

public interface Infa2 extends Infa1 {
	public void method2();

}

MultipleInterfaceTest Class

public class MultipleInterfaceTest implements Infa2 {

	public void method1() {
		System.out.println("Implementation of Method 1");
	}

	public void method2() {
		System.out.println("Implementation of Method 2");
	}

	public static void main(String[] args) {

		Infa2 obj = new MultipleInterfaceTest();
		obj.method1();
		obj.method2();
	}

}

Output

Implementation of Method 1
Implementation of Method 2

Multiple Inheritance

Interface supports multiple Inheritance. Here, there are two interfaces which are implemented in class MultipleInheritanceTest.

Interface 1

public interface Infa1 {
	public void method();

}

Interface 2

public interface Infa2 {
	public void method();

}

MultipleInheritanceTest Class

public class MultipleInheritanceTest implements Infa1, Infa2 {

	public void method() {
		System.out.println("Implementation of Method ");
	}


	public static void main(String[] args) {
		MultipleInheritanceTest test = new MultipleInheritanceTest();
		test.method();
	}

}

Output

Implementation of Method

Abstraction in Java

HOME

In this tutorial, I’ll explain about abstraction in Java.

What is Abstraction?

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

There are two ways to achieve abstraction in java

  1. Abstract class
  2. Interface

What is an Abstract Class?

A class which is declared as abstract is known as an abstract class.

  1. It can have abstract and non-abstract methods.
  2. It needs to be extended and its method is implemented.
  3. It cannot be instantiated.
  4. It can have constructors and static methods also.
  5. It can have final methods which will force the subclass not to change the body of the method.
  6. An abstract class is a class that contains at least one abstract method.
  7. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

Example of Abstract Class

abstract class Test
{
}

What is Abstract Method?

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract int test(int a, int b);

Below is an example of abstraction.

Abstract Class

//Abstract Class
public abstract class Bank {

	// Abstract method
	abstract void getRateOfInterest();
}

SubClass1

//Subclass (inherit from Bank)
public class SBI extends Bank {

	// The body of getRateOfInterest is declared here
	public void getRateOfInterest() {

		System.out.println("Interest Rate of SBI Bank :" + 5.3);

	}
}

SubClass2

//Subclass (inherit from Bank)
public class BOI extends Bank {

	// The body of getRateOfInterest is declared here
	public void getRateOfInterest() {

		System.out.println("Interest Rate of BOI Bank :" + 4.1);

	}

}

Test Class

public class AbstractionTest {

	public static void main(String[] args) {

		Bank bank1 = new SBI();
		bank1.getRateOfInterest();

		Bank bank2 = new BOI();
		bank2.getRateOfInterest();

	}

}

Output

Interest Rate of SBI Bank :5.3
Interest Rate of BOI Bank :4.1

Abstract class can have a data member, constructor, abstract method, non abstract method and even main() method.

Below is an example where the abstract class has both abstract method as well as non abstract method.

public abstract class Bike { // abstract class

	int x = 25; // Instance Variable

	Bike() // Constructor
	{
		int y = 10;

		System.out.println("Value of y :" + y);
		System.out.println("Bike is created");

	}

	abstract void run(); // abstract method

	void change_gear() // non abstract method
	{
		System.out.println("Gear is changed");
	}

}

SubClass

public class Trek extends Bike {

	void run() {
		System.out.println("Bike is running");
	}

}

Test Class

public class AbstractionDemoTest {

	public static void main(String[] args) {

		Bike bike = new Trek();
		bike.run();
		bike.change_gear();

		System.out.println("Value of x :" + bike.x);

	}
}

Output

Value of y :10
Bike is created
Bike is running
Gear is changed
Value of x :25

Advantages of Abstraction

  1. It reduces the complexity of viewing the things.
  2. Avoids code duplication and increases reusability.
  3. Helps to increase security of an application or program as only important details are provided to the user.

Encapsulation in Java

HOME

Encapsulation in Java is a process of wrapping the data and code in a single unit. The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class. It is one of the four important OOPs concept.

To achieve encapsulation in Java −

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to modify and view the variables values.

The get method returns the variable value, and the set method sets the value.

Syntax of get method – method name starts with get followed by the variable name where first letter is capital.

Syntax of set method – method name starts with set followed by the variable name where first letter is capital.

Below is an example of encapsulation with two variables and their getter and setter method.

public class EncapsulationDemo {

	// private variables
	private String name;
	private int age;

	// getter method for age
	public int getAge() {
		return age;
	}

	// setter method for age
	public void setAge(int setAge) {
		this.age = setAge;

	}

	// getter method for name
	public String getName() {
		return name;
	}

	// setter method for name
	public void setName(String newName) {
		this.name = newName;
	}

}

The variables of the EncapsulationDemo class can be accessed using the following program −

public class EncapsulationTest {

	public static void main(String[] args) {

		//creating instance of the encapsulated class
		EncapsulationDemo encap = new EncapsulationDemo();

		//setting value in the age member 
		encap.setAge(25);
		
		//setting value in the name member 
		encap.setName("Terry");

		//getting value of the age and name member  
		System.out.print("Name : " + encap.getName() + " and Age :" + encap.getAge());

	}

}

Output

Name : Terry  and Age :25

The encapsulate class is easy to test. So, it is better for unit testing.

The standard IDE’s are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.

Inheritance in Java

HOME

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another class.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Inheritance is applicable for public and protected members only. Private members can’t be inherited.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

The keyword used for inheritance is extends

Syntax

class derived-class extends base class
{
//methods and variables
}

Below is an example of Inheritance. In the below example, ParentCalculation is the Base class (parent class) and has a method addition. ChildCaclculation is the derived class (child class) which has extends base class ParentCalculation. When an object of ChildCaclculation class is created, a copy of all methods and fields of the superclass acquire memory in this object. That is why by using the object of the subclass we can also access the members of a superclass. 

Base Class

public class ParentCalculation {

	int z;

	public void addition(int x, int y) {
		z = x + y;
		System.out.println("The sum of the given numbers:" + z);
	}

}

Derived Class

public class ChildCaclculation extends ParentCalculation {

	int c;

	public void multiplication(int a, int b) {
		c = a * b;
		System.out.println("The multiplication of the given numbers:" + c);
	}

	public static void main(String[] args) {
		ChildCaclculation cal = new ChildCaclculation();
		cal.addition(10, 14);
		cal.multiplication(5, 6);

	}

}

Output

The sum of the given numbers:24
The multiplication of the given numbers:30

Types of Inheritance in Java

Below are the different types of inheritance available in Java. 

1. Single Inheritance

In single inheritance, subclass (derived) inherit the features of superclass (base). In the image below, class A serves as a base class for the derived class B.

Base Class

public class Employee {

	String name = "Tom";
	int emp_no = 12001;

	public void displayEmployee() {
		System.out.println("Base Class");

		System.out.println("Name :" + name);
		System.out.println("Emp_No :" + emp_no);
	}
}

Derived Class

package JavaDemo.Inheritance;

public class Department extends Employee {

	String deptName = "IT Division";

	public void displayDept() {
		System.out.println("Derived Class");

		System.out.println("Department Name :" + deptName);
	}

	public static void main(String[] args) {

		Department dept = new Department();

		// Derived method
		dept.displayDept();

		// Base Method
		dept.displayEmployee();

	}

}

Output

Derived Class
Department Name :IT Division
Base Class
Name :Tom
Emp_No :12001

2. Multilevel Inheritance

In this, derived class extends an base class as well as the derived class act as a base class for another derived class. In the below example, Employee is Base class, Department is intermediate derived class and SubDivision is derived class.

Base Class

package JavaDemo.Inheritance;

public class Employee {

	String name = "Tom";
	int emp_no = 12001;

	public void displayEmployee() {
		System.out.println("Base Class");

		System.out.println("Name :" + name);
		System.out.println("Emp_No :" + emp_no);
	}
}

Intermediate Derived Class

package JavaDemo.Inheritance;

public class Department extends Employee {

	String deptName = "IT Division";

	public void displayDept() {
		System.out.println("Intermediate Derived Class");

		System.out.println("Department Name :" + deptName);
	}

	public static void main(String[] args) {

		Department dept = new Department();

		// Intermediate Derived method
		dept.displayDept();

		// Base Method
		dept.displayEmployee();

	}

}

Derived Class

package JavaDemo.Inheritance;

public class SubDivision extends Department {

	String sub_division = "Production Support";

	public void displaySubDivision() {
		System.out.println("Derived Class");

		System.out.println("SubDivision Name :" + sub_division);
	}

	public static void main(String[] args) {

		SubDivision div = new SubDivision();

		// Derived method
		div.displaySubDivision();

		// Intermediate Derived method
		div.displayDept();

		// Base Method
		div.displayEmployee();

	}

}

Output

Derived Class
SubDivision Name :Production Support
Intermediate Derived Class
Department Name :IT Division
Base Class
Name :Tom
Emp_No :12001

3.  Hierarchical Inheritance

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclasses.

In the below example, Employee is the base class. Department and Salary are the derived classes.

Base Class

package JavaDemo.Inheritance;

public class Employee {

	String name = "Tom";
	int emp_no = 12001;

	public void displayEmployee() {
		System.out.println("Base Class");

		System.out.println("Name :" + name);
		System.out.println("Emp_No :" + emp_no);
	}
}

Derived Class1

public class Department extends Employee {

	String deptName = "IT Division";

	public void displayDept() {
		System.out.println("Derived Class 1");

		System.out.println("Department Name :" + deptName);
	}

	public static void main(String[] args) {

		Department dept = new Department();

		// Derived method
		dept.displayDept();

		// Base Method
		dept.displayEmployee();

	}

}

Derived Class2

package JavaDemo.Inheritance;

public class Salary extends Employee {

	float Salary = 65000;

	public void displaySalary() {
		System.out.println("Derived Class 2");
		System.out.println("Salary :" + Salary);
	}

	public static void main(String[] args) {

		Salary sal = new Salary();
		sal.displaySalary();
		sal.displayEmployee();

		System.out.println("=========================");

		Department dept = new Department();
		dept.displayDept();
		dept.displayEmployee();

	}

}

Output

Derived Class 2
Salary :65000.0
Base Class
Name :Tom
Emp_No :12001
=========================
Derived Class 1
Department Name :IT Division
Base Class
Name :Tom
Emp_No :12001

4. Multiple Inheritance

In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Java does not support multiple inheritances with classes. We can achieve multiple inheritances only through Interfaces.

5. Hybrid Inheritance

 It is a combination of single and multiple inheritance. As Java doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.