Found 2616 Articles for Java

What is the importance of the Throwable class and its methods in Java?

raja
Updated on 06-Feb-2020 11:30:26

368 Views

The Throwable class is a superclass of all errors and exceptions in Java. Objects that are instances of this class are thrown by the Java Virtual Machine or can be thrown by a throw statement. Similarly, this class or one of its subclasses can be the argument type in a catch clause.Instances of two subclasses Error and Exception are used to indicate that exceptional situations have occurred, these instances are created in the context of the exceptional situation to include relevant information.Commonly used exception methods of Throwable classpublic String getMessage(): returns the message string about the exception.public Throwable getCause(): returns the cause of the exception. ... Read More

When to use an abstract class and when to use an interface in Java?

raja
Updated on 02-Sep-2023 15:58:43

41K+ Views

An interface can be used to define a contract behavior and it can also act as a contract between two systems to interact while an abstract class is mainly used to define default behavior for subclasses, it means that all child classes should have performed the same functionality.When to use an abstract classAn abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.If we want to ... Read More

Can we create an object of an abstract class in Java?

raja
Updated on 06-Feb-2020 11:32:30

13K+ Views

No, we can't create an object of an abstract class. But we can create a reference variable of an abstract class. The reference variable is used to refer to the objects of derived classes (subclasses of abstract class).An abstract class means hiding the implementation and showing the function definition to the user is known as Abstract class. A Java abstract class can have instance methods that implement a default behavior if we know the requirement and partially implementation we can go for an abstract class.ExampleLive Demoabstract class Diagram {    double dim1;    double dim2;    Diagram(double a, double b) ... Read More

What is a constant and how to define constants in Java?

raja
Updated on 14-Sep-2023 02:01:33

29K+ Views

A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants.A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.To define a variable as a constant, we just need to add the keyword "final" in front of the variable declaration.Syntaxfinal float pi = 3.14f;The above statement declares the float variable "pi" as a constant with a value of 3.14f. We cannot change the value of "pi" ... Read More

Can we declare more than one class in a single Java program?

raja
Updated on 06-Feb-2020 11:35:52

14K+ Views

A single Java program contains two or more classes, it is possible in two ways in Java.Two Ways of Implementing Multiple Classes in a single Java ProgramNested ClassesMultiple non-nested classesHow the compiler behave with Multiple non-nested classesIn the below example, the java program contains two classes, one class name is Computer and another is Laptop. Both classes have their own constructors and a method. In the main method, we can create an object of two classes and call their methods.ExampleLive Demopublic class Computer {    Computer() {       System.out.println("Constructor of Computer class.");    }    void computer_method() { ... Read More

How many ways to prevent method overriding in Java?

raja
Updated on 06-Feb-2020 10:57:24

4K+ Views

Method overriding works because of the run-time method binding feature in Java. So, if we force the java compiler to do static binding for a method then we can prevent that method from being overridden in a derived class.We can prevent method overriding in Java in 3 waysBy making method final in the base classBy making a method static in the base classBy making a method private in the base classFinal methods can not be overriddenBy making a method final we are adding a restriction that derived class cannot override this particular method.ExampleLive Democlass Base {    public void show() ... Read More

Why we should follow the naming conventions in Java?

raja
Updated on 06-Feb-2020 11:00:02

3K+ Views

Naming conventions in Java make programs more understandable by making them easier to read.In Java, class names generally should be nouns, in title case with the first letter of each separate word capitalized. and interface names generally should be adjectives,  in title case with the first letter of each separate word capitalized.Why should follow the Java naming standardsTo reduce the effort needed to read and understand source code.To enable code reviews to focus on more important issues than arguing over syntax and naming standards.To enable code quality review tools to focus their reporting mainly on significant issues other than syntax ... Read More

What are the differences between default constructor and parameterized constructor in Java?

raja
Updated on 06-Feb-2020 11:01:46

11K+ Views

Default ConstructorA default constructor is a 0 argument constructor which contains a no-argument call to the super class constructor.To assign default values to the newly created objects is the main responsibility of default constructor.Compiler writes a default constructor in the code only if the program does not write any constructor in the class.The access modifier of default constructor is always the same as a class modifier but this rule is applicable only for “public” and “default” modifiers.When will compiler add a default constructorThe compiler adds a default constructor to the code only when the programmer writes no constructor in the code.If the ... Read More

What are the different types of classes in Java?

raja
Updated on 06-Feb-2020 11:06:17

2K+ Views

Types of classes in JavaConcrete classAny normal class which does not have any abstract method or a class that has an implementation of all the methods of its parent class or interface and its own methods is a concrete class.ExampleLive Demopublic class Concrete { // Concrete Class    static int product(int a, int b) {       return a * b;    }    public static void main(String args[]) {       int p = product(2, 3);       System.out.println("Product: " + p);    } }OutputProduct: 6Abstract classA class declared with abstract keyword and have zero or ... Read More

Why Object class is the super class for all classes in Java?

raja
Updated on 24-Feb-2020 10:51:49

4K+ Views

Java.lang.Object class is the root or superclass of the class hierarchy, which is present in java.lang package. All predefined classes and user-defined classes are the subclasses from Object class.Why object class is a superclassRe-usabilityEvery object has 11 common properties, these properties must be implemented by every Java developer.To reduce the burden on the developer SUN developed a class called Object by implementing all these 11 properties with 11 methods.All these methods have generic logic common for all the subclasses, if this logic is not satisfying subclass requirement then subclass can override itRuntime polymorphismTo achieve runtime polymorphism so that we can write a ... Read More

Advertisements