Posts

Showing posts from October, 2017

Object Class in Java

Object class is super class of all classes in Java. Every class in Java extends Object class directly or in-directly. There are total 12 methods in Object class. You can call override all non-final, public and protected methods. You can also call all public methods on object of any class(custom or in-built). toString clone equals hashCode notify and notifyAll wait finalize registerNatives getClass

We have 2 overloaded methods, one has Integer and another has String as parameter. You are calling method with null. Which method will be called

Question: You have 2 overloaded methods, doSomething, in below code. One takes Integer as parameter whereas another takes String. If you call it with null, which method will be called? What should be output of below code? public class OverloadDemo { public void doSomething(String str){ System.out.println("do something with String"); } public void doSomething(Integer str){ System.out.println("do something with Integer"); } public static void main(String[] args) { OverloadDemo overloadDemo = new OverloadDemo(); overloadDemo.doSomething(null); } } Answer: Compilation error -  The method doSomething(String) is ambiguous for the type OverloadDemo

equals() method of Object class in Java

Image
When you compare two object references using == operator, it evaluate to true only when both references refer to same object. public class Person { private Integer id; private String name; public Person(Integer id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Person p1 = new Person(1, "Prithvi"); Person p2 = p1; Person p3= new Person(1, "Prithvi"); System.out.println(p1==p2); System.out.println(p1==p3); } } Output: true false We have created 3 references of Person class. Object references p1 and p2 both refer to same object so comparison evaluate to true. Comparison of p1 and p3 evaluate to false, even though p3 has same contents as p1 but refer to different object so comparison returns false. Below image explains this - Objects p3 and p1 are meaningfully equal. We must have some way to see their content are same or not. But the question is, How will you compare two different object

How to define Auxiliary Constructor in case class in Scala

We have seen case class previously. We had discussed what is case class, how to create object of case class, list of boilerplate code generated for case class. If you don't know case class, I recommend to understand case class first. Let us revise case class declaration. case class Person(id:Int, name: String) Person class is declared with primary constructor where id and name are parameter. So good so far. The constructor which is not primary constructor, other than primary constructors and overloaded to primary constructor are called auxiliary constructor. How will you declare Auxiliary(other overloaded) constructors. What is syntax of creating auxiliary constructor. Please note that constructors in case class are not really constructor. Actually these are apply methods on companion object. Auxiliary constructor in case class are different than regular class. We can add auxiliary constructors in case class by adding apply() methods in companion object of case class

What is difference between Runnable and Thread? What is the best way to create thread?

There are 2 ways of creating thread. First way is by extending Thread class and another is implementing Runnable interface. What are the differences between these 2 ways. This is frequently asked question in Java interviews. 1. Multiple Inheritance: Thread is a class. Once you extend Thread class, you can not extend other class because Java does not support multiple inheritance. Whereas Runnable is interface so you can extend other class while implementing Runnable interface. 2. Worker and Job: Thread is worker which works on job(Runnable). It is always a good practice to separate code of worker and job. If we create thread by extending Thread class, It will combine code of worker and job. So it is good practice to implement Runnable interface. Object of this class(Job) will be passed to thread constructor(Worker). The thread instance will call run method on class which implements Runnable. This is how we can separate Worker and its Job. 3. extends means adding new Feature: Whe

case class in Scala

Case class is special type of class. You can class by prepending case keyword  in class declaration. case class Person(id:Int, name: String) Object Creation: You do not need to use new  keyword to create object of case class.  object TestMe { def main(args: Array[String]): Unit = { val person = Person(1,"Prithvi") } } When you create case class, compiler generates apply method for constructor. When you create object, it internally calls apply method. Visibility of variables: If we don't specify visibility(var or val) in variable declaration, It will use val as by default. Since variable are declared as val, accessors will be generated for those variables but not mutators.   Case classes are meant for immutability. Once object of case class is created, it should not be mutated. But we can make case class mutable by declaring variables as var but it will violate the intention of case class  Boilerplate codes: As we have seen, case class is

Hibernate Interview Questions

Explain table mappings (Many To Many, One to Many, Many to One) in Hibernate. Explain internal and external cache in Hibernate. How to configure external cache in Hibernate? What is difference between saveOrUpdate() and merge() methods in Hibernate? What is difference between save() and persist() methods in Hibernate? What are the core interfaces in Hibernate?

Spring Interview Questions

What is Autowiring? What is difference between  BeanFactory and ApplicationContext in Spring? What are the different Implementation of ApplicationContext? What is IoC? What is IoC container in Spring? How to change name of xml file in Spring-MVC? Explain the process of creating controller in Spring. How to configure error messages in web application using Spring? What are the types of controllers in Spring? What are scopes of bean in Spring? What is @Transaction in Spring? What are the attributes of this annotation? Explain all type of propagation in Spring. How transaction work in Spring? Explain types of views in Spring? What is cyclic bean dependency in Spring? how to resolve this? What is Spring bean life cycle What are the scopes of spring bean Difference between setter and constructor injection in Spring? When to use what type of injection

Thread interview questions

What is difference between Runnable and Thread? What is the best way to create thread? What is difference between Thread and Process? Explain sleep, yield and join methods of Thread class. Why wait, notify and notifyAll methods are defined in Object class but not in Thread class? Implement Producer and Consumer problem using wait and notify method. Is it possible to call run method explicitly? Please explain. Is it possible to overload run method? How two threads communicate? What is difference between concurrency and synchronization ? What is thread stacks? What is call stack? What is Daemon thread? How start() method work in Java? What is difference between wait() and sleep() in java.