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 301 of 745
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
922 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
3K+ 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
3K+ Views
Whenever you read an integer value into a String, you can remove leading zeroes of it using StringBuffer class, using regular expressions or, by converting the given String to character array.Converting to character arrayFollowing Java program reads an integer value from the user into a String and removes the leading zeroes from it by converting the given String into Character array.Exampleimport java.util.Scanner; public class LeadingZeroes { public static String removeLeadingZeroes(String num){ int i=0; char charArray[] = num.toCharArray(); for( ; i
5K+ Views
In Java, serialization is a concept using which we can write the state of an object into a byte stream so that we can transfer it over the network (using technologies like JPA and RMI).But, static variables belong to class therefore, you cannot serialize static variables in Java. Still if you try to do so, the program gets compiled successfully but it raises an exception at the time of execution.In the following java program, the class Student has a static variable and we are trying to serialize and desterilize its object in the another class named ExampleSerialize.Exampleimport java.io.FileInputStream; import java.io.FileOutputStream; ... Read More
5K+ Views
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.Making an interface final.If you declare a class final cannot extend it. If you make a method final you cannot override it and, if you make a variable final you cannot modify it. i.e. use final with Java entities you cannot modify them further.If you make an interface final, you ... Read More
17K+ Views
No, you cannot instantiate an interface. Generally, it contains abstract methods (except default and static methods introduced in Java8), which are incomplete.Still if you try to instantiate an interface, a compile time error will be generated saying “MyInterface is abstract; cannot be instantiated”.In the following example we an interface with name MyInterface and a class with name InterfaceExample.In the interface we have an integer filed (public, static and, final) num and abstract method demo().From the class we are trying to − create an object of the interface and print the num value.Example Live Demointerface MyInterface{ public static final int num ... Read More