In this article, we will go over the ArrayList in great detail.


Sometimes in our programs we need to deal with collection of data that connected with some logic together. It would be great to put them all in one box and carry them all together. The array is data type for that exact purpose, It can hold collection of data.

The array is a collection of data of the same type.

  • The array is a fixed-size collection of data. We need to specify the size of the array while creating it, and the size cannot be changed.
  • We have random access to each element in the array by using its index…


  1. Method Overloading
  2. Method Overriding

Method Overloading

  1. Method overloading is when more than one method with the same name but a different number or type of arguments defined in the same class.
  2. The return type of the method can be different.
public class CalculatorUtil {   public static int sum(int i, int i2) {
return i + i2;
}
public static int sum(int i, int i2, int i3) {
return i + i2 + i3;
}

public static double sum(double i, double i2) {
return i +…


The ability of an object to take many forms.

Photo by Nitty Ditty on Unsplash

We can create polymorphic objects when we have a parent-child relationship between classes or interfaces and their implementations.

Let’s say we have these classes


The main method has a mechanism to accept input when run from the terminal.

terminal

Run from terminal

Let’s say I have this class

public class Main {
public static void main(String[] args) {
System.out.println("hello, world!");
}
}

This code is saved in Main.java file under my desktop. In order to run it from the terminal, I will need to navigate with my terminal to my desktop and execute these commands:

javac Main.java — to compile the source code into java byte code.

java Main — to run without any arguments.

Run from the terminal with arguments

public class Main {
public static void main(String[] args) {…


Converting String to numeric values

To convert String data type to numeric data type we can use wrapper classes of numeric primitives.

public class Main {
public static void main(String[] args) {
String strNum = "12";
int num = Integer.parseInt(strNum);
System.out.println(num); // 12
}
}

We convert similarly for other numeric data types by using their wrapper class versions.

public class Main {
public static void main(String[] args) {
String strNum = "12av";
// NumberFormatException here
int num = Integer.parseInt(strNum);
System.out.println(num);
}
}

If we try to convert a string that is not a number(the content of the string is not a number), we will…


Abstraction focuses on what an object does instead of how it does it.

Photo by Paweł Czerwiński on Unsplash

Abstraction allows us to focus on what an object does instead of how it does. Abstraction is achieved by abstract methods. In java, abstract methods can be created in the abstract classes and interfaces.

  1. Abstract class.
  2. Interface.

Abstract class

  • An abstract class is a special class in Java that can have abstract methods.
  • We cannot initialize abstract class directly.
  • The abstract class can have regular methods and properties as well.
  • Abstract class extend another abstract class but implementation of abstract methods are not required.
  • The first non-abstract class that…


  1. Inheritance.
  2. Method overriding and variables hiding.
  3. this and super keywords.

Inheritance

Inheritance is a process where one class can inherit visible properties and methods from another class — the parent-child relationship between two classes (or superclass and subclass).

// in Person.java file
public class Person {
public String name;
public String address;
public int age;

public void walk() {
System.out.println(name + " is walking.");
}
}// in Student.java file
public class Student extends Person {
public static void main(String[] args){
Student student = new Student();
student.name = "John Doe";
student.address = "101 Main St";
student.age = 22;
student.walk();
}
}
Student extends Person

In…


in Person.java file

Encapsulation is a data hiding mechanism. We achieve encapsulation by making our variables private and providing public getters and setters.

Why do we need to encapsulate? If the property has public access, the client code can have direct access and can assign any value. By encapsulating we have one layer where we can control what comes to our property in our setter method. In the above example, we can see how we are restricting negative age in the setAge method. Another example — let’s say we are creating a custom List data structure based on an array. The underlining array…


The final keyword can be used with variables, methods, and classes.

Photo by Maxim Zhgulev on Unsplash

The final keyword is a special specifier that you can put together with variable, method, and class declarations.

  1. Final variable.
  2. Final method.
  3. Final class.

Final variable

Let’s start with the variables. Once we assigned a value for the final variable there is no way we can reassign it.

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store