Rishi Raj has Published 131 Articles

Using run-time polymorphism in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

281 Views

A single action can be performed in multiple ways using the concept of polymorphism. Run-time polymorphism can be performed by method overriding. The overridden method in this is resolved at compile time.A program that demonstrates run-time polymorphism in Java is given as follows:Example Live Democlass Animal {    void sound() { ... Read More

Why do we use import statement in Java? Where it should be included in the class?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

3K+ Views

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.A program that demonstrates this in Java ... Read More

Removing Single Elements in a Vector in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

75 Views

A single element in the Vector can be removed using the java.util.Vector.remove() method. This method removes the element at the specified index in the Vector. It returns the element that was removed. Also after the element is removed from the specified index, the rest of the Vector elements are shifted ... Read More

Recursive factorial method in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

4K+ Views

The factorial of any non-negative integer is basically the product of all the integers that are smaller than or equal to it. The factorial can be obtained using a recursive method.A program that demonstrates this is given as follows:Example Live Demopublic class Demo {    public static long fact(long n) { ... Read More

Replace all the elements of a Vector with Collections.fill() in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

434 Views

All the elements of a vector can be replaced by a specific element using java.util.Collections.fill() method. This method requires two parameters i.e. the Vector and the element that replaces all the elements in the Vector. No value is returned by the Collections.fill() method.A program that demonstrates this is given as ... Read More

Why Protected access modifier used in Java?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

157 Views

The data members and methods of a class can be accessed from the same package or sub-classes in a different package if they are specified with the protected access modifier. The keyword protected is used to specify this modifier.A program that demonstrates the protected access modifier in Java is given ... Read More

Why Abstract Class is used in Java?

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

654 Views

A class is an abstract class if it contains at least one abstract method. It can contain other non-abstract methods as well. A class can be declared as abstract by using the abstract keyword. Also, an abstract class cannot be instantiated.A program that demonstrates an abstract class in Java is ... Read More

Sort a List in reverse order in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:24

7K+ Views

A list can be sorted in reverse order i.e. descending order using the java.util.Collections.sort() method. This method requires two parameters i.e. the list to be sorted and the Collections.reverseOrder() that reverses the order of an element collection using a Comparator.The ClassCastException is thrown by the Collections.sort() method if there are ... Read More

Send email using Java Program

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

423 Views

To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API and Java Activation Framework (JAF) installed on your machine. You can download latest version of JavaMail (Version 1.2) from Java's standard website. You can download latest version ... Read More

Sorting a HashMap according to values in Java

Rishi Raj

Rishi Raj

Updated on 30-Jul-2019 22:30:23

473 Views

As we know that Hash map in Java does not maintain insertion order either by key or by order.Also it does not maintain any other order while adding entries to it. Now in order to sort a hash map according to the values mapped to its corresponding keys we first ... Read More

Advertisements