Found 9152 Articles for Object Oriented Programming

Can a class in Java be both final and abstract?

Samual Sam
Updated on 30-Jul-2019 22:30:20

2K+ Views

An abstract cannot be instantiated. Therefore to use an abstract class you need to create another class and extend the abstract class and use it. If a class is final you can’t extend it further. So, you cannot declare a class both final and abstract. Example Still if you try to do so you will get a compile time error saying “illegal combination of modifiers:” final abstract class Demo{ public final void display(){ System.out.println("Hello welcome to tutorialspoint"); } } Output C:\Sample>javac Demo.java Demo.java:1: error: illegal ... Read More

What is the number wrapper class and its methods in Java?

vanithasree
Updated on 30-Jul-2019 22:30:20

417 Views

The Number class (abstract) of the java.lang package represents the numeric values that are convertible to primitive types byte, double, float, int, long, and short. Following are the method provided by the Number class of the java.lang package. Sr.No Method & Description 1 byte byteValue() This method returns the value of the specified number as a byte. 2 abstract double doubleValue() This method returns the value of the specified number as a double. 3 abstract float floatValue() This method returns the value of the specified number as a float. ... Read More

What is a method signature in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

7K+ Views

The method signature consists of the method name and the parameter list. Example Live Demo public class MethodSignature { public int add(int a, int b){ int c = a+b; return c; } public static void main(String args[]){ MethodSignature obj = new MethodSignature(); int result = obj.add(56, 34); System.out.println(result); } } Output 90 Method ... Read More

How to create and write JSON array to a file in java?

Abhinaya
Updated on 19-Feb-2020 12:26:38

4K+ Views

Java provides javax.json.Json package which contains classes to read a JSON array:Exampleimport java.io.FileOutputStream; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonWriter; public class JSONArrayToFile {    public static void main(String args[]) throws Exception {       JsonArray value = Json.createArrayBuilder()          .add(Json.createObjectBuilder()          .add("id", "1001")          .add("Technology", "JavaFX"))          .add(Json.createObjectBuilder()          .add("id", "1002")          .add("Technology", "OpenCV"))          .build();       System.out.println(value);       JsonWriter writer = Json.createWriter(new FileOutputStream("sampleData"));       writer.writeArray(value);       writer.close();    } }Output["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]

What is the super class of every class in Java?

Alshifa Hasnain
Updated on 28-Feb-2025 18:48:15

6K+ Views

The Object class is the superclass of every single class in Java. This position implies that every class in Java, even if a built-in class or function like String or a user-defined one, directly or indirectly inherits from Object. Understanding the Object Class The Object class provides fundamental methods that every Java object inherits. Even if a class does not explicitly extend another class, it implicitly extends Object. Below is a demostration of the same − class Example { //This class implicitly extends to an Object } Why Does Every Class Inherit from ... Read More

How to remove a specific element from a JSON Array in Java?

Govinda Sai
Updated on 19-Feb-2020 12:25:46

15K+ Views

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.Exampleimport org.json.JSONArray; public class RemoveFromJsonArray {    public static void main(String args[]) throws Exception {       String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"};       JSONArray jsArray = new JSONArray();       for (int i=0; i < myArray.length; i++) {          jsArray.put(myArray[i]);      }      System.out.println(jsArray);      jsArray.remove(3);      System.out.println("After deleting ::"+jsArray);    } }Output["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]

Can a final class be subclassed in Java?

Alshifa Hasnain
Updated on 27-Feb-2025 19:28:35

2K+ Views

In Java Inheritance is a fundamental feature that allows a class to derive properties and behaviors from another class. In Java, not all classes can be subclassed. A final class is a special type of class that cannot be extended.  What is a Final Class in Java? In Java, a class is declared final using the final keyword. The final modifier for finalizing the implementations of classes, methods, and variables. When a class is marked as final, it means − It cannot be extended. All its methods remain unchanged in their ... Read More

How to convert JSON Array to normal Java Array?

Ramu Prasad
Updated on 19-Feb-2020 12:24:47

3K+ Views

The get method of the JSONArray class returns the element at a particular index. Using this method, you can get the elements of the JSONArray object and populate the array with them.Exampleimport java.util.Arrays; import org.json.JSONArray; public class JsonToArray {    public static void main(String args[]) throws Exception {       String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"};       JSONArray jsArray = new JSONArray();       for (int i = 0; i < myArray.length; i++) {          jsArray.put(myArray[i]);      }      System.out.println(jsArray);      String[] array = new String[myArray.length];   ... Read More

How to convert Java Array/Collection to JSON array?

Sravani S
Updated on 30-Jul-2019 22:30:20

8K+ Views

Google provides a library named org.json.JSONArray and, following is the maven dependency to add library to your project. com.googlecode.json-simple json-simple 1.1 The JSONArray class of the org.json package provides put() method. Using this method, you can populate the JSONArray object with the contents of the elements.Exampleimport org.json.JSONArray; public class ArrayToJson { public static void main(String args[]) { String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"}; JSONArray jsArray = new JSONArray(); ... Read More

How to perform binary search on an array in java?

V Jyothi
Updated on 16-Jun-2020 10:17:11

168 Views

The Arrays class of java package provides you a method named binarySearch() using this method you can perform a binary search on an array in Java.ExampleLive Demoimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int intArr[] = {30,20,5,12,55};       Arrays.sort(intArr);       System.out.println("The sorted int array is:");       for (int number : intArr) {          System.out.println("Number = " + number);       }       int searchVal = 12;       int retVal = Arrays.binarySearch(intArr,searchVal);       System.out.println("The index of element 12 is : " + retVal);    } }OutputThe sorted int array is: Number = 5 Number = 12 Number = 20 Number = 30 Number = 55 The index of element 12 is: 1

Advertisements