Java Interview Questions

Advertisements

Prev Tutorial Next Tutorial

oops Concept Interview Questions

Inheritance

What is Inheritance ?

The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object-oriented programming.

What are advantage of Inheritance ?

If we develop any application using concept of Inheritance than that application have following advantages,

  • Application development time is less.
  • Application take less memory.
  • Application execution time is less.
  • Application performance is enhance (improved).
  • Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost.

Why use Inheritance ?

  • For Method Overriding (used for Runtime Polymorphism).
  • It's main uses are to enable polymorphism and to be able to reuse code for different classes by putting it in a common super class
  • For code Re-usability

You can decrease scope of access modifier in inheritance ?

No, In Inheritance the scope of access modifier increasing is allow but decreasing is not allow. Suppose in parent class method access modifier is default then it's present in child class with default or public or protected access modifier but not private(it decreased scope).

How many type of inheritance are allow in java ?

In Java following inheritance are allow;

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

Which inheritance is not supported by Java ?

Multiple Inheritance, are not supported by java.

Why multiple inheritance is not supported in java?

Due to ambiguity problem java does not support multiple inheritance at class level.

How to achieve multiple inheritance in java ?

Java can not support multiple inheritance at class level but you can achieve multiple inheritance in java by using Interface concept.

Difference between import and package ?

  • Package keyword is always used for creating the undefined package and placing common classes and interfaces.
  • import is a keyword which is used for referring or using the classes and interfaces of a specific package.

Give real life example of Inheritance ?

The real life example of inheritance is child and parents, all the properties of father are inherited by his son.

What is Difference between Class and Object ?

ClassObject
1Class is a container which collection of variables and methods.object is a instance of class
2No memory is allocated at the time of declarationSufficient memory space will be allocated for all the variables of class at the time of declaration.
3One class definition should exist only once in the program.For one class multiple objects can be created.

Give real life example of class and object.

In real world many examples of object and class like dog, cat, and cow are belong to animal's class. Each object has state and behaviors. For example a dog has state:- color, name, height, age as well as behaviors:- barking, eating, and sleeping.

Interface

What is interface ?

Interface is similar to class which is collection of public static final variables (constants) and abstract methods.

Why use interface in java ?

In java interface are used for achieve multiple inheriatance.

Can an Interface extend another Interface?

Yes an Interface can inherit another Interface.

How interface is similar to class ?

Whenever we compile any Interface program it generate .class file. That means the bytecode of an interface appears in a .class file.

How interface is different from class ?

  • we cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All methods in an interface are abstract.
  • An interface cannot contain instance fields. Interface only contains public static final variables.
  • An interface is can not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces. That means interface support multiple inheritance

Why interface have no constructor ?

Because, constructor are used for eliminate the default values by user defined values, but in case of interface all the data members are public static final that means all are constant so no need to eliminate these values.

Other reason because constructor is like a method and it is concrete method and interface does not have concrete method it have only abstract methods that's why interface have no constructor.

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. They are used to provide some essential information to the JVM so that JVM may perform some useful operation.

Why Method Overloading is not possible by changing the return type of method?

In java, method overloading is not possible by changing the return type of the method because there may occur ambiguity.

Why Interface have no Constructor ?

Because, constructor are used for eliminate the default values by user defined values, but in case of interface all the data members are public static final that means all are constant so no need to eliminate these values.

Other reason because constructor is like a method and it is concrete method and interface does not have concrete method it have only abstract methods that's why interface have no constructor.

Why not use abstract and final modifier together ?

In java, abstract and final both modifiers are not allowed for a class at a time the reason is abstract and final are opposite keywords. If a class is an abstract, then that class must be extended (inherited). If a class is final then that class can not be extended. So both keyword can not be use at a time

Why use Abstract class ?

Abstract class are used for fulfill common requirement. If we use concrete classes for fulfill common requirements than such application will get the following limitations.

  • Application will take more amount of memory space (main memory).
  • Application execution time is more.
  • Application performance is decreased.

To overcome above limitation you can use abstract class.

Difference between abstract class and concrete class ?

Concrete classAbstract class
Concrete class are used for specific requirementAbstract class are used for fulfill common requirement.
Object of concrete class can be create directly.Object of abstract class can not be create directly (can create indirectly).
Concrete class containing fully defined methods or implemented method.Abstract class have both undefined method and defined method.

Difference between Abstraction and Encapsulation ?

Encapsulation is not provides fully security because we can access private member of the class using reflation API, but in case of Abstraction we can't access static, abstract data member of class.

Difference between Abstract and Interface ?

AbstractInterface
1It is collection of abstract method and concrete methods.It is collection of abstract method.
2There properties can be reused commonly in a specific application.There properties commonly usable in any application of java environment.
3It does not support multiple inheritance.It support multiple inheritance.
4Abstract class is preceded by abstract keyword.It is preceded by Interface keyword.
5Which may contain either variable or constants.Which should contains only constants.
6The default access specifier of abstract class methods are default.There default access specifier of interface method are public.
7These class properties can be reused in other class using extend keyword.These properties can be reused in any other class using implements keyword.
8Inside abstract class we can take constructor.Inside interface we can not take any constructor.
9For the abstract class there is no restriction like initialization of variable at the time of variable declaration.For the interface it should be compulsory to initialization of variable at the time of variable declaration.
10Inside interface we can take instance and static block.Inside interface we can not take instance and static block.
11There are no any restriction for abstract class variable.For the interface variable can not declare variable as private, protected, transient, volatile.
12There are no any restriction for abstract class method modifier that means we can use any modifiers.For the interface method can not declare method as strictfp, protected, static, native, private, final, synchronized.

Give Real life example of Abstraction

Abstraction shows only important things to the user and hides the internal details for example when we ride a bike, we only know about how to ride bike but can not know about how it work ? and also we do not know internal functionality of bike.

Give real life example of Encapsulation ?

The common example of encapsulation is capsule. In capsule all medicine are encapsulated in side capsule.

Difference between Encapsulation and Abstraction

Encapsulation is not provides fully security because we can access private member of the class using reflation API, but in case of Abstraction we can't access static, abstract data member of class.

How to achieve Encapsulation in java

In java you can achieve Encapsulation by using class concept.

polymorphism

Give Real life example of polymorphism

Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.

Difference between Overloading and Overriding ?

Overloading Overriding
1Whenever same method or Constructor is existing multiple times within a class either with different number of parameter or with different type of parameter or with different order of parameter is known as Overloading.Whenever same method name is existing multiple time in both base and derived class with same number of parameter or same type of parameter or same order of parameters is known as Overriding.
2Arguments of method must be different at least arguments.Argument of method must be same including order.
3Method signature must be different.Method signature must be same.
4Private, static and final methods can be overloaded.Private, static and final methods can not be overrided.
5Access modifiers point of view no restriction.Access modifiers point of view not reduced scope of Access modifiers but increased.
6Also known as compile time polymorphism or static polymorphism or early binding.Also known as run time polymorphism or dynamic polymorphism or late binding.
7Overloading can be exhibited both are method and constructor level.Overriding can be exhibited only at method leve.
8The scope of overloading is within the class.The scope of Overriding is base class and derived class.
9Overloading can be done at both static and non-static methods.Overriding can be done only at non-static method.
10For overloading methods return type may or may not be same.For overriding method return type should be same.

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements