Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Showing posts with label JAVA INTERVIEW QUESTIONS & ANSWERS. Show all posts
Showing posts with label JAVA INTERVIEW QUESTIONS & ANSWERS. Show all posts

Thursday, 12 October 2017

Interview Questions on Super Keyword in Java

Java Super Keyword Interview Questions

Interview Questions on Super Keyword in java

In this article, We are going to discuss the most important topic of core java interview is super keyword in java. Here we will focus on some important interview questions on super keyword in java which is frequently asked in java interviews.

Let's start basic super keyword java interview questions for 1 year experience and fresher both.


(1) What is super keyword in java?

Super keyword in java is a reference variable which is refers immediate parent or super class object. In other words, Super keyword is used to access the members of parent class.

Super keyword point to immediate parent class object. It works with object only of parent class.


(2) Write some points about super keyword in java?

There are some use of super keyword in java.
  • Super keyword can be used to refer parent class instance variables.
  • Super keyword can be used to invoke parent class method.
  • Super keyword can be used to invoke parent class constructor.

(3) Can we access parent class variables in child class by using super keyword?

Yes, We can access parent class variable in child class by using java super keyword.

For example:

class Parent
{
int a = 20;
}
class Child extends Parent
{
int a = 30;
void show()
{
System.out.println(a);//print child class value of a
System.out.println(super.a);//print parent class value of a
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}

Output: 30
             20

You can see in the above example, There are same data member in both parent and child class. If you will not use super keyword in child class, it will print only child class value by default i.e 30 because priority goes to child. If you want to print both child and parent object values you have to use super keyword in sub class.


(4) Can we call parent class method in sub class by using super keyword.

Yes, We can access parent class method in child class by using super keyword.

For example:

class Parent
{
void display()
{
System.out.println("parent");
}
}
class Child extends Parent
{
void display()
{
System.out.println("child");
super.display();
}
public static void main(String args[])
{
Child c = new Child();
c.display();
}
}

Output: child
             parent


(5) Can we call parent class constructor in sub class constructor i.e constructor chaining by using super keyword?

Yes, We can call parent class constructor in sub class constructor by using super keyword but super keyword must be the first statement in sub class constructor.

Syntax:

class ParentDemo
{
ParentDemo()
{}
}
class Child extends ParentDemo
{
Child()
{
super();//must be first statement i.e in first line
//statement;
//statement;
}
psvm()
{}
}


(6) Can we use both "this" and "super" in constructor?

No, It is not possible in java we cannot use both this and super keyword together in java constructor because this and super should be the first statement in any java constructor.


(7) What is difference between this() and super()?

There are some difference between java this() constructor and super() constructor.

Java this()
  • It is used to access current class constructor.
  • It can be used inside another constructor of same class.
  • It can be used to remove ambiguity error when we have data members and local are same name.
For example: Remove ambiguity error

class Demo
{
int a;
int b;
Demo(int a, int b)
{
this.a = a;
this.b = b;
}
}
}

Java super()
  • It is used to access parent class constructor from sub class.
  • It can be used to only inside child class constructor.
  • It doesn't remove any ambiguity error from programs.

(8) Can we use super keyword in static method of a sub class for calling parent class method?

No, We cannot use super keyword in static methods because it belongs to the immediate parent object and static belongs to the class level.

Read More:

Interview Questions on Static Keyword in Java.
Interview Questions on Final Keyword in Java.
Interview Questions on Multithreading in Java.
Struts Framework Interview Questions.
Interview Questions on Java String
Some Basic Programs on Java Keywords.


Above all the super keyword interview questions and answers are quite useful for java interviews.

Share:

Monday, 2 October 2017

Interview Questions on Method Overloading in Java

Java Method Overloading Interview Questions

Interview Questions on Method Overloading in Java

Here we will discuss some interview questions on method overloading in java. In this post, we will see most frequently asked method overloading questions in core java interviews.

Let's start with method overloading interview questions and answers.


(1) What is method overloading in java?

If we have more than one method or multiple methods in the same class with same name but different number of arguments is known as method overloading in java.

Syntax:

class Demo
{
void show(int a, int b)//with two arguments
{}
void show(int a, int b, int c)//with three arguments
{}
psvm(){}}


(2) Why we use method overloading?

Java method overloading increases the readability of a program.

(3) How many ways we can achieve method overloading?

There are two ways we can achieve method overloading in java which is given below.

  1. By changing number of arguments.
  2. By changing the data types.

(4) Can we overload main() method in java?

Yes, We can easily overload main() method in java.




(5) What are the others name for java method overloading?

There are many other names of method overloading and these are

  • compile-time polymorphism.
  • static polymorphism.
  • static binding

Method overloading is also know as compile-time polymorphism, static polymorphism, and static binding.



(6) Can overloaded method be override?

Yes, we can easily override a overloaded method in sub-clas which is overloaded in super class.


(7) Can we overload a static method?

Yes, we can overload static methods but we cannot override static method.


(8) Can we overload constructor in java?

Yes, we can overload constructor in java.

(9) Can we declare overloaded method as final?

Yes, we can declare overloaded method as final.

(10) Can we make overloaded method synchornized?

Yes, It is possible overloaded method can be synchronized.


(11) Can we declare one overloaded method as static and another one as non-static?

Yes, we can define one overloaded method as static and another non-static in java.

class OverloadS
{
static void show(int i, int j)
{
System.out.println(i+j);
}
void show(int i, int j, int k)
{
System.out.println(i+j+k);
}
public static void main(String args[])
{
OverloadS os = new OverloadS();
os.show(10,20);
os.show(10,20,30);
}
}

Output: 30
              60

(12) Can we achieve method overloading by changing the return -type?

No, we cannot achieve method overloading by changing the return type.

(13) Difference Between method overloading and method overriding?

There are many differences between method overloading and method overriding in java and some of them are given below.

Method Overloading
  • Whenever we have more than one method with the same name but different number of arguments or different data types in the same class is known as method overloading.
  • Method overloading are happens at compile-time.
  • Method overloading are known as compile-time or static polymorphism and early binding also.

Method Overriding
  • Whenever we have super class method and sub class method are same name and same number of arguments is known as method overriding.
  • Method overriding are happens at run-time.
  • Method overriding are known as run-time or dynamic polymorphism and late binding also.

Read More :


Above all the interview questions on method overloading in java are quite useful.

Share:

Java Interview Questions on Polymorphism

Java Polymorphism Interview Questions

java polymorphism interview questions

Here we will see some java interview questions on polymorphism which is mostly asked in any java interviews.

Polymorphism in java interview questions and answers for experienced and beginners both.


(1) What is polymorphism in java?

In java, Polymorphism means "one name many forms". In other words, you can say whenever an object producing different-different behavior in different-different circumstances is called polymorphism in java.


(2) Give real-life examples of polymorphism

Let's understand real-time example of polymorphism.

1) Related to person
  • Suppose you are in the classroom that time you will behave like a student.
  • Suppose when you at home you behave like son or daughter.
  • Suppose you are in the market at that time you behave like a customer.

2) Related to product


Let's take an example of an Air conditioner which produces hot air in winter and cold air in summer.

So there is one name but it producing different - different output according to the situation.


(3) How many types are of polymorphism in java?

There are two types of polymorphism in java and these are given below.
  • Compile-Time polymorphism or Static polymorphism.
  • Run-time polymorphism or Dynamic polymorphism.

(4) Can we achieve polymorphism through data member?

No, Polymorphism is always achieved via behavior of an object only properties of an object do not play any role in case of polymorphism.


(5) What are compile-time and run-time polymorphism?

Compile-Time Polymorphism

The best example of compile-time or static polymorphism is the method overloading. Whenever an object is bound with their functionality at compile time is known as compile-time or static polymorphism in java.

Run-Time Polymorphism

The best example of run-time or dynamic polymorphism is the method overriding. Whenever an object is bound with their functionality at run-time is know as run-time or dynamic polymorphism in java.


(6) What is method overloading?

Whenever we have more than one same name method but a different number of arguments or different data types in the same class is known as method overloading in java. Method overloading the example of compile-time polymorphism.

For example:

class Addition
{
void add(int a, int b)//with 2 arguments
{
System.out.println(a+b);
}
void add(int a, int b, int c)//change no of arguments i.e 3
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Addition a = new Addition();
a.add(10,20);
a.add(20,5,6);
}
}

Output: 30
             31


(7) What is method overriding?

In java, whenever we have same name method in parent and child class with the same number of arguments and same data types is known as method overriding in java. Method overriding is the example of dynamic polymorphism.

For example:

class Parent
{
void show()
{
System.out.println("Parent");
}
}
class Child extends Parent
{
void show()
{
System.out.println("Child");
}
public static void main(String args[])
{
Parent p =
new Child();
p.show();
}
}

Output: Child


(8) How to achieve static polymorphism?

Compile - time polymorphism is also known as static polymorphism. We can achieve static polymorphism through method overloading in java.


(9) How to achieve dynamic polymorphism?

Run-time polymorphism is also known as dynamic polymorphism. We can achieve dynamic polymorphism through method overriding.

(10) Difference between method overloading and method overriding?


(11) What is up-casting in java?

Whenever we put the reference id of a child class object into the parent class reference variable is known as upcasting in java.

For example:

Parent p = new Child();//upcasting declaration


(12) What is static binding?

Static binding is also known as compile - time polymorphism. Method overloading is the example of static binding.

In static binding, The type of object is determined at compile-time.

Static binding is also known as early binding.

(13) What is dynamic binding?

Dynamic binding is also known as run-time polymorphism. Method overriding is the example of dynamic binding.

In dynamic binding, The type of object is determined at run-time.

Dynamic binding is also known as late binding.


(14) Can we achieve method overloading by changing the return type?

No, We cannot achieve method overloading through return type in java.


(15) What is runtime polymorphism?

Polymorphism is a powerful concept of oops(Object Oriented Programming System). Above all the polymorphism interview questions and answers are quite useful for java competitions. 

Share:

Tuesday, 26 September 2017

Interview Questions on Abstraction in Java

Java Abstraction Interview Questions

Abstraction Interview Questions in java

Now, Here we are going to discuss the most important core java interview questions and answers which is "interview questions on abstraction in java".

So let's start with abstraction interview questions.


(1) What is abstraction in java?

In java, Abstraction means show functionality and hide complexity or internal details or hide implementation details to the user is know as abstraction in java.

For example:

The best example in the world of abstraction is ATM machine where we can withdraw or transfer money easily but how it happens? we don't know. We don't know internal details or implementation details.


(2) How to achieve abstraction in java?

There are two ways in java we can achieve abstraction.
  • By using abstract class(0 to 100%).
  • By using interface(100%).


(3) What is abstract class in java?

When we declared any class with "abstract" keyword is known as abstract class. In abstract class we can keep abstract method(without body) and non-abstract method(with body).

For example:

abstract class Demo
{
abstract void show();//abstract method
void show(){}//non-abstract method
}

(4) Can we create instance of abstract class?

No, We cannot create an instance of an abstract class.

(5) Can we define abstract class without abstract method?

Yes, We can define abstract class without abstract method.

(6) Can we declare abstract method in non-abstract class?

No, We can't declare abstract method in non-abstract class.

For example:

class Demo
{
abstract void show();
}

Above example will throw compile time error.


(7) What is interface in  java? 

An interface in java is a blueprint of a class that have static constants and abstract method by default.

(8) Why we use interface in java?

In java we use interface so that we can achieve fully abstraction because through abstract class we can't achieve full abstraction.


(9) Can we create instance of interface?

No, We cannot create object of both an interface and abstract class.

(10) Can we declare abstract method as static?

No, We can't use static keyword with abstract method.

(11) Can we declare abstract method as final?

No, We cannot use final keyword with abstract class.


(12) Can we declare abstract method as private?

No, We cannot declare abstract method as private.

(13) Can we use public, protected and default modifiers with abstract method?

Yes, We can use public, protected and default modifiers with abstract method.

(14) Can we declare local inner class as abstract?

Yes, We can declare local inner class as abstract.

(15) Method in interface are by default public and abstract. true or false?

Yes, It is true.

(16) Data member in interface are by default public, static, and final. true of false?

Yes, It is true.

(17) Can abstract class implements interface in java?

Yes, Abstract class can implement interface.

(18) Can we use abstract keyword with constructor?

No, We can't use abstract keyword with constructor.

(19) Abstract classes can be nested. true or false?

Yes, Abstract classes can be nested.


(20) Can abstract class have constructor in java?

Yes, We can declare constructor in abstract class.

(21) Difference between abstraction and encapsulation in java?


(22) What is the difference between abstract class and interface?

You can click on this link to see what is the difference between abstract class and interface in java with example 


(23) Can abstract method declaration include throws clause?

Yes.


(24) What will happen if we do not override all the abstract methods in sub-class?

It will throw compile-time error. We have to override all the abstract method in sub-class. If you do not want to override all the abstract method in sub-class then you have to make your sub-class as abstract class.

Read More:

Java Interview Questions on Interface.
Java OOPS Interview Questions.
Java Inheritance Interview Questions.
Java Super Keyword Interview Questions.
Java Abstract Class with Example.
Java Interface with Example.

This java abstraction interview questions will help you in java interviews. 

Share:

Wednesday, 20 September 2017

Interview Questions and Answers on Inheritance in Java

Java Inheritance Interview Questions

Java Inheritance Interview Questions and Answers

Now, Here we are going see some most important interview questions and answers on inheritance in java. This is the most important topic from the core java interview point of view.

There are many oops concepts interview questions are asked in core java interviews and Java inheritance concept is one of them.


(1) What is inheritance in java?

Inheritance is the oops concept and in java inheritance means re-usability. By using this inheritance concept a child class can inherit its parent class so that the child class can reuse all the field and methods of its parent class.


(2) What are the types of inheritance?

There are 5 types of inheritance. Suppose there is a class A, B, C, and D.

1) Single level inheritance.

B extends A

2) Multilevel inheritance.

C extends B and B extends A

3) Multiple inheritance.

C extends B and A

4) Hierarchical inheritance.

C  and B extends A  

5) Hybrid inheritance.

D extends C and B and C and B extends A


(3) Use of inheritance in java?

There are many reasons for using inheritance in java and these are given below.
  1. To achieve dynamic binding or method overriding.
  2. For code re-usability.
  3. To save time.

(4) Does java support multiple inheritance?

No, Java doesn't support multiple inheritance because of complexity and ambiguity in a program. Java does not support multiple inheritance through the class but it is possible through the interface.

(5) What is the syntax of inheritance?

There is the syntax of inheritance.

//Single levele inheritance
class A
{
//data member;
//member function
}
class B extends A
{
//data member;
//member function
}
}


(6) What is IS-A relationship in inheritance?

Java inheritance represents is-a relationship and parent-child relationship is also know as is-a relationship.


(7) What is aggregation in java?

If a class have an entity reference, it is known as aggregation and aggregation represents Has-a relationship.

For example 

class Student
{
String name;
Address  address;//Adderess is a class
}

Here Address is a class which may contain other information like city, country, etc.


(8) By default, all the classes extend which class in java?

In java, All the classes extend Object class by default because Object class is the super class in java.


(9) How inheritance can be implemented in java?

By two ways we can implement inheritance.

  • Using extends keyword.
  • Using implements keyword.

(10) How do you implement multiple inheritance in java?

We can implement multiple inheritance in java by using interface concept. A class can implement multiple interface at the same time but a class can't extend more than one class at a time.

For example:

interface My
{
//method
}
interface My1
{
//method
}
class Test implements My,My1
{}


(11) Can a class extend itself?

No, A class can't extend itself.


(12) Are interfaces extend Object class by default?

As we know that all the classes extend Object class by default but the interface does not extend Object class by default.


(13) Can you inherit a final class in java?

No, We can't extend the final class.


(14) Can you inherit final method of super class into a sub-class?

Yes, we can inherit final method of a super class into a sub-class.


(13) Can you inherit private members of super class into a sub-class?

No, we cannot inherit private member of a super class into a sub-class.


(14) Can we inherit constructor in sub-class?

No, we cannot inherit constructor in sub-class.


(15) What happens if super class and sub class having same field name?

Super class field will be hidden in the sub class but you can access hidden field of a super class by using 'super' keyword in the sub-class.


(16) Can you inherit static member into a sub-class.

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Link

Translate