Found 4338 Articles for Java 8

What is narrowing in java? Explain.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

4K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) as listed below −boolean − Stores 1-bit value representing true or, false.byte − Stores twos compliment integer up to 8 bits.char − Stores a Unicode character value up to 16 bits.short − Stores an integer value upto 16 bits.int − Stores an integer value upto 32 bits.long − Stores an integer value upto 64 bits.float − Stores a floating point value upto 32bits.double − Stores a floating point value up to 64 bits.Converting one primitive data type into another is known as type ... Read More

What happens if the subclass does not override abstract methods in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:39:48

5K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Extending an abstract classOnce you extend an abstract class in Java you need to override all the abstractmethods in it or, declare it abstract. If you don’t, a ... Read More

Can the abstract methods of an interface throw an exception in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:27:42

2K+ Views

Yes, the abstract methods of an interface can throw an exception.ExampleIn the following example the interface (MyInterface) contains an abstract method with name display, which throws an IOException.import java.io.IOException; abstract interface MyInterface {    public abstract void display()throws IOException ; }Rules to be followedYou need to follow the rules given below while implementing such method −If the abstract method in the interface throws certain exception. The implemented method can throw the same exception as shown below −Example 1 Live Demoimport java.io.IOException; abstract interface MyInterface {    public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{    public void ... Read More

Can abstract method declaration include throws clause in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:28:41

2K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();Yes, you can throw and exception from an abstract class.ExampleIn the following Java program, we have a two classes: one abstract class with name MyClass which have an abstract method (display()) and, another class that extends the abstract class.Here, the display() method abstract class throws an IOException.Example Live Demoimport java.io.IOException; abstract class MyClass {    public abstract void display() throws IOException; } public class AbstractClassExample extends MyClass{    public void display() throws IOException ... Read More

Accessing variables of two interfaces, which are same from an implementing class in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:29:24

710 Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.You can implement multiple interfaces using a single class in Java. Whenever both interfaces have same name, since all the fields of an interface are static by default, you can access them using the name of the interface as −Exampleinterface MyInterface1{    public static int num = 100;    public void display(); } interface MyInterface2{    public static int num = 1000;    public void show(); } public class InterfaceExample implements MyInterface1, MyInterface2{    public static int num = 10000; ... Read More

What happens if a class does not implement all the abstract methods of an interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:31:01

2K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.In a separate class you need to implement this interface and provide body for all its abstract methods.Once you implement an interface using a class, you must provide body (implement) to all of its abstract methods or, should declare the class as abstract. If you don’t a compile time error will be generated for each unimplemented method saying “InterfaceExample is not abstract and does not override abstract method method_name in interface_name”.In the following java program, we have an interface with name ... Read More

Can we define constructor inside an interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:31:53

6K+ Views

No, you cannot have a constructor within an interface in Java.You can have only public, static, final variables and, public, abstract, methods as of Java7.From Java8 onwards interfaces allow default methods and static methods.From Java9 onwards interfaces allow private and private static methods.Moreover, all the methods you define (except above mentioned) in an interface should be implemented by another class (overridden). But, you cannot override constructors in Java.Still if you try to define constructors in an interface it generates a compile time error.ExampleIn the following Java program, we are trying to define a constructor within an interface.public interface MyInterface{   ... Read More

Explain how to remove Leading Zeroes from a String in Java

Venkata Sai
Updated on 29-Jun-2020 14:48:21

2K+ Views

Whenever you read an integer value into a String, you can remove leading zeroes of it using StringBuffer class, using regular expressions or, by converting the given String to character array.Converting to character arrayFollowing Java program reads an integer value from the user into a String and removes the leading zeroes from it by converting the given String into Character array.Exampleimport java.util.Scanner; public class LeadingZeroes {    public static String removeLeadingZeroes(String num){       int i=0;       char charArray[] = num.toCharArray();       for( ; i

Can we serialize static variables in Java?

Venkata Sai
Updated on 29-Jun-2020 14:48:49

4K+ 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).But, static variables belong to class therefore, you cannot serialize static variables in Java. Still if you try to do so, the program gets compiled successfully but it raises an exception at the time of execution.In the following java program, the class Student has a static variable and we are trying to serialize and desterilize its object in the another class named ExampleSerialize.Exampleimport java.io.FileInputStream; import java.io.FileOutputStream; ... Read More

Can we declare an interface as final in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:33:25

4K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.Making an interface final.If you declare a class final cannot extend it. If you make a method final you cannot override it and, if you make a variable final you cannot modify it. i.e. use final with Java entities you cannot modify them further.If you make an interface final, you ... Read More

Advertisements