Venkata Sai

Venkata Sai

45 Articles Published

Articles by Venkata Sai

45 articles

What are the modifiers allowed for methods in an Interface in java?

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

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.In Java 7As of Java7 you can have only public, abstract as modifiers for the methods of an interface.interface MyInterface{ public abstract void display(); public abstract void setName(String name); public abstract void setAge(int age); }Using any other modifier with the methods of an interface would lead to a compile time error.From Java8From Java8 onwards interfaces ...

Read More

Does Java support multiple inheritance? Why? How can we resolve this?

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

Whenever, you extend a class a copy of superclass’s members is available to the subclass object and, when you can call the method of the superclass using the object of the subclass.ExampleIn the following example, we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo().class SuperClass{ public void demo() { System.out.println("demo method"); } } public class SubClass extends SuperClass { public static ...

Read More

Can we cast an object reference to an interface reference in java? If so when?

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

Yes, you can.If you implement an interface and provide body to its methods from a class. You can hold object of the that class using the reference variable of the interface i.e. cast an object reference to an interface reference.But, using this you can access the methods of the interface only, if you try to access the methods of the class a compile time error is generated.ExampleIn the following Java example, we have an interface named MyInterface with an abstract method display().We have a class with name InterfaceExample with a method (show()). In addition to it, we are implementing the ...

Read More

Explain restrictions on using enum in java?\\n

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 855 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

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

Can we create an enum with custom values in java?

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

Enumeration (enum) in Java is a datatype which stores a set of constant values (Strings in general). You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Custom values to the constantsInstead of declaring just string constants in an enum, you can also have values to these constants as −enum Vehicles { ACTIVA125(80000), ACTIVA5G(70000), ACCESS125(75000), VESPA(90000), TVSJUPITER(75000); }Whenever, you need to assign custom values to the constants of an enum −To hold the value of each ...

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

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 are copy constructors in Java?

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

Generally, the copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.Java supports for copy constructors but unlike C language, Java does not provide an explicit copy constructor you need to define it yourself.writing a copy constructorUsually, to initialize the values of instance variables of a class (one way) we create a parameterized constructor accepting the values for all instance variables and initialize them with the given values.int name; int age; public Student(String name, int age){ this.name = name; this.age ...

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
Showing 1–10 of 45 articles
« Prev 1 2 3 4 5 Next »
Advertisements