Java: difference between private, protected, and public?
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.
Access specifiers are keywords that determines the type of access to the member of a class. These are:
* Public
* Protected
* Private
* Defaults
java show window JFrame JDialog always on top
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Always_on_Top_JFrame_JAVA{
public static void main(String[] args) {
JFrame frame = new JFrame("Title - always on top :D ");
// Set's the window to be "always on top"
frame.setAlwaysOnTop( true );
frame.setLocationByPlatform( true );
frame.add( new JLabel(" Always on TOP ") );
frame.pack();
frame.setVisible( true );
}
}
Order of catching exception in java
A. Yes, it does. The FileNotFoundException is inherited from the IOException.
wrapper classes in Java
Wrapper Classes in Java – What Every Developer Should Know
A wrapper class is a reference type that encapsulates a primitive value in an object. The Java standard library provides a dedicated wrapper for each of the eight primitive data types, as well as one for void. These classes reside in the java.lang package and are designed to bridge the gap between primitives and objects, enabling you to store primitive values in collections, work with generics, and use a wealth of utility methods for parsing, comparison, and conversion. Since Java 5, autoboxing and unboxing allow you to seamlessly convert between a primitive and its corresponding wrapper without explicit calls to valueOf() or xxxValue(), making the code cleaner while still relying on the underlying wrapper machinery.
The table below lists every primitive type alongside its immutable wrapper counterpart. Immutability means once a wrapper instance is created, its value cannot change – a critical property when using them as keys in maps or as elements in thread‑safe contexts. Common operations like conversion from a String (Integer.parseInt()), obtaining a wrapper instance (Boolean.valueOf(true)), and extracting the primitive (double d = dObj.doubleValue()) are all defined directly on these classes.
Java Class as Applet as well as Application
A. Yes. Add a main() method to the applet.
Bring JFrame JDialog Window to front java swing
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
//myFrame is object of Window or JFrame
public void run() {
myFrame.toFront();
myFrame.repaint();
}
});
Redirect Standard System Output and Error Message to PrintStream in Java
->We can achieve this by using
public static void setErr(PrintStream err)
and public static void setOut(PrintStream out) methods.
By default, they both point at the system console. The following example redirects Out and Err messages to 'error.txt' file
Stream stream = new Stream(new FileOutputStream("error.txt"));
System.setErr(stream);
System.setOut(stream);
You can redirect the Err and Out stream to any PrintStream object.
concatenate arrays of any type - java
public static T[] concat(T[] first, T[] second) {
T[] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
For combining arbitrary number of arrays
difference between an interface and an abstract class
Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Interface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method definition for all the methods
Abstract class is a Class prefix with a abstract keyword followed by Class definition. Interface is a Interface which starts with interface keyword.
Abstract class contains one or more abstract methods. where as Interface contains all abstract methods and final declarations
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes. Interfaces are useful in a situation that all properties should be implemented.
Differences are as follows:
* Interfaces provide a form of multiple inheritance. A class can extend only one other class.
* Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
* A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
* Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
Similarities:
* Neither Abstract classes or Interface can be instantiated.
How to define an Abstract class?
A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class:
abstract class testAbstractClass {
protected String myString;
public String getMyString() {
return myString;
}
public abstract string anyAbstractFunction();
}
How to define an Interface?Answer: In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.
Example of Interface:
public interface sampleInterface {
public void functionOne();
public long CONSTANT_ONE = 1000;
}
Adding image to JPanel Java
BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
add( picLabel );
:D
