Found 4338 Articles for Java 8

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

Venkata Sai
Updated on 30-Jul-2019 22:30:26

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 are enumerations in Java? How to retrieve values from an enum?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

540 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 }Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0 in the above example the days are identified using numbers as shown in the following illustration −Retrieving values from an enumYou can retrieve all the ... Read More

Explain restrictions on using enum in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

625 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 cast an object reference to an interface reference in java? If so when?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

7K+ 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

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

Venkata Sai
Updated on 30-Jul-2019 22:30:26

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(). Live Democlass SuperClass{ public void demo() { System.out.println("demo method"); } } public class SubClass extends SuperClass { public ... Read More

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

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ 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

Can we declare the method of an Interface final in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

873 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.By default, all the methods of an interface are public and abstract. For example, In the following Java program, we are having a declaring a method with name demo.public interface MyInterface{ void demo(); }If you compile this using the javac command as shown below −c:\Examples>javac MyInterface.javait gets compiled without errors. But, if you verify the interface after compilation using the javap ... Read More

Explain naming conventions for packages in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

While choosing a package name you need to keep the following points in mind.The name of the package should be in small letters.It is suggested to start the name of the package with the top level domain level followed by sub domains, ex: com.example.tutorialspoint.ExampleYou can declare a package as −package com.mypackage.tutorialspoint; Public class Sample{ Public static void main(String args[]){ System.out.println("Welcome to Tutorialspoint"); } }Executing a packageYou need to compile the file with packages using –d option as −C:\Sample>javac -d . PackageExample.javaAfter compiling, you can see the class files ... Read More

Can we override final methods in Java?

Narasimha Murthi
Updated on 30-Jun-2020 07:12:47

1K+ Views

Overriding is a one of the mechanisms to achieve polymorphism. This is the case when we have two classes where, one inherits the properties of another using the extends keyword and, these two classes same method including parameters and return type (say, sample).Since it is 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 subclass.When we invoke this method (sample) JVM calls the respective method based on the object used to call the method.Overriding final methodsNo, you cannot override final method in java. If ... Read More

Can I overload static methods in Java?

Narasimha Murthi
Updated on 30-Jul-2019 22:30:26

817 Views

Overloading is a one of the mechanisms to achieve polymorphism where, a class contains two methods with same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.Example Live Demopublic class Calculator {    public int addition(int a , int b){       int result = a+b;       return result;    }    public int addition(int a , int b, int c){       int result = a+b+c;       return result;    }    public static void main(String args[]){       Calculator ... Read More

Advertisements