Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 14 of 50

Does java support hybrid inheritance?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 3K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members are created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Exampleclass Super{    int a =100;    int b = 200;    public void ...

Read More

Can we extend interfaces in Java? Explain?

Maruthi Krishna
Maruthi Krishna
Updated on 30-Jun-2020 1K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Just like classes you can extend one interface from another using the extends keyword as shown below −interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations extends ArithmeticCalculations{    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }But, when you implement the sub-class you need to provide body for the abstract methods in both interfaces.ExampleIn the following example we have created two interfaces ...

Read More

Can we write an interface without any methods in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 3K+ Views

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method. Live Demoimport java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public ...

Read More

What is a remote interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 4K+ Views

A Remote interface is available in the java.rmi package it is a marking/tagging interface, it is used with remote method invocation(RMI).RMI is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.To it is a marking interface, to mark an object of a class remote, you need to implement this interface.To create a remote interface −Create an interface that extends the predefined interface Remote which belongs to the package or, implement the Remote interface with the class, which you need to make remote.Declare all the business methods that can be invoked ...

Read More

What happens if the subclass does not override abstract methods in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 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

Can we declare the variables of a Java interface private and protected?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 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

Can we implement one interface from another in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 462 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

How to write a class inside an interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 387 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
Maruthi Krishna
Updated on 29-Jun-2020 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

Accessing variables of two interfaces, which are same from an implementing class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 29-Jun-2020 980 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
Showing 131–140 of 500 articles
« Prev 1 12 13 14 15 16 50 Next »
Advertisements