Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Object Oriented Programming Articles
Page 52 of 588
How to hide unsupported interface methods from class in Java?
Actually you can’t, once you implement an interface it is a must to provide implementation to all its methods or, make the class abstract. There is no way to skip methods of an interface without implementing (unless they are default methods). Still, if you try to skip implementing methods of an interface a compile-time error would be generated.Exampleinterface MyInterface{ public static int num = 100; public void sample(); public void getDetails(); public void setNumber(int num); public void setString(String data); } public class InterfaceExample implements MyInterface{ public static int num = 10000; public void ...
Read MoreHow to find the Strings within a text file in Java?
Following Java program accepts a String value from the user, verifies whether a file contains the given String and prints the number of occurrences of the word too.Exampleimport java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class FindingWordFromFile { public static void main(String args[]) throws FileNotFoundException { //Reading the word to be found from the user Scanner sc1 = new Scanner(System.in); System.out.println("Enter the word to be found"); String word = sc1.next(); boolean flag = false; int count = 0; System.out.println("Contents ...
Read MoreHow to override only few methods of interface in Java?
Once you implement an interface from a concrete class you need to provide implementation to all its methods. If you try to skip implementing methods of an interface at compile time an error would be generated.Exampleinterface MyInterface{ public void sample(); public void display(); } public class InterfaceExample implements MyInterface{ public void sample(){ System.out.println("Implementation of the sample method"); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.sample(); } }Compile-time errorInterfaceExample.java:5: error: InterfaceExample is not abstract and does not override abstract method display() ...
Read MoreCan I define more than one public class in a Java package?
No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.ExampleIn the following example we have two classes Student and AccessData we are having both of them in the same class and declared both public.import java.util.Scanner; public class Student { private String name; private int age; Student(){ this.name = "Rama"; this.age = 29; } Student(String name, int age){ ...
Read MoreWhen overriding clone method, why do we need to declare it as public in Java?
The clone() method belongs to the class named Object of the java.lang package, it accepts an object as a parameter creates and returns a copy of it.In order to use this method, you need to make sure that your class implements the Cloneable (marker) interface.Examplepublic class CloneExample implements Cloneable { private String name; private int age; public CloneExample(String name, int age){ this.name = name; this.age = age; } public void displayData(){ System.out.println("Name : "+this.name); System.out.println("Age : "+this.age); } public static void ...
Read MoreWhat happens when a subclass object is assigned to a superclass object in Java?
Converting one data type to others in Java is known as casting.If you convert a higher datatype to lower datatype, it is known as narrowing (assigning higher data type value to the lower data type variable).char ch = (char)5;If you convert a lower data type to a higher data type, it is known as widening (assigning lower data type value to the higher data type variable).Int i = 'c';Similarly, you can also cast/convert an object of one class type to others. But these two classes should be in an inheritance relation. Then, If you convert a Super class to subclass ...
Read MoreException Hierarchy in case of multiple catch blocks.
An exception is an issue (run time error) that occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Multiple exceptions in a codeWhenever we have a code that may generate more than one exception and if you need to handle them specifically you can use multiple catch blocks on a single try.try{ //code } catch(Exception1 ex1) { // } catch(Exception2 ex2) { // }Exampleimport java.util.Arrays; import java.util.Scanner; public class MultipleCatchBlocks { public static void main(String [] args) ...
Read MoreHow to read/write data from/to .properties file in Java?
The .properties is an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.Creating a .properties file −To create a properties file −Instantiate the Properties class.Populate the created Properties object using the put() method.Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.ExampleThe Following Java program creates a properties file in the path D:/ExampleDirectory/import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile { ...
Read MoreWhich packages contain Wrapper class in Java?
Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them. Following is the list of primitive data types and their respective classes −Primitive datatypeWrapper classcharCharacterbyteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleanPackageWrapper classes in Java belong to the java.lang package, Therefore there is no need to import any package explicitly while working with them.ExampleThe following Java example accepts various primitive variables from the user and creates their respective wrapper classes.import java.util.Scanner; public class WrapperClassesExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: ...
Read MoreHow to move files using FileUtils in Java?
Using the File classThe class named File of the java.io package represents a file or directory (pathnames) in the system. This class provides various methods to perform various operations on files/directories.This class provides various methods to manipulate files, The rename() method of the File class accepts a String representing a destination file and, renames the abstract file path of the current file to the given one.This method actually moves the file from the source path to the destination path.Exampleimport java.io.File; public class MovingFile { public static void main(String args[]) { //Creating a source file object ...
Read More