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
Java Articles - Page 175 of 440
8K+ Views
Polymorphism is the ability of an object to perform different actions (or, exhibit different behaviors) based on the context.Overloading is one of the mechanisms to achieve polymorphism where a class contains two methods with the same name and different parameters.Whenever you call this method the method body will be bound with the method call based on the parameters.ExampleIn the following Java program, the Calculator class has two methods with name addition the only difference is that one contains 3 parameters and the other contains 2 parameters.Here, we can call the addition method by passing two integers or three integers. Based ... Read More
2K+ Views
An interface in Java is similar to a class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces.Default methods − Unlike other abstract methods these are the methods that can have a default implementation. If you have a default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Example Live Demointerface MyInterface{ public static int num = ... Read More
1K+ Views
Actually you can’t, once you implement an interface it is a must to provide implementation to all its methods or, make the class abstract. There is no way to skip methods of an interface without implementing (unless they are default methods). Still, if you try to skip implementing methods of an interface a compile-time error would be generated.Example Live Demointerface MyInterface{ public static int num = 100; public void sample(); public void getDetails(); public void setNumber(int num); public void setString(String data); } public class InterfaceExample implements MyInterface{ public static int num = 10000; public ... Read More
711 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.To create an object of this type you need to implement this interface, provide a body for all the abstract methods of the interface and obtain the object of the implementing class.All the methods of the interface are public and abstract and, we will define an interface using the interface keyword as shown below −interface MyInterface{ public void display(); public void setName(String ... Read More
1K+ Views
An abstract method is the one that does not have a body. It contains only a method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation (body) to it. If a class contains at least one abstract method, you must declare it abstract.Example Live Demoimport java.io.IOException; abstract class MyClass { public abstract void display(); } public class AbstractClassExample extends MyClass{ public void display(){ System.out.println("subclass implementation of the display method"); } public static void main(String ... Read More
375 Views
In Java, both double and float are data types that are used to declare a variable that can store only decimal values or floating point numbers. For example: 1.2343, 3.343, 24.321234, etc. The double data type is larger in size compared to the float data type. When to Prefer Double Type Over Float Type A double-type is preferred over a float-type if a more precise and accurate result is required. The precision of double-type is up to 15 to 16 decimal points, while the precision of float type is only around 6 to 7 decimal digits. Following are the syntaxes for ... Read More
8K+ Views
Yes, the protected method of a superclass can be overridden by a subclass. If the superclass method is protected, the subclass overridden method can have protected or public (but not default or private) which means the subclass overridden method can not have a weaker access specifier. Example class A { protected void protectedMethod() { System.out.println("superclass protected method"); } } class B extends A { protected void protectedMethod() { System.out.println("subclass protected method"); } } public class Test { public static void main(String args[]) { B b = new B(); b.protectedMethod(); } } Output subclass protected method
4K+ Views
A JPanel is a subclass of JComponent class and it is an invisible component in Java. The FlowLayout is a default layout for a JPanel. We can add most of the components like buttons, text fields, labels, tables, lists, trees, etc. to a JPanel.We can also add multiple sub-panels to the main panel using the add() method of Container class.Syntaxpublic Component add(Component comp)Exampleimport java.awt.*; import javax.swing.*; public class MultiPanelTest extends JFrame { private JPanel mainPanel, subPanel1, subPanel2; public MultiPanelTest() { setTitle("MultiPanel Test"); mainPanel = new JPanel(); // main panel mainPanel.setLayout(new GridLayout(3, 1)); mainPanel.add(new JLabel("Main ... Read More
3K+ Views
Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.You can declare a bound parameter just by extending the required class with the type-parameter, within the angular braces as −class Sample Example Live DemoIn the following Java example the generic class Sample restricts the type parameter to the sub classes of the Number classes using the bounded parameter.class Sample { T data; Sample(T data){ ... Read More
2K+ Views
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ... Read More