Object Oriented Programming Articles

Page 258 of 589

Explain restrictions on using enum in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 860 Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Points to keep in mindYou need to keep the following points in mind while declaring an enum −It is recommended to write the name of the constants in all capital letters as −public class EnumerationExample {    enum Days { ...

Read More

Explain about field hiding in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 1K+ Views

Whenever you inherit a superclass a copy of superclass’s members is created at the subclass and you using its object you can access the superclass members.If the superclass and the subclass have instance variable of same name, if you access it using the subclass object, the subclass field hides the superclass’s field irrespective of the types. This mechanism is known as field hiding.But, since it makes code complicated field hiding is not recommended.ExampleIn the following example we have two classes Super and Sub one extending the other. They both have two fields with same names (name and age).When we print ...

Read More

Can we have variables and methods in an enum in Java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 4K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.You can define an enumeration using the keyword enum followed by the name of the enumeration as −enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Methods and variables in an enumerationEnumerations are similar to classes and, you can have variables, methods, and constructors within them. Only concrete methods are allowed in an enumeration.ExampleIn the following example, we are defining an enumeration with name ...

Read More

What is variable shadowing in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 6K+ Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.Class (static) variables − Class variables are variables declared within a class, ...

Read More

What is EOFException in Java? How do we handle it?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 4K+ Views

While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.Let us consider the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.ExampleFollowing program demonstrates how to handle the EOFException in Java.import ...

Read More

Elaborate the legal operands of the instance of operator in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 198 Views

The instanceof operator in Java is used to find whether a reference is an instance of a Type i.e. class or an interface.Examplepublic class InstanceOfExample {    public static void main(String args[]) {       String str = "hello";       boolean bool = str instanceof String;       System.out.println(bool);    } }OutputtrueLegal operands of the instanceof operatorThe following are the only legal operands of the instanceof operator −Left operand − It must be a reference representing an object.Right operand − It must be the name of Java class or, interface.Except these two if you use any ...

Read More

Differentiate between the prefix and postfix forms of the ++ operator in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 4K+ Views

Java provides two operators namely ++ and --, to increment and decrement values by 1 respectively.There are two variants of these operators −Pre-increment/decrement − This form, increments/decrements the value first, and then performs the specified operation.ExampleIn the following example, the initial value of the variable i is 5. We are printing the incremented value of it using the pre increment operator.Since we are using the pre increment operator, the value of i is incremented then printed.public class ForLoopExample {    public static void main(String args[]) {       int i = 5;       System.out.println(++i);       ...

Read More

Can a for statement loop infinitely in java?

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 3K+ Views

Using loops in programming languages we can execute a set of statements repeatedly. Java provides various loops namely while loop, for loop and the do while loop.The for statement contains an initialization statement, a condition and, an increment or decrement operation.Initializing statement − The initialization determines the starting value of the loop.Condition − The condition in for loop is a statement returning a boolean value. This condition determines the exit value of the loop. It is executed before the statements of the loop.Increment and decrement − Using this the loop will be incremented or decremented to the next value.After initializing ...

Read More

Java Programs for printing Pyramid Patterns. (of numbers)

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 307 Views

Following is a Java program which prints a pyramid of numbers.Examplepublic class PrintingPyramids {    public static void main(String args[]){       int n,i,j,k=1;       n = 5;       for(i = 0; i

Read More

Java program to print a given pattern. (stars)

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 857 Views

Following is a Java program which prints a pyramid of numbers.Examplepublic class PrintingPyramidStars {    public static void main(String args[]){       int n,i,j;       n = 5;       for(i = 0; i

Read More
Showing 2571–2580 of 5,881 articles
« Prev 1 256 257 258 259 260 589 Next »
Advertisements