Found 2616 Articles for Java

What will happen when we try to override final method of the superclass in Java?

raja
Updated on 30-Jul-2019 22:30:26

1K+ Views

Any method that is declared as final in the superclass cannot be overridden by a subclass. If we try to override the final method of super class we will get an error in Java.Rules for implementing Method OverridingThe method declaration should be the same as that of the method that is to be overridden.The class (subclass) should extend another class (superclass), prior to even try overriding.The Sub Class can never override final methods of the Super Class.ExampleLive Democlass Car {    public void brake() {       System.out.println("brake() method of Car");    }    public final void accelerate() {       ... Read More

What is a ClassCastException and when it will be thrown in Java?

raja
Updated on 30-Jul-2019 22:30:26

1K+ Views

The java.lang.ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.When will be ClassCastException is thrownWhen we try to cast an object of Parent class to its Child class type, this exception will be thrown.When we try to cast an object of one class into another class type that has not extended the other class or they don't have any relationship between them.ExampleLive Democlass ParentTest {    String parentName;    ParentTest(String n1){       parentName = n1;    }    public ... Read More

What are the different types of nested classes are defined in Java?

raja
Updated on 30-Jul-2019 22:30:26

803 Views

In Java, it is possible to define a class inside another class, such classes are called Nested classes. We can use the access modifiers like private, public, protected or default for inner classes and default or public access modifiers for outer class.There are two types of nested classes are defined in Java.Static Nested ClassNon-Static Nested ClassStatic Nested ClassWe Can define an inner class as static, so such type of classes is called a static nested class.The nested class is defined with the static keyword, so this type of nested classes doesn’t share any relationship with the instance of an outer class.A static ... Read More

What are the differences between length and length () in Java?

raja
Updated on 30-Jul-2019 22:30:26

3K+ Views

The length is an instance variable of an array in Java whereas length() is a method of String class.lengthAn array is an object that holds a fixed number of values of the same type.The length variable in an array returns the length of an array i.e. a number of elements stored in an array.Once arrays are initialized, its length cannot be changed, so the length variable can directly be used to get the length of an array.The length variable is used only for an array.ExampleLive Demopublic class ArrayLengthTest {    public static void main(String args[]) {       int ... Read More

Can we declare a try catch block within another try catch block in Java?

raja
Updated on 30-Jul-2019 22:30:26

9K+ Views

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.Nested Try-Catch BlockIf an inner try statement does not have a matching catch statement for a particular exception, the control is transferred to the next try statement catch handlers that are expected for a matching catch statement.This continues until one of the catch statements succeeds, or until all of the nested try statements are done.If no one catch statements match, then the Java run-time system will handle the exception.When nested try blocks are used, the inner try block is executed first. Any exception thrown in the ... Read More

Can we declare a top level class as protected or private in Java?

raja
Updated on 30-Jul-2019 22:30:26

7K+ Views

No, we cannot declare a top-level class as private or protected. It can be either public or default (no modifier). If it does not have a modifier, it is supposed to have a default access.Syntax// A top level class    public class TopLevelClassTest {       // Class body }If a top-level class is declared as private the compiler will complain that the modifier private is not allowed here. This means that a top-level class cannot be a private, the same can be applied to protected access specifier also.Protected means that the member can be accessed by any class in the same ... Read More

Can a method local inner class access the local final variables in Java?

raja
Updated on 29-Jun-2020 11:12:53

687 Views

Yes, we can access the local final variables using the method local inner class because the final variables are stored on the heap and live as long as the method local inner class object may live.Method Local Inner ClassA local inner class instance can be delivered as an argument and retrieved from the methods and it is available inside a valid scope.The only limitation in method local inner class is that a local parameter can be executed only when it is defined as final.The method executing the local parameters can be called after the execution of the method, within which the local inner ... Read More

How to handle the ArrayStoreException (unchecked) in Java?

raja
Updated on 24-Feb-2020 10:21:25

229 Views

The java.lang.ArrayStoreException is an unchecked exception and it can occur when we try to store an object of a type in an array of objects of a different type. Usually, one would come across java.lang.ArrayStoreException: java.lang.Integer which occurs when an attempt is made to store an integer in an array of different type like an array of String or array of float, etc.Example1Live Demopublic class ArrayStoreExceptionTest {    public static void main(String[] args) {       Object[] names = new Float[2];       names[1] = new Integer(2);    } }OutputException in thread "main" java.lang.ArrayStoreException: java.lang.Integer         at ArrayStoreExceptionTest.main(ArrayStoreExceptionTest.java:4)In the ... Read More

How to handle the NumberFormatException (unchecked) in Java?

raja
Updated on 06-Feb-2020 12:47:43

2K+ Views

The NumberFormatException is an unchecked exception thrown by parseXXX() methods when they are unable to format (convert) a string into a number.The NumberFormatException can be thrown by many methods/constructors in the classes of java.lang package. Following are some of them.public static int parseInt(String s) throws NumberFormatExceptionpublic static Byte valueOf(String s) throws NumberFormatExceptionpublic static byte parseByte(String s) throws NumberFormatExceptionpublic static byte parseByte(String s, int radix) throws NumberFormatExceptionpublic Integer(String s) throws NumberFormatExceptionpublic Byte(String s) throws NumberFormatExceptionThere are situations defined for each method, where it can throw a NumberFormatException. For instance, public static int parseInt(String s) throws NumberFormatException whenString s is null or length of s is zero.If the String s ... Read More

How to handle the ArithmeticException (unchecked) in Java?

raja
Updated on 30-Jul-2019 22:30:26

6K+ Views

The java.lang.ArithmeticException is an unchecked exception in Java. Usually, one would come across java.lang.ArithmeticException: / by zero which occurs when an attempt is made to divide two numbers and the number in the denominator is zero. ArithmeticException objects may be constructed by the JVM.Example1Live Demopublic class ArithmeticExceptionTest {    public static void main(String[] args) {       int a = 0, b = 10;       int c = b/a;       System.out.println("Value of c is : "+ c);    } }In the above example,  ArithmeticExeption is occurred due to denominator value is zero.java.lang.ArithmeticException: Exception thrown by java during ... Read More

Advertisements