Avoid Starting Thread Twice in Android

Smita Kapse
Updated on 29-Jun-2020 14:36:41

518 Views

Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to avoid thread start twice in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             In the above code, we have taken edittext and textview. When user enter some text into edittext, it going to wait ... Read More

Implement One Interface from Another in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:35:20

425 Views

No, we cannot implement one interface from another you can just extend it using the extends keyword as −interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations implements ArithmeticCalculations{    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }Still, if you try to implement one interface from another using the implements keyword. The compiler does not recognize the implements keyword after the name of the interface and throws a compile time error saying “'{' expected”.ExampleIn the following Java program, we have two interfaces ... Read More

Write a Class Inside an Interface in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:34:08

355 Views

Defining a class within an interface is allowed in Java. If the methods of an interface accept a class as an argument and the class is not used elsewhere, in such cases we can define a class inside an interface.ExampleIn the following example we have an interface with name CarRentalServices and this interface has two methods that accepts an object of the class Car as an argument. Within this interface we have the class Car. Live Demointerface CarRentalServices {    void lendCar(Car c);    void collectCar(Car c);    public class Car{       int carId;       String carModel; ... Read More

Can We Declare an Interface as Final in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:33:25

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

What Happens If a Class Does Not Implement All Abstract Methods of an Interface in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:31:01

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

Access Variables of Two Same Interfaces in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:29:24

906 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

Abstract Method Declaration with Throws Clause in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:28:41

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

Can Abstract Methods of an Interface Throw an Exception in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:27:42

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

Change Exception from Unchecked to Checked in Java Method Override

Maruthi Krishna
Updated on 29-Jun-2020 14:23:57

562 Views

A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Unchecked to checkedWhen a method in superclass throws an unchecked exception the method the subclass method overriding cannot throw ... Read More

Guidelines for Overriding Methods that Throw Exceptions in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:22:28

751 Views

While a superclass method throws an exception while overriding it you need to follow the certain rules.Should throw Same exception or, sub typeIf the super-class method throws certain exception, the method in the sub-class should throw the same exception or its sub type.ExampleIn the following example, the readFile() method of the super-class throws an IOEXception and, the readFile() method of the sub-class throws FileNotFoundException exception.Since the FileNotFoundException exception is the sub type of the IOException this program gets compiled and executed without any errors.import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super{    public String readFile(String path) throws ... Read More

Advertisements