Skip to main content

Posts

Showing posts from September, 2021

Combination of nested classes and interfaces in Java

Nested classes and interfaces combinations in Java Here we discuss, 4 types of nested classes and interfaces combination with examples. Class inside a Class Interface inside a Class Interface inside an interface Class inside an interface So lets see all 4 categories one one by with examples. 1. Class inside a Class Class inside another class refer as nested class. The class written within is called the nested class, and the class that holds the inner class is called the outer class. class Outer {     class Inner {                  public void innerMethod() {             System.out.println("Class inside a Class Example");         }     }          public static void main(String[] args) {                  Outer.Inner obj = new Outer().new ...

Anonymous inner class and its types with examples

What is Anonymous inner class? When any inner class is defined without any name it is called as Anonymous inner class. Anonymous inner class also refer as name-less inner class. From 4 categories of inner classes, Anonymous inner classes is most frequently used inner class in java. Based on declaration and behavior there are 3 types of inner classes. Anonymous inner class that extends a class Anonymous inner class that implements interface Anonymous inner class that defined inside arguments So lets see above three types in details with examples : 1. Anonymous inner class that extends a class We can extends another class using anonymous inner class. like if we don't want to create class that extends another class all the time, then we can use anonymous inner class that extends a class. Lets see simple example of both extending thread using another class and creating anonymous inner class that extends a class. Example 1 : Extending Thread class using normal class class MyThread exten...

How to call inner class method from static, non static method and outside of outer class

Calling Regular inner class methods in Java In this article, we learn about 3 ways to access inner class code or methods : Call inner class method from Static method | How to instantiate non static inner class within a static method? Call inner class method from non static method of outer class Call inner class method from outside of outer class So lets see one by one with code. 1. Calling inner class method from Static method | Instantiate non static inner class within a static method? For instantiate inner class first we must have object of outer class. after that we can create object of inner class using outer class reference. class OuterClass {     class InnerClass {         public void innerClassMethod() {             System.out.println("Non Static Inner class");         }     }          public static void main(String[]...

Java Lambda Expressions HackerRank solution with Explanation

Lambda Expressions in Java What is Lambda Expression? Lambda expression is just anonymous function or we can say nameless function.  Lambda Expression in Java 8 with examples   Problem Description : Write the following methods that return a lambda expression performing a specified action: PerformOperation isOdd(): The lambda expression must return true if a number is odd or false if it is even. PerformOperation isPrime(): The lambda expression must return  if a number is prime or  if it is composite. PerformOperation isPalindrome(): The lambda expression must return  if a number is a palindrome or  if it is not. See full description on HackerRank : Java Lambda Expressions Probelm Description   Sample Input and Output : Input 1 4 2 5 3 898 1 3 2 12 Output EVEN PRIME PALINDROME ODD COMPOSITE The first integer specifies the condition to check for (1 for Odd/Even, 2 for Prime, or 3 for Palindrome). The second integer denotes the number to be checked. So l...

Varargs in Java with Examples | HackerRank Solution | Varargs in Method Overloading and Overriding

Java Varargs - Simple Addition HackerRank Solution with Explanation Problem Description : Create the class Add and the required methods so that the code prints the sum of the numbers passed to the function add .  Sample Input and Output : Input : 1 2 3 4 5 6 Output : 1+2=3 1+2+3=6 1+2+3+4+5=15 1+2+3+4+5+6=21 Lets first see what is Varargs before jump on solution. What is Varargs in Java? In Java, an argument of a method can accept arbitrary number of values. This argument can accept variable number of values is called varargs. Varargs is a short name for variable arguments. Every time we use varargs, the Java compiler creates an array to hold the given parameters. If you want to create Java method, but you are not sure how many arguments is method accept. So in that case you can use Variable arguments.  Syntax of Varargs : void methodName (datatype ...args) {     // Method body } void add (int ...args) {     // Method body } So lets jump on solu...

Java Exception Handling using Try-catch

Java Exception Handling (Try-catch) HackerRank Problem and Solution  Problem Description :  You will be given two integers x and y as input, you have to compute x/y. If x and y are not 32 bit signed integers or if y is zero, exception will occur and you have to report it. Read sample Input/Output to know what to report in case of exceptions. Learn about Exception and Exception handling : What is Exception and Exception handling in Java   Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs.  Sample Input and Output : Input 1 10 3 Output 1 3 Input 2 10 0 Output 2 java.lang.ArithmeticException: / by zero Input 3 10 string Output 3 java.util.InputMismatchException Lets see solution :  import java.io.*; import java.util.*; public class Solution {     public static void main(String[] args) {     ...

Java Exception Handling HackerRank Soution with Explanation

HackerRank Solution for Java Exception Handling Problem What is Exception Handling? The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained. Learn more about Exception types and how to handle it : Exception and Exception handling in Java   Problem Description: You are required to compute the power of a number by implementing a calculator. Create a class MyCalculator which consists of a single method long power(int, int). This method takes two integers, n and p, as parameters and finds n^p. If either n or p is negative, then the method must throw an exception which says "java.lang.Exception: n or p should not be negative.". Also, if both n and p are zero, then the method must throw an exception which says "java.lang.Exception: n and p should not be zero." . Sample Input and Output : Input 3 5 2 4 0 0 -1 -2 -1 3 Output  243 16 java.lang.Exception: n and p should not be z...