Found 2620 Articles for Java

How to wrap a JSON using flexjson in Java?

raja
Updated on 06-Jul-2020 12:45:15

806 Views

The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.Syntaxpublic JSONSerializer rootName(String rootName)Exampleimport flexjson.JSONSerializer; public class JSONRootNameTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true);       Employee emp = new Employee("Adithya", "Jai", 28, ... Read More

Can an interface in Java extend multiple interfaces?

Maruthi Krishna
Updated on 15-Oct-2019 12:48:00

690 Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static. Just like classes you can extend one interface from another using the extends keyword as shown below:interface ArithmeticCalculations {    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations extends ArithmeticCalculations {    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }In the same way you can extend multiple interfaces from an interface using the extends keyword, by separating the interfaces using comma (, ) ... Read More

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

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

680 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 {    void child(); } class Cat implements Animal {    public void child() {       System.out.println("kitten");    } } class Dog implements Animal {    public void child() {       System.out.println("puppy");    } } public class LooseCoupling {    public static void main(String args[]) {       Animal obj = new Cat();       obj.child();    } }Outputkitten

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

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

415 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

214 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

475 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

Advertisements