Java Interview Prepration
1- What is Abstract Class in Java?
In Java, abstract class is declared with the abstract keyword. It may have both abstract and non-abstract methods(methods with bodies). An abstract is a Java modifier applicable for classes and methods in Java but not for Variables. In this article, we will learn the use of abstract classes in Java.
Java abstract class is a class that can not be initiated by itself, it needs to be subclassed by another class to use its properties. An abstract class is declared using the “abstract” keyword in its class definition.
In Java, the following some important observations about abstract classes are as follows:
- An instance of an abstract class can not be created.
- Constructors are allowed.
- We can have an abstract class without any abstract method.
- There can be a final method in abstract class but any abstract method in class(abstract class) can not be declared as final or in simpler terms final method can not be abstract itself as it will yield an error: “Illegal combination of modifiers: abstract and final”
- We can define static methods in an abstract class
- We can use the abstract keyword for declaring top-level classes (Outer class) as well as inner classes as abstract
- If a class contains at least one abstract method then compulsory should declare a class as abstract
- If the Child class is unable to provide implementation to all abstract methods of the Parent class then we should declare that Child class as abstract so that the next level Child class should provide implementation to the remaining abstract method
// Abstract class
abstract class Xorance {
abstract void printInfo();
}
// Abstraction performed using extends
class Employee extends Xorance {
void printInfo()
{
String name = "aastha";
int age = 21;
float salary = 222.2F;
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
// Base class
class Base {
public static void main(String args[])
{
Xorance s = new Employee();
s.printInfo();
}
}
Output
aastha
21
222.2
What is Interface in java?
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
How to declare an interface?
An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.
Uses of Interfaces in Java
Uses of Interfaces in Java are mentioned below:
- It is used to achieve total abstraction.
- Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances.
- Any class can extend only one class, but can implement multiple interfaces.
- It is also used to achieve loose coupling.
- Interfaces are used to implement abstraction.
// Java program to demonstrate working of
// interface
import java.io.*;
// A simple interface
interface In1 {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class TestClass implements In1 {
// Implementing the capabilities of
// interface.
public void display(){
System.out.println("Xorance");
}
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(t.a);
}
}
Output
Xorance
10
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Access Modifiers
in Java, Access modifiers help to restrict the scope of a class, constructor, variable, method, or data member. It provides security, accessibility, etc to the user depending upon the access modifier used with the element. Let us learn about Java Access Modifiers, their types, and the uses of access modifiers in this article.
Types of Access Modifiers in Java
There are four types of access modifiers available in Java:
- Default – No keyword required
- Private
- Protected
- Public
1. Default Access Modifier
When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default. The data members, classes, or methods that are not declared using any access modifiers i.e. having default access modifiers are accessible only within the same package.
In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of the second package.
PROGRAM-1
// Java program to illustrate default modifier
package p1;
// Class Geek is having Default access modifier
class Xorance
{
void display()
{
System.out.println("Hello World!");
}
}
PROGRAM-2
// Java program to illustrate error while
// using class from different package with
// default modifier
package p2;
import p1.*;
// This class is having default access modifier
class XoranceNew
{
public static void main(String args[])
{
// Accessing class Xorance from package p1
Xorance obj = Xorance Geek();
obj.display();
}
}
Output
Compile time error
2. Private Access Modifier
The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared.
- Any other class of the same package will not be able to access these members.
- Top-level classes or interfaces can not be declared as private because
- private means “only visible within the enclosing class”.
- protected means “only visible within the enclosing class and any subclasses”
// Java program to illustrate error while
// Using class from different package with
// Private Modifier
package p1;
// Class A
class A {
private void display()
{
System.out.println("Xorance Academy");
}
}
// Class B
class B {
public static void main(String args[])
{
A obj = new A();
// Trying to access private method
// of another class
obj.display();
}
}
Output
error: display() has private access in A
obj.display();
3. Protected Access Modifier
The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within the same package or subclasses in different packages
// Java Program to Illustrate
// Protected Modifier
package p1;
// Class A
public class A {
protected void display()
{
System.out.println("Xorance");
}
}
// Java program to illustrate
// protected modifier
package p2;
// importing all classes in package p1
import p1.*;
// Class B is subclass of A
class B extends A {
public static void main(String args[])
{
B obj = new B();
obj.display();
}
}
Output
Xorance
4. Public Access Modifier
The public access modifier is specified using the keyword public.
- The public access modifier has the widest scope among all other access modifiers.
- Classes, methods, or data members that are declared as public are accessible from everywhere in the program. There is no restriction on the scope of public data members.
// Java program to illustrate
// public modifier
package p1;
public class A
{
public void display()
{
System.out.println("Xorance");
}
}
package p2;
import p1.*;
class B {
public static void main(String args[])
{
A obj = new A();
obj.display();
}
}
Output
Xorance
Difference between Stack and Heap Memory in Java
The JVM (Java Virtual Machine) splits the memory into two sections: stack memory and heap memory. Let’s find out some major differences between stack and heap memory in java.
What is Stack Memory in Java?
In java, a stack is a memory that comprises local variables, reference variables and methods. This memory is assembled when a thread is created. Stack memory always prefers the famous LIFO (Last-In-First-Out) order.
What is Heap Memory in Java?
In java, a heap is part of memory that comprises objects and reference variables. Whenever we create objects, it occupies the place in the heap memory; on the other hand, the reference of that object forms in the stack. Like stack, heap does not follow any LIFO order.
Difference between Stack and Heap Memory in Java.
| S.No. | Stack Memory in Java | Heap Memory in Java |
| 1. | Stack follows the LIFO order. | Heap does not follow any order because here, the pattern of memory allocation is not fixed. |
| 2. | It stores the entities that have a short life, like variables and methods. | Heap stores entities like objects. |
| 3. | It is not flexible as we cannot make any changes once the memory allocation is done. | It is flexible as we can make changes here even after the allocation of memory. |
| 4. | It is faster than heap in terms of allocation and deallocation. | It is slower than stack in terms of allocation and deallocation. |
| 5. | The size of stack memory is small. | The size of heap memory is large. |
| 6. | Through the JVM option -Xss, we can improve the size of a stack. | Through the JVM options -Xmx and -Xms, we can change the size of a stack. |
| 7. | It is affordable. | It is affordable as compared to the stack. |
| 8. | The implementation part is easy here. | The implementation part is tough here. |
| 9. | In stack, the memory allotment is continuous. | In heap, the memory allotment is random. |
| 10. | The allocation and deallocation are automatically performed by the compiler. | Here the allocation and deallocation are done manually. |
What is the constructor?
The constructor can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name. The constructor must not have an explicit return type.
class Student3{
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}
Java Copy Constructor
In Java, a copy constructor is a special type of constructor that creates an object using another object of the same Java class. It returns a duplicate copy of an existing object of the class.
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
Difference between abstract class and Interface
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases.
In this article, we will learn abstract class vs interface in Java.
Points | Abstract Class | Interface |
|---|---|---|
Definition | Cannot be instantiated; contains both abstract (without implementation) and concrete methods (with implementation) | Specifies a set of methods a class must implement; methods are abstract by default. |
Implementation Method | Can have both implemented and abstract methods. | Methods are abstract by default; Java 8, can have default and static methods. |
Inheritance | class can inherit from only one abstract class. | A class can implement multiple interfaces. |
Access Modifiers | Methods and properties can have any access modifier (public, protected, private). | Methods and properties are implicitly public. |
Variables | Can have member variables (final, non-final, static, non-static). | Variables are implicitly public, static, and final (constants). |
