Posts

Showing posts from April, 2020

Constructors in Inheritance - Different scenarios explained

In this article, I will be covering how constructors work in inheritance. How Constructor invocation works in Inheritance Constructor invocation works slightly differently when inheritance is involved. When an object of the sub-class is created, it implicitly invokes the c onstructor in the base class . Once the code in the base class constructor is completed, control returns to the sub class constructor and the code is the sub-class constructor is executed. There are different scenarios in which the invocation differs slightly. Let’s explore these scenarios Scenario 1 – Base class has a constructor When the base-class has a constructor, creating an object of the sub-class results in automatic invocation of the base class constructor. Consider the following code snippet: public class Animal{public Animal() {System.out.println("In Animal constructor");}}public class Cat extends Animal{public Cat() {System.out.println("In Cat constructor");}} Here, The Animal class

Javascript Under The Hood

Javascript is a lightweight programming language. It has been around a long time. In this article, I will be giving giving a high level overview of Javascript, how it evolved and you can use it to build real world systems. History Of Javascript Javascript was initially developed to add dynamic behavior to static web pages. It was developed as a scripting language by Netscape around the year 1995. It’s interpreter was shipped along with the Netscape Navigator browser. Over the years, it was adopted by other browsers like Internet Explorer. Google also released its Chrome browser with the Javascript engine. Early usage of Javascript As mentioned earlier, Javascript was initially developed to add dynamic behavior to static web pages. The Javascript engine is shipped as part of the browser.  So it integrates with HTML and executes in the browser. It was primarily used for client side validations. So for example, if you have an HTML form that accepts a phone number, you can use client side

Java 8 DoubleBinaryOperator example

In this blog post, I will be explaining how the Java 8 DoubleBinaryOperator functional interface works. To know more about functional interfaces, you can refer  this  blog post. Edit What is DoubleBinaryOperator The DoubleBinaryOperator is a specialization of the BinaryOperator interface. To see an example of the   BinaryOperator interface, refer to  this  blog post. While the BinaryOperator interface accepts any data type, the DoubleBinaryOperator interface accepts arguments of double data type. The DoubleBinaryOperator  interface provides a method applyAsDouble . It accepts two arguments of double data type. It returns a result double data type. So it basically applies the logic in the applyAsDouble method to the input parameters and returns a double result. Code sample for DoubleBinaryOperator Consider the following code snippet: DoubleBinaryOperator multiplier = (a, b) -> a * b;double input1 = 6;double input2 = 5;System.out.println("Result of multiplying "

Spring Initializr Explained

Image
The Spring Initializr is a web-based service that can help you get started with Spring Boot easily. In this article, I will be explaining what Spring Initializr is and how you can you it. Edit What is Spring Initializr? The Spring Initializr is a web-based service provided by the makes of Spring. You can access it from https://start.spring.io/.  This helps you create a ready to use project structure for a basic Spring Boot application. You can directly use this project structure and add your code.   How to use Spring Initializr? First, you need to select the appropriate values and create the project structure. Next, you need to use the project structure in your IDE. Creating Project Structure Step 1 – Go to https://start.spring.io/ . You will see a screen as follows: Step 2 – Select values for the type of project, programming language and Spring Boot version. Here, I will be creating a Maven Project in Java with 2.3.0 version of Spring Boot. So I will select the following

JPA/Hibernate Inheritance Mapping Strategies Explained

Image
Java is inherently an object-oriented language. Databases are relational in nature. JPA/Hibernate support Inheritance strategies . These allow mapping Java class hierarchies to database tables. In this article, I will be covering inheritance strategies supported by JPA/Hibernate. Introduction JPA/Hibernate support 4 inheritance strategies, Single Table , Joined , Table Per Sub-class and MappedSuperClass . In this article, I will be covering each inheritance strategy and how it works. Sample Code Consider the following class diagram: Inheritance_strategies_class_diagram   Here Person is a super class. It has two sub-classes, Employee and Customer . Let us see how each of these strategies work for the above class hierarchy. Single Table This strategy is also known as table per hierarchy . In this strategy, JPA creates a single table corresponding to all the classes in the inheritance hierarchy. This table has columns corresponding to the fields in all the classes in the hierarch

Python String operations

In my earlier article, I covered Python numeric and String variables. Python supports several String operations. In this article, I will be demonstrating the Python String operations Introduction You can create String variables by simply assigning a String value to the variable name. The String variable can be enclosed either in single quotes or double quotes. The following code demonstrates this: a = 'Hello'b = "world" This code creates two String variables a and b How Python treats Strings Python treats Strings as a sequence of character. The first character is at position 0 and the last character is at a position which is equal to the length of the String -1 . In addition, the end of the String is considered to be at -1 position. For example consider the String “ Hello World “. In forward direction the character ‘ H ‘ is at position 0 and the character ‘ d ‘ is at position 10 . In the backward direction, the character ‘ d ‘ is at position -1 and the character

How to check if a Date is before or after another Date

In this article, I will be covering how to can check if a date is before or after another Date. You can achieve this using the java.util.Date , java.util.Calendar classes or the new Java 8 java.util.LocalDate class. Using Date class Using the java.util.Date class, there are two ways to check if a date is before or after another Date. You can either use the before/after method or the compareTo method. Using Before/After The java.util.Date class has methods before() and after() . You can use these methods to compare two dates. These methods work as follows: The before method returns true if the date on which is it invoked is before the specified date, otherwise it returns false. The after method returns true if the date on which is it invoked is after the specified date, otherwise it returns false. The following code demonstrates this: SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date date1 = simpleDateFormat.parse("2014-08-08");Date date2

Java 10 Type Inference explained with Code Samples

A new feature added by Java 10 is the Local Variable Type Inference . In this article, I will be covering this feature in detail. Introduction Before Java 10, while declaring a variable, you explicitly had to specify its type, The following code demonstrates this: int i = 10;String s = "Hello"; So this code declares an int variable i and assigns it the value 10 . It also declares a String variable s and assigns it the value “ Hello “. Java 10 introduces the Local Variable Type Inference feature. This means that Java can determine the data type of a variable based on the value assigned to the variable. So you do not need to explicitly specify the data type . Java 10 introduces a new type var . You need to specify ‘ var ‘ instead of the data type. The following code demonstrates this: var i = 6;var s = "Hello"; So this code declares the variables i and s . No data type is specified, the compiler infers the data type based on the value assigned to the variables

Spring Java-Based Configuration Example

In this article, I will be demonstrating how Spring Java-Based Configuration works. I will be using Eclipse and Maven . In order to get a basic introduction to Spring, you can check out this article. In order to understand how to configure a standalone Spring application using XML configuration, you can refer to this article. Project Set-up Step 1 – Create a new Maven Project in Eclipse. You can refer to this article on how to create a Maven project in Eclipse. Step 2 – Add Spring dependencies to the maven pom file. You can add the following: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.2.RELEASE</version> </dependency> </dependen