
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
Found 9150 Articles for Object Oriented Programming

4K+ Views
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Private fields of an interfaceIf the fields of the interface are private, you cannot access them in the implementing class.If you try to declare the fields of an interface private, a compile time error is generated saying “modifier private not allowed here”.ExampleIn the following Java example, we are trying to declare the field and method of an interface private.public interface MyInterface{ private static final int num = 10; private abstract void demo(); }Compile time errorOn compiling, the above program ... Read More

5K+ Views
The GUI in Java processes the interactions with users via mouse, keyboard, and various user controls such as buttons, checkboxes, text fields, etc., as events. These events are to be handled properly to implement Java as an Event-Driven Programming. What is Event Handling? Event handling refers to the mechanism that controls events and determines the actions taken when an event occurs. This mechanism includes code known as an event handler, which is executed in response to an event. Components in Event Handling The following are the three main components of event handling in Java: Events ... Read More

525 Views
In Java classes and interfaces related to each other are grouped under a package. Package is nothing but a directory storing classes and interfaces of a particular concept. For example, all the classes and interfaces related to input and output operations are stored in java.io package.There are two types of packages namely user-defined packages and built-in packages (pre-defined)The import keywordWhenever you need to use the classes from a particular package −First of all, you need to set class path for the JAR file holding the required package.Import the required class from the package using the import keyword. While importing you ... Read More

5K+ Views
Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) as listed below −boolean − Stores 1-bit value representing true or, false.byte − Stores twos compliment integer up to 8 bits.char − Stores a Unicode character value up to 16 bits.short − Stores an integer value upto 16 bits.int − Stores an integer value upto 32 bits.long − Stores an integer value upto 64 bits.float − Stores a floating point value upto 32bits.double − Stores a floating point value up to 64 bits.Converting one primitive data type into another is known as type ... Read More

6K+ Views
A method which does not have body is known as abstract method. It contains only 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 to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Extending an abstract classOnce you extend an abstract class in Java you need to override all the abstractmethods in it or, declare it abstract. If you don’t, a ... Read More

2K+ Views
Yes, the abstract methods of an interface can throw an exception.ExampleIn the following example the interface (MyInterface) contains an abstract method with name display, which throws an IOException.import java.io.IOException; abstract interface MyInterface { public abstract void display()throws IOException ; }Rules to be followedYou need to follow the rules given below while implementing such method −If the abstract method in the interface throws certain exception. The implemented method can throw the same exception as shown below −Example 1 Live Demoimport java.io.IOException; abstract interface MyInterface { public abstract void display()throws IOException ; } public class InterfaceExample implements MyInterface{ public void ... Read More

2K+ Views
A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();Yes, you can throw and exception from an abstract class.ExampleIn the following Java program, we have a two classes: one abstract class with name MyClass which have an abstract method (display()) and, another class that extends the abstract class.Here, the display() method abstract class throws an IOException.Example Live Demoimport java.io.IOException; abstract class MyClass { public abstract void display() throws IOException; } public class AbstractClassExample extends MyClass{ public void display() throws IOException ... Read More

904 Views
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.You can implement multiple interfaces using a single class in Java. Whenever both interfaces have same name, since all the fields of an interface are static by default, you can access them using the name of the interface as −Exampleinterface MyInterface1{ public static int num = 100; public void display(); } interface MyInterface2{ public static int num = 1000; public void show(); } public class InterfaceExample implements MyInterface1, MyInterface2{ public static int num = 10000; ... Read More

2K+ Views
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.In a separate class you need to implement this interface and provide body for all its abstract methods.Once you implement an interface using a class, you must provide body (implement) to all of its abstract methods or, should declare the class as abstract. If you don’t a compile time error will be generated for each unimplemented method saying “InterfaceExample is not abstract and does not override abstract method method_name in interface_name”.In the following java program, we have an interface with name ... Read More

8K+ Views
Interfaces in Java are used for defining a contract that classes can implement. They can contain method signatures, default methods, static methods, and constants. we must implement the methods defined in the interface in the implementing class. Can we define a constructor inside an interface in Java? No, constructors cannot be defined inside an interface in Java. Constructors are special methods that are used to initialize objects of a class, and since interfaces cannot be instantiated, they do not have constructors. Why can constructors not be defined in an interface? There are several reasons why constructors cannot be defined ... Read More