Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Articles - Page 257 of 745
3K+ Views
When superclass and the subclass contain the same instance methods including parameters, when called, the superclass method is overridden by the method of the subclass.Example Live Democlass Super{ public void sample(){ System.out.println("Method of the Super class"); } } public class MethodOverriding extends Super { public void sample(){ System.out.println("Method of the Sub class"); } public static void main(String args[]){ MethodOverriding obj = new MethodOverriding(); obj.sample(); } }OutputMethod of the Sub classMethod shadowingWhen superclass and subclass contain the same method including parameters and if they ... Read More
890 Views
Checked exceptionsA checked exception is an exception that occurs at the compile time, these are also called as compile-time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after the execution of the complete program.Example Live Demoimport java.io.File; import java.io.FileInputStream; public class Test { public static void main(String args[]){ System.out.println("Hello"); try{ ... Read More
3K+ Views
A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate() method of Graphics2D class inside the paintComponent().Syntaxpublic abstract void rotate(double theta, double x, double y)Exampleimport java.awt.*; import java.awt.geom.*; import javax.swing.*; public class RotateJLabelTest extends JFrame { public RotateJLabelTest() { setTitle("Rotate JLabel"); JLabel label ... Read More
3K+ Views
You can write a single generic method declaration that can be called with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Following are the rules to define Generic Methods −All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example).Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies ... Read More
1K+ Views
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Restrictions on genericsYou cannot use generics in certain ways and in certain scenarios as listed below −You cannot use primitive datatypes with generics.class Student{ T age; Student(T age){ this.age = age; ... Read More
1K+ Views
Whenever you create an object in Java it is stored in the heap area of the JVM. If the JVM is not able to allocate memory for the newly created objects an exception named OutOfMemoryError is thrown.This usually occurs when we are not closing objects for long time or, trying to act huge amount of data at once.There are 3 types of errors in OutOfMemoryError −Java heap space.GC Overhead limit exceeded.Permgen space.Example 1 Live Demopublic class SpaceErrorExample { public static void main(String args[]) throws Exception { Float[] array = new Float[10000 * 100000]; } }OutputRuntime exceptionException ... Read More
4K+ Views
Reference typesAs we know a class is a blue print in which we define the required behaviors and properties and, an interface is similar to class but it is a Specification (containing abstract methods).These are also considered as datatypes in Java, unlike other primitive datatypes a literal of these kind of types points/refers to the location of the object. They are also known as reference types.GenericsGenerics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the ... Read More
3K+ Views
In general, whenever you create a file you can restrict/permit certain users from reading/writing/executing a file.In Java files (their abstract paths) are represented by the File class of the java.io package. This class provides various methods to perform various operations on files such as read, write, delete, rename, etc.In addition, this class also provides the following methods −setExecutble() − This method issued to set the execute permissions to the file represented by the current (File) object.setWritable() − This method is used to set the write permissions to the file represented by the current (File) object.setReadable() − This method is used ... Read More
3K+ Views
When an exception occurs in Java, the program terminates abnormally and the code past the line that caused the exception doesn’t get executed.To resolve this you need to either wrap the code that causes the exception within try catch ot, throw the exception using the throws clause. If you throw the exception using throws clause it will be p[postponed to the calling line i.e.Example Live Demoimport java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ExceptionExample{ public static String readFile(String path)throws FileNotFoundException { String data = null; Scanner sc = new Scanner(new File("E://test//sample.txt")); ... Read More
6K+ Views
The local variables can be declared in methods, code blocks, constructors, etc in Java. When the program control enters the methods, code blocks, constructors, etc. then the local variables are created and when the program control leaves the methods, code blocks, constructors, etc. then the local variables are destroyed. The local variables do not have any default values in Java. This means that they can be declared and assigned a value before the variables are used for the first time, otherwise, the compiler throws an error.Examplepublic class LocalVariableTest { public void print() { int num; ... Read More