Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 45 of 50

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

Maruthi Krishna
Maruthi Krishna
Updated on 10-Sep-2019 713 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
Maruthi Krishna
Updated on 10-Sep-2019 1K+ 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

How can we restrict Generics (type parameter) to sub classes of a particular class in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 3K+ Views

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.You can declare a bound parameter just by extending the required class with the type-parameter, within the angular braces as −class Sample Example Live DemoIn the following Java example the generic class Sample restricts the type parameter to the sub classes of the Number classes using the bounded parameter.class Sample {    T data;    Sample(T data){ ...

Read More

Can we have generic constructors in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 2K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ...

Read More

What is need of introducing Generics, when Arrays already present in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 129 Views

Arrays in Java are used to store homogeneous datatypes, where generics allow users to dynamically choose the type (class) that a method, constructor of a class accepts, dynamically.By defining a class generic you are making it type-safe i.e. it can act up on any datatype. To understand generics let us consider an example −Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample {    public static void main(String args[]) {       Student ...

Read More

Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 750 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.To define a generic class you need to specify the type parameter you are using in the angle brackets “” after the class name and you can treat this as datatype of the instance variable an ...

Read More

Can you create an array of Generics type in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 1K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ...

Read More

How to check if an URL is valid or not using Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 3K+ Views

The URL class of the java.net package represents a Uniform Resource Locator which is used to point a resource(file or, directory or a reference) in the worldwide web.This class provides various constructors one of them accepts a String parameter and constructs an object of the URL class. While passing URL to this method if you used an unknown protocol or haven’t specified any protocol this method throws a MalformedURLException.Similarly, the toURI() method of this class returns an URI object of the current URL. If the current URL is not properly formatted or, syntactically incorrect according to RFC 2396 this method throws ...

Read More

Are Generics applied at compile time or run time?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 741 Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student{    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ...

Read More

Is it possible to instantiate Type-parameter <T> in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Sep-2019 5K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Example Live Democlass Student {    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample ...

Read More
Showing 441–450 of 500 articles
« Prev 1 43 44 45 46 47 50 Next »
Advertisements