Found 4338 Articles for Java 8

What is the difference between getter/setter methods and constructor in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:37:17

10K+ Views

ConstructorsA constructor in Java is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have the same name as their class and, have no return type.If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.You can also accept parameters through constructors and initialize the instance variables of a class using the given values, these are known as parameterized constructors.ExampleThe following Java program has a class named student ... Read More

Is it possible to override a Java method of one class in same?

Maruthi Krishna
Updated on 10-Sep-2019 11:32:26

2K+ Views

When we have two classes where one extends another and if, these two classes have the same method including parameters and return type (say, sample) the method in the subclass overrides the method in the superclass.i.e. Since it is an inheritance. If we instantiate the subclass a copy of superclass’s members is created in the subclass object and, thus both methods are available to the object of the subclass.But if you call the method (sample), the sampling method of the subclass will be executed overriding the super class’s method.Example Live Democlass Super{    public static void sample(){       System.out.println("Method ... Read More

What happens when a subclass object is assigned to a superclass object in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:25:12

5K+ Views

Converting one data type to others in Java is known as casting.If you convert a higher datatype to lower datatype, it is known as narrowing (assigning higher data type value to the lower data type variable).char ch = (char)5;If you convert a lower data type to a higher data type, it is known as widening (assigning lower data type value to the higher data type variable).Int i = 'c';Similarly, you can also cast/convert an object of one class type to others. But these two classes should be in an inheritance relation. Then, If you convert a Super class to subclass ... Read More

When overriding clone method, why do we need to declare it as public in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:19:07

1K+ Views

The clone() method belongs to the class named Object of the java.lang package, it accepts an object as a parameter creates and returns a copy of it.In order to use this method, you need to make sure that your class implements the Cloneable (marker) interface.Example Live Demopublic class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);       System.out.println("Age : "+this.age);    }    public static ... Read More

How to override only few methods of interface in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:10:33

6K+ Views

Once you implement an interface from a concrete class you need to provide implementation to all its methods. If you try to skip implementing methods of an interface at compile time an error would be generated.Example Live Demointerface MyInterface{    public void sample();    public void display(); } public class InterfaceExample implements MyInterface{    public void sample(){       System.out.println("Implementation of the sample method");    }    public static void main(String args[]) {       InterfaceExample obj = new InterfaceExample();       obj.sample();    } }Compile-time errorInterfaceExample.java:5: error: InterfaceExample is not abstract and does not override abstract method ... Read More

Can we overload methods of an interface in Java?

Maruthi Krishna
Updated on 10-Sep-2019 11:04:47

6K+ 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

What happens if we overload default methods of an interface in java?

Maruthi Krishna
Updated on 10-Sep-2019 10:59:13

1K+ 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

How to hide unsupported interface methods from class in Java?

Maruthi Krishna
Updated on 10-Sep-2019 10:49:39

813 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

Can we change the access specifier from (public) while implementing methods from interface in Java?

Maruthi Krishna
Updated on 10-Sep-2019 10:43:28

504 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

Can we synchronize abstract methods in Java?

Maruthi Krishna
Updated on 10-Sep-2019 10:36:50

681 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

Advertisements