Found 2616 Articles for Java

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

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; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super {    public void sampleMethod()throws FileNotFoundException {       System.out.println("Method of superclass");    } } public class ExceptionsExample extends Super {    public void sampleMethod() {       System.out.println("Method of Subclass");    }    public static void main(String args[]) ... Read More

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

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; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super {    public String readFile(String path)throws FileNotFoundException {       throw new FileNotFoundException();    } } public class ExceptionsExample extends Super {    @Override    public String readFile(String path)throws IOException {       //method body ......    } }Compile ... Read More

How to make a collection thread safe in java?

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) collection backed by the specified collection.2static List synchronizedList(List list)This method accepts an object of the List interfacereturns a synchronized (thread-safe) list backed by the specified list.3static Map synchronizedMap(Map m)This method accepts an object of the Map interface and, returns a synchronized (thread-safe) map backed by the specified map.4static ... Read More

Can we change method signature in overriding in Java?

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 try the method of the super class will be executed.Reason − If you change the signature both are considered as different methods and, since the copy of super class method is available at sub class object, it will be executed.Example Live Democlass Super {    void sample(int a, int b) { ... Read More

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

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 ClassLoader class. These two classes accept string values representing the class names and loads the specified classes.While passing class names to these methods you need to make sure that −The class names you pass to these methods should be accurate.The specified class (along with the package) should be either in ... Read More

How transient works with final in Java serialization?

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

681 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 the serialization process). i.e. When we declare a variable transient, after de-serialization its value will always be null, false, or, zero (default value).Therefore, While serializing an object of a class, if you want JVM to neglect a particular instance variable you need can declare it transient.public transient int limit = ... Read More

Difference between constants and final variables in Java?

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 Java(directly). But, you can still create a constant by declaring a variable static and final.Once you declare a variable static they will be loaded in to the memory at the compile time i.e. only one copy of them is available.once you declare a variable final you cannot modify its value ... Read More

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

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 throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ... Read More

How to make elements of array immutable in Java?

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() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample ... Read More

Can a final keyword alone be used to define a constant in Java?

Maruthi Krishna
Updated on 15-Oct-2019 06:41:59

297 Views

A 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 other languages java does not support constants directly. But, you can still create a constant by declaring a variable static and final.Static − Once you declare a variable static they will be loaded into the memory at the compile time i.e. only one copy of them is available.Final − once you declare a variable final you cannot modify its value ... Read More

Advertisements