blog banner
 

 

1. Describe Interface in JAVA ?

  • Interface in java is core part of Java programming language and one of the way to achieve abstraction in Java along with abstract class.
  • The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.
  • An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
  • Along with abstract methods an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
  • Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.


2. What are the similarities between a class and interfaces ?

  1. An interface can contain any number of methods.
  2. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  3. The byte code of an interface appears in a .class file.
  4. Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

 

3. What are the conditions of using Interfaces ? 
   What are the differences Between a class and interfaces ?

  1. You cannot instantiate an interface.
  2. An interface does not contain any constructors.
  3. All of the methods in an interface are abstract.
  4. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  5. An interface is not extended by a class; it is implemented by a class.
  6. An interface can extend multiple interfaces.
  7. You can’t mark an interface as final.
  8. Interface variables must be static.
  9. An Interface cannot extend anything but another interfaces.

 

4. How to declare and define an interface ?

The interface keyword is used to declare an interface. An interface is written in a file with a .java extension, with the name of the interface matching the name of the file

interface printable{

void print();

}

 

5. How does a class implements an Interface ?

using the implements keyworkd.

class A6 implements printable{

public void print(){System.out.println("Hello");}


public static void main(String args[]){

A6 obj = new A6();
obj.print();

}

}

 

6. Can an Interface inherits/extend another Interface ?

Yes, an Interface can inherit or extend another Interface just like inheritance with classes.

A class implements interface but one interface extends another interface .

interface Printable{

void print();

}


interface Showable extends Printable{

void show();

}


class Testinterface2 implements Showable{

public void print(){System.out.println("Hello");}


public void show(){System.out.println("Welcome");}

 

public static void main(String args[]){

Testinterface2 obj = new Testinterface2();
obj.print();
obj.show();

}

}


Output : 
Hello
Welcome

 

7. Is multiple Inheritance possible with Interface inheritance

A Java class can only extend one parent class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.

The extends keyword is used once, and the parent interfaces are declared in a comma-separated list

For example, if the Hockey interface extended both Sports and Event, it would be declared as:

public interface Hockey extends Sports, Event
 

8. What is marker or tagged interface?

An interface that have no member is known as marker or tagged interface. For example: Serializable, Cloneable, Remote etc. The java.io.Serializable interface and Cloneable are typical marker interfaces. These do not contain any methods. They are used to provide some essential information to the JVM so that JVM may perform some useful operation. classes must implement Serializable, Cloneable interfaces in order to be serialized and de-serialized.

 

//How Serializable interface is written?
public interface Serializable{
}

 

9. Can we create an object for an interface?

Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.

 

10. Do interfaces have member variables?

Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.

 

11. What modifiers are allowed for methods in an Interface?

Only public and abstract modifiers are allowed for methods in interfaces.

 

12. When should I use abstract classes and when should I use interfaces?

Use Interfaces when

    • You see that something in your design will change frequently.
    • If various implementations only share method signatures then it is better to use Interfaces.
    • you need some classes to use some methods which you don't want to be included in the class, then you go for the interface, which makes it easy to just implement and make use of the methods defined in the interface.

Use Abstract Class when…

    • If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
    • When you want to provide a generalized form of abstraction and leave the implementation task with the inheriting subclass.
    • Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies.

 

13. Can there be an abstract class with no abstract methods in it?

Yes, there can be an abstract class without abstract methods.

 

14. Can there be an abstract class with no abstract methods in it?

Yes, there can be an abstract class without abstract methods.

 

15. What is constructor in JAVA ?

Constructor in java is a special type of method that is used to initialize the object. Every class has a constructor,if we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class. A constructor does not have any return type.

A constructor has same name as the class in which it resides. Constructor in Java can not be abstract, static, final or synchronized. These modifiers are not allowed for constructor.

Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules for creating java constructor

  • Constructor name must be same as its class name
  • Constructor must have no explicit return type

Types of java constructors

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor

 

16. Explain Default Constructor in JAVA?

A constructor that have no parameter is known as default constructor.

Syntax of default constructor:

<class_name>(){}

Example of default constructor

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.


class Bike1{

Bike1(){

System.out.println("Bike is created");

}


public static void main(String args[]){

Bike1 b=new Bike1();

}

}

OutPut: 

Bike is created

 

17. What is the purpose of default constructor?

Default constructor provides the default values to the object like 0, null etc. depending on the type.

 

18. Exaplain Parameterized Constructor ?

A constructor that have parameters is known as parameterized constructor.

Example of parameterized constructor

In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.

class Student{

int id;
String name;

Student4(int i,String n){

id = i;
name = n;

}


void display(){System.out.println(id+" "+name);}

 

public static void main(String args[]){

Student4 s1 = new Student(111,"Karan");
s1.display();

}

}

OutPut :

111 Karan

 

19. Why use parameterized constructor?

Parameterized constructor is used to provide different values to the distinct objects.

 

20. Does constructors return any value?

Yes, constructors return current instant of a class. But yet constructor signature cannot have any return type.

 

21 Can constructor be inherited?

No, constructor cannot be inherited, though a derived class can call the base class constructor.

 

22. How are this() and super() used with constructors?

  • Constructors use this to refer to another constructor in the same class with a different parameter list.
  • Constructors use super to invoke the superclass's constructor. If a constructor uses super, it must use it in the first line; otherwise, the compiler will complain.

 

23. Explain Constructor overloading ?

A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded (comes in multiple versions). This is what constructor overloading means.

Overloaded constructors are differentiated on the basis of their type of parameters or number of parameters. Constructor overloading is not much different than method overloading. In case of method overloading you have multiple methods with same name but different signature, whereas in Constructor overloading you have multiple constructor with different signature but only difference is that Constructor doesn't have return type in Java.

class Language {

String name;

 

Language() {

System.out.println("Constructor method called.");

}

 

Language(String t) {

name = t;

}

 

void setName(String t) {
name = t;
}

 

void getName() {
System.out.println("Language name: " + name);
}

 

public static void main(String[] args) {

Language cpp = new Language();
Language java = new Language("Java");

cpp.setName("C++");

java.getName();
cpp.getName();

}

}

OutPut:

Constructor method called.

Language name: JAVA

Language name: C++

 

24. Difference between constructors and methods ?

The important difference between constructors and methods are:

    1. Constructors create and initialize objects that don't exist yet, while methods perform operations on objects that already exist.
    2. Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new.
    3. Constructors must be named with the same name as the class name. 
    4. Constructors can not return any value, not even void ( Note : constructors return current instant of a class implicitly, this is automatically done, But yet constructor signature cannot have any return type). Method must return a value even if it is void. Correct way to say this difference is :  Constructor can not have a return type in its declaration and signature whereas method must have a return type even if it is void in its declaration and signature
    5. Constructors can not be overriden as methods.

 

25. Explain copy constructor ?

Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.

A copy constructor is a constructor that takes only one argument which is of the type as the class in which the copy constructor is implemented. For example, let us assume a class namely Car and it has a constructor called copy constructor which expects only one argument of type Car.

Copy constructors are widely used for creating a duplicates of objects known as cloned objects. Duplicate object in the sense the object will have the same characteristics of the original object from which duplicate object is created. But we have to ensure that both original and duplicate objects refer to different memory locations.

Copy constructors are implemented for deep cloning and shallow cloning. Both these cloning techniques can be achieved by overriding the clone method in java. Shallow cloning is the default cloning implementation in java and we just need to override the clone method. But deep cloning has to be implemented as per our need and logic. Explaining about cloning is beyond the scope of this post. So let’s come back

There are many ways to copy the values of one object into another in java. They are:

  1. By constructor
  2. By assigning the values of one object into another
  3. By clone() method of Object class

In this example, we are going to copy the values of one object into another using java constructor.

class Student{

int id;
String name;


Student(int i,String n){

id = i;
name = n;

}

 

Student(Student s){

id = s.id;
name =s.name;

}


void display(){System.out.println(id+" "+name);}

 

public static void main(String args[]){

Student s1 = new Student(111,"Karan");
Student s2 = new Student(s1);
s1.display();
s2.display();

}

}
 

26. What is Shallow Copy? Or What is Shallow cloning in JAVA?

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied. Object class has a method clone() which does shallow cloning.

 

Whenever we use default implementation of clone method we get shallow copy of object means it create new instance and copy all the field of object to that new instance and return it as object type we need to explicitly cast it back to our original object. This is shallow copy of the object. clone() method of the object class support shallow copy of the object. If the object contains primitive as well as non primitive or reference type variable In shallow copy, the cloned object also refers to the same object to which the original object refers as only the object references gets copied and not the referred objects themselves. That's why the name shallow copy or shallow cloning in Java. If only primitive type fields or Immutable objects are there then there is no difference between shallow and deep copy in Java.

It can be seen that no new objects are created for obj1 and it is referring to the same old contained objects. If either of the containedObj contain any other object no new reference is created.
 

27. What is Deep Copy ? Or What is Deep cloning in JAVA ?

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.

 

Whenever we need own meaning of copy not to use default implementation we call it as deep copy, whenever we need deep copy of the object we need to implement according to our need. So for deep copy we need to ensure all the member class also implement the Cloneable interface and override the clone() method of the object class. After that we override the clone() method in all those classes even in the classes where we have only primitive type members otherwise we would not be able to call the protected clone() method of Object class on the instances of those classes inside some other class. It’s typical restriction of the protected access.

Admin Team


0 Respond



Written
Exam Questions
BLOG Posts
For study materials
Job Walk-In
Updates across India
Interview round
Questions
Interview or procedure
Experiences
Files
For study materials
Please LIKE our page to Get regular JOB WALK-IN UPDATES across India and STUDY MATERIALS on facebook news feed.