01/04/2023

Different Types of Variables in Java Programming Language
In this post, we will talk about Different Types of Variables in the Java Programming Language. Types of Variables in java with complete explanations and examples. Like a different type of variable how to use it?. how to access it etc?. So java is the class base language the class contains five elements. Java Class Elements are Variables, Methods, Constructors, Instance Blocks and Static Blocks.

So C/C++ language contains variable int a=10 b=20 etc. in the same way Java also contains some variable parts. So to work with employee projects we need the employee id, name, salary etc. So every project required variable concepts. So before going to the variable concept. First of all, we must know what is the purpose of the variable in Java. 

So the variable is used to store the values by using these values we achieve the project requirements. For example, for an employee project, we need the employee's salary, name, id etc. So in a single line variables are used to store value and by using these values we achieve the project requirements. generally, variables are 3 types.

Different Types of Variables in Java Programming Language

What Are the Three Different Types of Variables in Java

  
  1. Local Variables
  2. Instance Variables
  3. Static Variables
 

1. Local Variables

 
The variable is declared inside the method or constructor or blocks also. Those variables are called local variables

Local Variable Example


class {

	public static void main(String[] args) {
		//local variables
		int a = 50;
		int b = 20
		System.out.println(a);
		System.out.println(a);
	}
}

The scope of the local variables is only within the method or within the constructor or within the blocks only. So simply can say that the local variables cannot be accessed outside the method, block, or constructors. If we try to access the local variable outside it generates the error message.

Example


class demo {

	public static void main(String[] args) {
		void method() {
			int a = 30;
			System.out.println(a); //possible
		}

		void method2() {
			System.out.println(a); // not possible to print inside the method
		}
	}
}

Local variable Memory Allocated when the method starts and whenever the method is completed memory is released. Local variable stored in stack memory.

2. Instance Variables


So before the brief of instance variables, we must know areas of java language.java contains 2 types of areas, instance area, and static area. So once we understand these 2 areas then the java variables concept is very easy. So the body of the instance method is called the instance area, and the body of the static method is called the static method.

Instance Variables Example


class demo {
	void method() // instance method
	{
		//instance area
	}

	static void method2() //static method
	{
		//static area
	}

	public static void main(String[] args) {}
}

  • The variable that is declared inside the class but outside the method, Those variables are called instance variables.
  • The scope of the instance variable is inside the class all methods, constructors and blocks can access.
  • For instance, variable memory is allocated when the object is created and memory is destroyed when the object is destroyed.
  • Instance variable stored in heap memory

Note: When the same area we can access the instance variable directly. But in a different area, first of all, we must allocate memory of instance variable So how memory is allocated, linked with the object, means create an object of that class in a single line, first of all, create an object of that class. And access that class instance variable and method by using that object.

Instance var, method==========>direct access========>instance area

Instance var, method==============>throw object========>static area

Example


class demo {
	//instance var
	int x = 20;
	int y = 30;
	void m() //instance method
	{
		//instance area
		System.out.println(x + y);
	}

	public static void main(String[] args) //main method (static method)
	{
		//static area
		demo d new demo();
		System.out.println(d.x);
		System.out.println(d.y);
		d.m();
	}
}

3. Static Variable


The variable that is declared inside the class but outside the method with a static modifier, Those variables are called static variables.

Static Variable Example


class {
	//static variables
	static int a;
	static float b;
	static char c;
}

  • Static variable memory is allocated when the .class file is loading.
  • The scope of the static variable is inside the class
  • Static variable stored in non-heap memory.

Note:- access the static variable and method by using the class name. Reason static variable memory allocated when the .class file is loading. means before the main method load, static variable memory is allocated. Check this Java Program for Inter Thread Communication.

Example


class demo {
	//static var
	static int x = 20;
	static int y = 30;
	void m() //instance method
	{
		//instance area
		System.out.println(demo.x);
		System.out.println(demo.y);
	}

	public static void main(String[] args) //main method (static method)
	{
		//static area
		demo d = new demo();
		System.out.println(demo.x);
		System.out.println(demo.y);
		d.m();
	}

Read: Different Types of Inheritance in Java With Example
Also Read: How to Achieve Abstraction in Java With Example
How to Achieve Abstraction in Java With Example
How to Achieve Abstraction in Java With Example. By following the below method you can easily achieve Abstraction in Java. In Java programming, a class defined with the abstract keyword is said to be an abstract class. It can use both abstract and non-abstract methods within the body. If you have just marked your presence in the developing zone. Before jumping directly to the abstract class of Java, let’s explore the abstraction process first. Let's discuss how we achieve Abstraction in the Java language.

How to Achieve Abstraction in Java With Example

How to Achieve Abstraction in Java With Example

  
In Java, Abstraction can be achieved in two ways:

  1. By using abstract class
  2. By using interface

The interface helps you to achieve 100 per cent abstraction.

Abstract Class: 

An abstract class is declared with the abstract keyword. It can leverage both abstract and non-abstract methods in it. An abstract class is extended and all its methods are implemented. Always remember a few rules before defining an abstract class in Java Programming.

  1. It must be declared by abstract keyword. 
  2. It is comprised of both abstract and non-abstract methods. 
  3. It can include constructors as well as static methods. 
  4. It includes final methods which will not allow subclasses to alter the body methods. 

Declaration: 

abstract class X { }

Abstract Methods: 

The methods which are defined with the abstract keyword without any implementation are called the abstract method. E.g.,

abstract void sayName(); //without method body

Real-world Example of Abstract Class


Let’s understand the concept of an Abstract class and methods with the help of an example. Here I’m defining an abstract class named office with abstract method salary. The implementation of office class is provided by the emp and othworkers classes.

Read: Different Types of Inheritance in Java With Example

For end users- these implementation classes will be hidden. The factory method (it returns the class instance) will provide the implementation class object. In this program, on creating the othworkers class instance, the salary() method will be invoked.


abstract class office {
	abstract void salary();
}
class emp extends office {
	void salary() {
		System.out.println(“salaried employees”);
	}

	class othworkers extends office {
		void salary() {
			System.out.println(“salaried workers”);
		}

		class AbstractionCheck {

			public static void main(String args[]) {
				office w = new otheworkers(); //object provided through method
				w.salary();
			}
		}

In the above example, the implementation will be unknown to end-users and the methods will be called by the user. The output of the program will be salaried workers.

Another Example of Abstraction Achieved by Interface


Implementation of the interface can also be achieved by the abstract class. All the methods are overridden in such scenarios.


interface X {
	void p();
	void q();
	void r();
}


abstract class Y implements X {

	public void r() {
		System.out.println(Hello Its r”);
	}

	class S extends Y {

		public void p() {
			System.out.println(Hi its p”);
		}

		public void q() {
			System.out.println(Hi its q”);
		}

		public void r() {
			System.out.println(Hi its r”);
		}

		class Check {

			public static void main(String args[]) {
				X obj = new S();
				obj.p();
				obj.q();
				obj.r();
			}
		}

What is Abstraction Exactly in Java Programming?


In abstraction, we only show functionalities to a user by hiding the implementation and other details. Or, you can say displaying only useful information and hiding internal details. Let’s understand this with the help of an example, On your mobile phone, you set alarms without even knowing its internal functionalities like how it works. 

You can join the Java Certification Course which will help you to understand the core basics of Java programming with real-world application development. Abstraction allows you to concentrate on objects' work rather than explaining how it does.

Output


Hi its p
Hi its q
Hi its r

Thus, we can see that abstraction can be achieved with the help of abstract classes as well as the interface. With the abstraction class, you can create transparency of 0 to 100 per cent while the interface allows you to achieve 100% abstraction. 
Different Types of Inheritance in Java With Example
5 Different Types of Inheritance in Java With Example. The Java development services have been progressing for many years and we can definitely see a huge rise in the future. There is plenty of Java uses in your application; among them, inheritance is a process in which one object gains all the properties and behaviours of a parent object. Types of Inheritance in Java and the Applications of Inheritance in Java Language. 

We Are Going to Discuss Each and Single Detail with the Help of Syntax, Examples, and Images. Inheritance is considered a crucial part of the Object Oriented programming system. You may think about the application of inheritance in Java. Not to worry as we have got a quick summary of the Java inheritance and how to utilize it in your next project.

Things to Remember: Parent or Superclass for all classes are Object Class that is present in java.lang Package.

The main idea behind Types of Inheritance in Java is that you are allowed to create new classes upon the existing classes. By reusing the methods or fields of the parent class, you can inherit from an existing class and also add new methods or fields in your current class. Generally, the inheritance is known as a parent-child relationship that is represented by the IS-A relationship. There are duly two main reasons why you should opt for Types of Inheritance in Java.


Let us look at basic Java Inheritance.

Syntax:

class Subclass-name extends Superclass-name
{
// methods and fields
}

The Java inheritance also consists of a few terminologies which are mostly used while inheriting the classes. Go through these given terms to understand the concept in a better way.

Class -A group of objects that has common properties is defined as a class. Objects are created from a class.

Child Class -A class which inherits the other class. Also known as a subclass, derived class or extended class.

Parent Class -A class from where a subclass inherits the features. Also known as the superclass or base class.

Reusability -It is a mechanism that allows you to reuse the fields and methods of the existing class when creating a new class. You can also use the same fields and methods that are already defined in the previous class.

Different Types of Inheritance in Java With Example

Now, it's time to dive into different types of Java inheritance along with its applications.

5 Different Types of Inheritance in Java With Example


  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance
Note: Java does not support Hybrid and Multiple Inheritance at the class level because of the ambiguity problem.

1. Single Inheritance in Java


It is one of the easy inheritances to understand and implement as well. When a parent class gets extended by a child class then we call it a single inheritance. It is direct to direct connection defined as in the figure. Single Inheritance is one of the Different Types of Inheritance in Java.

Single Inheritance in Java

Example of Single Inheritance in Java



Class A {

	public void method A() {
		System.out.println("Parent class");
	}
}


Class B extends A {

	public void method B() {
		System.out.println("Child class");
	}

	public static void main(String args[]) {
		B obj = new B();
		obj.methodA(); //calling super class
		obj.methodB(); //calling sub class
	}
}

2. Multilevel Inheritance in Java


A mechanism in an object-oriented language where you can inherit from a derived class thereby making this derived class the parent class for the new class is defined as a multilevel inheritance. Hence, in Java, a class cannot directly access the members of the grandparents. Multilevel Types of Inheritance in Java.

Multilevel Inheritance in Java

Example of Multilevel Inheritance in Java


Class X {

	public void method X() {
		System.out.println("Class X method");
	}
}
Class Y extends X {

	public void method Y() {
		System.out.println("class Y method");
	}
}
Class Z extends Y {

	public void methodZ() {
		System.out.println("class Z method");
	}

	public static void main(String args[]) {
		Z obj = new Z();
		obj.methodX(); //calling grand parent class
		obj.methodY(); //calling base class
		obj.methodZ(); //calling local method
	}
}

3. Hierarchical Inheritance in Java


When one class is inherited by many subclasses, it is said to have a hierarchical inheritance. A better pictorial representation helps you to understand better. Hierarchical Types of Inheritance in Java.

Hierarchical Inheritance in Java

Example of Hierarchical Inheritance in Java


class A {

	public void method A() {
		System.out.println("method of Class A");
	}
}
class B extends A {

	public void method B() {
		System.out.println("method of Class B");
	}
}
class C extends A {

	public void method C() {
		System.out.println("method of Class C");
	}
}
class D extends A {

	public void method D() {
		System.out.println("method of Class D");
	}
}
class JavaExample {

	public static void main(String args[]) {
		B obj1 = new B();
		C obj2 = new C();
		D obj3 = new D();

		obj1.methodA();
		obj2.methodA();
		obj3.methodA();
	}
}

4. Hybrid Inheritance in Java


Hybrid inheritance is a combination of single and multiple inheritances as depicted in the below-given figure. It can be achieved in the same way as the multiple inheritances in java. With the help of interfaces, you can have multiple as well as hybrid Different Types of Inheritance in Java With Example.

Hybrid Inheritance in Java

Example of Hybrid Inheritance in Java


class C {

	public void display() {
		System.out.println("C");
	}
}

class A extends C {

	public void display() {
		System.out.println("A");
	}
}

class B extends C {

	public void display() {
		System.out.println("B");
	}

}

class D extends A {

	public void display() {
		System.out.println("D");
	}

	public static void main(String args[]) {

		D obj = new D();
		obj.display();
	}
}

5. Multiple Inheritance in Java


When a child class inherits or extends more than one parent class, it is said to have multiple inheritances. There is an issue when it comes to multiple Inheritance in Java as the derived class will have to manage the dependency on two base classes. It is mandatory to know that multiple inheritances are used very rarely in software projects as they can often lead to problems in the hierarchy. 

This might end up in unwanted complexity when further you want to extend the class. All of the new object-oriented languages like Small Talk, Java, and C# do not support multiple inheritances while it is fully supported in C++.

Multiple Inheritance Example

Author Bio:


Ava Barker working as a Technology Consultant at Tatvasoft UK which provides java development services in London. Coming from a technology background she likes to share her insights about development, design and more. She has also published her author bylines in many different publications online.