Found 9150 Articles for Object Oriented Programming

Writing UTF8 data to a file using Java

Maruthi Krishna
Updated on 10-Sep-2019 12:02:31

4K+ Views

In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters from multiple character sets, you will be able to do so using the single Unicode character encodings. It provides 3 types of encodings.UTF-8 − It comes in 8-bit units (bytes), a character in UTF8 can be from 1 to 4 bytes long, making UTF8 variable width.UTF-16 ... Read More

How can we check if file exists anywhere on the system in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:58:33

354 Views

You can verify whether a particular file exists in the system in two ways using the File class and using the Files class.Using The File classThe class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.This class provides various methods to manipulate files, The exists a () method of it verifies whether the file or directory represented by the current File object exists if so, it returns true else it returns false.ExampleThe following Java program verifies whether a specified file exists in ... Read More

How to read integers from a file using BufferedReader in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:54:20

19K+ Views

The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter.This class provides a method known as readLine() which reads and returns the next line from the source and returns it in String format.The BufferedReader class doesn’t provide any direct method to read an integer from the user you need to rely on the readLine() method to read integers too. i.e. Initially you need to read the integers in string format.The parseInt() method of the Integer class accepts a String ... Read More

How do you upcast and downcast the same object in Java?

Maruthi Krishna
Updated on 11-Sep-2019 07:22:54

337 Views

Converting one data type to others in Java is known as casting.Up 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).Example Live Demoimport java.util.Scanner; public class NarrowingExample {    public static void main(String args[]){       char ch = (char) 67;       System.out.println("Character value of the given integer: "+ch);    } }OutputCharacter value of the given integer: CDown casting − If you convert a lower datatype to a higher data type, it is known as widening (assigning lower data type ... Read More

Why subclass doesn't inherit the private instance variables of superclass in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:46:33

6K+ Views

When you declare the instance variables of a class private, you cannot access them in another class if you try to do so a compile-time error will be generated.But, if you inherit a class that has private fields, including all other members of the class the private variables are also inherited and available for the subclass.But, you cannot access them directly, if you do so a compile-time error will be generated.Example Live Democlass Person{    private String name;    public Person(String name){       this.name = name;    }    public void displayPerson() {       System.out.println("Data of the ... Read More

Is it possible to override a Java method of one class in same?

Maruthi Krishna
Updated on 10-Sep-2019 11:32:26

3K+ Views

When we have two classes where one extends another and if, these two classes have the same method including parameters and return type (say, sample) the method in the subclass overrides the method in the superclass.i.e. Since it is an inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.But if you call the method (sample), the sampling method of the subclass will be executed overriding the super class’s method.Example Live Democlass Super{    public static void sample(){       System.out.println("Method ... Read More

What happens when a subclass object is assigned to a superclass object in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:25:12

6K+ Views

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 More

How can we apply different borders to JButton in Java?

raja
Updated on 03-Jul-2020 12:29:27

5K+ Views

A JButton is a subclass of AbstractButton class and it can be used for adding platform-independent buttons in a Java Swing application. A JButon can generate an ActionListener interface when the user clicking on a button, it can also generate MouseListener when a user can do some actions from the mouse and KeyListener when a user can do some actions from the keyboard.We can set different borders like LineBorder, BevelBorder, EtchcedBorder, EmptyBorder, TitledBorder, etc to JButton using the setBorder() method of JComponent class.Syntaxpublic void setBorder(Border border)Exampleimport javax.swing.*; import java.awt.*; public class JButtonBordersTest extends JFrame {    private JButton button[];    private JPanel panel;    public JButtonBordersTest() {   ... Read More

When overriding clone method, why do we need to declare it as public in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:19:07

2K+ Views

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.Example Live Demopublic 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 ... Read More

How to override only few methods of interface in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:10:33

7K+ Views

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.Example Live Demointerface 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 ... Read More

Advertisements