Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2548 of 3366
632 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
802 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
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
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
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
1K+ 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
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
15K+ Views
In this article, we will learn to make a JTextField accept only numbers in Java. By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int. Different Approaches The following are the two different approaches for making a JTextField accept only numbers in Java: Using a KeyListener Using DocumentFilter Using a KeyListener The KeyListener interface handles keyboard events, making it straightforward to ... Read More
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
1K+ 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