Maruthi Krishna has Published 951 Articles

What is loose coupling how do we achieve it using Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:22:53

718 Views

Coupling refers to the dependency of one object type on another, if two objects are completely independent of each other and the changes done in one doesn’t affect the other both are said to be loosely coupled.You can achieve loose coupling in Java using interfaces -Example Live Demointerface Animal {   ... Read More

While overriding can the subclass choose not to throw an exception in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:13:35

472 Views

If the super-class method throws certain exception, you can override it without throwing any exception.ExampleIn the following example the sampleMethod() method of the super-class throws FileNotFoundException exception and, the sampleMethod() method does not throw any exception at all. Still this program gets compiled and executed without any errors. Live Demoimport java.io.File; ... Read More

Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:10:50

223 Views

If the super-class method throws certain exception, the method in the sub-class should not throw its super type.ExampleIn the following example the readFile() method of the super-class throws FileNotFoundException exception and, the readFile() method of the sub-class throws an IOException, which is the super type of the FileNotFoundException. Live Demoimport java.io.File; ... Read More

How to make a collection thread safe in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:08:23

2K+ Views

The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.This class provides different variants of the synchronizedCollection() method as shown below −Sr.NoMethods & Description1static Collection synchronizedCollection(Collection c)This method accepts any collection object and, returns a synchronized (thread-safe) ... Read More

Can we change method signature in overriding in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:03:06

2K+ Views

No, while overriding a method of the super class we need to make sure that both methods have same name, same parameters and, same return type else they both will be treated as different methods.In short, if we change the signature, you cannot override the super class’s method if you ... Read More

Why do we get ClassNotFoundException when the class exists in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:01:24

3K+ Views

Whenever we try to load a class, if the class loader is not able to find the class at the specified path a ClassNotFoundException is generated.This may occur while executing java program, loading a class explicitly using forName() method of the class named Class or the loadClass() method of the ... Read More

How transient works with final in Java serialization?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:56:27

670 Views

In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).Transient variables − The values of the transient variables are never considered (they are excluded from ... Read More

Difference between constants and final variables in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:53:36

10K+ Views

Constant in JavaA constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.Unlike in C language constants are not supported in ... Read More

Can we initialize static variables in a default constructor in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:50:25

7K+ Views

Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available ... Read More

How to make elements of array immutable in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:44:33

2K+ Views

No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() ... Read More

Advertisements