December 31, 2022

ZonedDateTime in Java With Examples

The java.time.ZonedDateTime class is part of new date and time API added in Java 8 that represents a date-time with a time-zone in the ISO-8601 calendar system, such as 2019-11-02T09:27:07+05:30[Asia/Calcutta].

Java ZonedDateTime class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset. This class is immutable thus thread-safe. Since it is marked as final so can't be extended. In this post we’ll see some examples demonstrating usage of Java ZonedDateTime class.

Creating instances of ZonedDateTime

ZonedDateTime class in Java doesn't have any public constructors to obtain an instance, you will use a factory method to get an instance.

1. Using now() method you can obtain the current date-time from the system clock in the default time-zone.

ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt);//2019-11-02T09:27:07.083270100+05:30[Asia/Calcutta]

You can also pass a ZoneId to obtain the current date-time from the system clock in the specified time-zone.

ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(zdt);//2019-11-02T05:05:31.583917800+01:00[Europe/Paris]

2. You can obtain an instance of ZonedDateTime using of() method by passing a year, month, day, hour, minute, second, nanosecond and time-zone.

ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
System.out.println(zdt);//2019-10-25T15:10:21.000000252-05:00[America/Chicago]
You can also pass instances of LocalDate, LocalTime and ZoneId to get a ZonedDateTime
of(LocalDate date, LocalTime time, ZoneId zone)

You can also pass instance of LocalDateTime and ZoneId to get a ZonedDateTime

of(LocalDateTime localDateTime, ZoneId zone)

Formatting ZonedDateTime (Converting to String)

For converting ZonedDateTime to String you can use DateTimeFormatter class which specifies the pattern for conversion.

public class FormatDate {
  public static void main(String[] args) {	
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    // Z - Time Zone offset
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a Z");
    System.out.println(zdt.format(dtf));
    
    // z - time zone name
    dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss z");
    System.out.println(zdt.format(dtf));
    
    //V - time-zone ID
    dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss VV");
    System.out.println(zdt.format(dtf)); 
  }
}
Output
2019-10-25 03:10:21 PM -0500
10/25/2019 15:10:21 GMT-05:00
10/25/2019 - 15:10:21 America/Chicago

Converting String to ZonedDateTime

Using parse() method you can convert String to ZonedDateTime.

public class FormatDate {
  public static void main(String[] args) {	
    String dateWithZone = "10/25/2019 15:10:21 GMT-05:00";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss z");
    ZonedDateTime zdt = ZonedDateTime.parse(dateWithZone, dtf);
    System.out.println(zdt); 
  }
}
Output
2019-10-25T15:10:21-05:00[GMT-05:00]

Getting date, time and zone values from ZonedDateTime

ZonedDateTime class has methods to get year, month, day, hour, minute, second values as well as zone information.

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    
    System.out.println("Year- " + zdt.getYear());
    System.out.println("Month- " + zdt.getMonthValue());
    System.out.println("Day- " + zdt.getDayOfMonth());
    
    System.out.println("Hour- " + zdt.getHour());
    System.out.println("Minute- " + zdt.getMinute());
    System.out.println("Second- " + zdt.getSecond());
    System.out.println("NanoSecond- " + zdt.getNano());
    
    System.out.println("Zone- " + zdt.getZone()); 
  }
}
Output
Year- 2019
Month- 10
Day- 25
Hour- 15
Minute- 10
Second- 21
NanoSecond- 252
Zone- America/Chicago

Adding or subtracting years, months, days to ZonedDateTime

There are methods to add or subtract days, months and years from a Java ZonedDateTime.

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time: " + zdt); 

    System.out.println("Year after subtraction- " + zdt.minusYears(2));
    System.out.println("Month after subtraction- " + zdt.minusMonths(4));
    System.out.println("Day after subtraction- " + zdt.minusDays(35));

    System.out.println("Year after addition- " + zdt.plusYears(2));
    System.out.println("Month after addition- " + zdt.plusMonths(4));
    System.out.println("Day after addition- " + zdt.plusDays(35));
  }
}
Output
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Year after subtraction- 2017-10-25T15:10:21.000000252-05:00[America/Chicago]
Month after subtraction- 2019-06-25T15:10:21.000000252-05:00[America/Chicago]
Day after subtraction- 2019-09-20T15:10:21.000000252-05:00[America/Chicago]
Year after addition- 2021-10-25T15:10:21.000000252-05:00[America/Chicago]
Month after addition- 2020-02-25T15:10:21.000000252-06:00[America/Chicago]
Day after addition- 2019-11-29T15:10:21.000000252-06:00[America/Chicago]

Adding or subtracting hours, minute, second to ZonedDateTime

There are methods to add or subtract hours, minutes, seconds, nano seconds from a ZonedDateTime.

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time: " + zdt); 

    System.out.println("Hour after subtraction- " + zdt.minusHours(2));
    System.out.println("Minute after subtraction- " + zdt.minusMinutes(25));
    System.out.println("Second after subtraction- " + zdt.minusSeconds(35));

    System.out.println("Hour after addition- " + zdt.plusHours(2));
    System.out.println("Minute after addition- " + zdt.plusMinutes(4));
    System.out.println("Second after addition- " + zdt.plusSeconds(35));
    System.out.println("NanoSecond after addition- " + zdt.plusNanos(250));
  }
}
Output
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Hour after subtraction- 2019-10-25T13:10:21.000000252-05:00[America/Chicago]
Minute after subtraction- 2019-10-25T14:45:21.000000252-05:00[America/Chicago]
Second after subtraction- 2019-10-25T15:09:46.000000252-05:00[America/Chicago]
Hour after addition- 2019-10-25T17:10:21.000000252-05:00[America/Chicago]
Minute after addition- 2019-10-25T15:14:21.000000252-05:00[America/Chicago]
Second after addition- 2019-10-25T15:10:56.000000252-05:00[America/Chicago]
NanoSecond after addition- 2019-10-25T15:10:21.000000502-05:00[America/Chicago]

Getting LocalDateTime, LocalDate, LocalTime from ZonedDateTime

public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time: " + zdt); 

    LocalDateTime ldt = zdt.toLocalDateTime();
    System.out.println("Extracted LocalDateTime: " + ldt); 

    LocalDate ld = zdt.toLocalDate();
    System.out.println("Extracted LocalDate: " + ld);

    LocalTime lt = zdt.toLocalTime();
    System.out.println("Extracted LocalTime: " + lt); 
  }
}
Output
Created Zoned Date-Time: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Extracted LocalDateTime: 2019-10-25T15:10:21.000000252
Extracted LocalDate: 2019-10-25
Extracted LocalTime: 15:10:21.000000252

Comparing ZonedDateTimes in Java

For comparing two ZonedDateTime instances there are the following methods-

  • compareTo(ChronoLocalDateTime<?> other)- Compares this date-time to another date-time. Returns negative value if less than tha passed LocalDateTime instance, positive if greater.
  • isAfter(ChronoLocalDateTime<?> other)- Checks if this date-time is after the specified date-time.
  • isBefore(ChronoLocalDateTime<?> other)- Checks if this date-time is before the specified date-time.
  • isEqual(ChronoLocalDateTime<?> other)- Checks if this date-time is equal to the specified date-time.
public class FormatDate {
  public static void main(String[] args) {			
    ZonedDateTime zdt1 = ZonedDateTime.of(2019, 10, 25, 15, 10, 21, 252, ZoneId.of("America/Chicago"));
    ZonedDateTime zdt2 = ZonedDateTime.of(2018, 8, 5, 4, 15, 21, 252, ZoneId.of("America/Chicago"));
    System.out.println("Created Zoned Date-Time1: " + zdt1); 
    System.out.println("Created Zoned Date-Time2: " + zdt2); 


    System.out.println(zdt1.compareTo(zdt2));
    System.out.println(zdt2.compareTo(zdt1));

    System.out.println(zdt1.isAfter(zdt2));
    System.out.println(zdt1.isBefore(zdt2));
    System.out.println(zdt1.isEqual(zdt2));
  }
}
Output
Created Zoned Date-Time1: 2019-10-25T15:10:21.000000252-05:00[America/Chicago]
Created Zoned Date-Time2: 2018-08-05T04:15:21.000000252-05:00[America/Chicago]
1
-1
true
false
false

That's all for the topic ZonedDateTime in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 30, 2022

Instant in Java With Examples

The java.time.Instant class is part of new date and time API added in Java 8 that represents a single instantaneous point on the time-line.

Instant is stored in two fields, it stores a long representing epoch-seconds and an int representing nanosecond-of-second, which will always be between 0 and 999,999,999. The epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z where instants after the epoch have positive values, and earlier instants have negative values. To initialize these two field there is a private constructor in the Java Instant class-

/**
 * @param epochSecond  the number of seconds from 1970-01-01T00:00:00Z
 * @param nanos  the nanoseconds within the second, must be positive
 */
private Instant(long epochSecond, int nanos) {
  super();
  this.seconds = epochSecond;
  this.nanos = nanos;
}

Instant class is immutable thus thread-safe. Since it is marked as final so can't be extended.

Creating instances of Instant

Instant class doesn't have any public constructors to obtain an instance, you will use a factory method to get an instance.

  1. now()- Obtains the current instant from the system clock.
    Instant instant = Instant.now();
    System.out.println(instant); //2019-11-07T05:21:04.464715600Z
  2. now(Clock clock)- Obtains the current instant from the specified clock.
    Instant instant = Instant.now(Clock.systemDefaultZone());
    System.out.println(instant); //2019-11-07T05:36:01.210573Z
  3. ofEpochMilli(long epochMilli)- Obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.
    Instant instant = Instant.ofEpochMilli(System.currentTimeMillis());
    System.out.println(instant); //2019-11-07T05:39:27.853Z
  4. ofEpochSecond(long epochSecond)- Obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z.
    Instant instant = Instant.ofEpochSecond(System.currentTimeMillis()/1000);
    System.out.println(instant); //2019-11-07T05:39:27.853Z
  5. ofEpochSecond(long epochSecond, long nanoAdjustment)- Obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
    Instant instant = Instant.ofEpochSecond(System.currentTimeMillis()/1000, 235);
    System.out.println(instant); //2019-11-07T05:40:55.000000235Z
  6. parse(CharSequence text)- Obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z. Using parse method you can convert String to an Instant.
    Instant instant = Instant.parse("2019-10-28T11:35:15Z");
    System.out.println(instant); //2019-10-28T11:35:15Z

Getting epoch seconds and nano second values from Instant

Since Instant instance is stored in two fields; epochSecond and nanos so there are methods to extract these two fields from a java.time.Instant instance.

public class InsantExample {
  public static void main(String[] args) {
    Instant instant = Instant.parse("2019-10-28T11:35:15.245Z");
    // epoch seconds
    System.out.println(instant.getEpochSecond());
    // Nanos
    System.out.println(instant.getNano());
  }
}
Output
1572262515
245000000

Time calculations using Instant

There are methods to add or subtract date and time values from an Instant.

Minus methods
  • minus(long amountToSubtract, TemporalUnit unit)- Returns a copy of this instant with the specified amount subtracted.
  • minus(TemporalAmount amountToSubtract)- Returns a copy of this instant with the specified amount subtracted.
  • minusMillis(long millisToSubtract)- Returns a copy of this instant with the specified duration in milliseconds subtracted.
  • minusNanos(long nanosToSubtract)- Returns a copy of this instant with the specified duration in nanoseconds subtracted.
  • minusSeconds(long secondsToSubtract)- Returns a copy of this instant with the specified duration in seconds subtracted.
public class InsantExample {
  public static void main(String[] args) {
    Instant instant = Instant.parse("2019-10-28T11:35:15.245Z");
    System.out.println("Instant- " + instant);
    
    System.out.println("Instant second subtraction- " + instant.minusSeconds(15));
    System.out.println("Instant millis subtraction- " + instant.minusMillis(2000));
    System.out.println("Instant nanos subtraction- " + instant.minusNanos(45));
    
    // Uses minus(long amountToSubtract, TemporalUnit unit)
    System.out.println("Instant days subtraction- " + instant.minus(10, ChronoUnit.DAYS));
    // Uses minus(TemporalAmount amountToSubtract)
    System.out.println("Instant days subtraction- " + instant.minus(Period.ofDays(10)));
    System.out.println("Instant Hours subtraction- " + instant.minus(Duration.ofHours(3)));
  }
}
Output
Instant- 2019-10-28T11:35:15.245Z
Instant second subtraction- 2019-10-28T11:35:00.245Z
Instant millis subtraction- 2019-10-28T11:35:13.245Z
Instant nanos subtraction- 2019-10-28T11:35:15.244999955Z
Instant days subtraction- 2019-10-18T11:35:15.245Z
Instant days subtraction- 2019-10-18T11:35:15.245Z
Instant days subtraction- 2019-10-28T08:35:15.245Z
Plus methods
  • plus(long amountToAdd, TemporalUnit unit)- Returns a copy of this instant with the specified amount added.
  • plus(TemporalAmount amountToAdd)- Returns a copy of this instant with the specified amount added.
  • plusMillis(long millisToAdd)- Returns a copy of this instant with the specified duration in milliseconds added.
  • plusNanos(long nanosToAdd)- Returns a copy of this instant with the specified duration in nanoseconds added.
  • plusSeconds(long secondsToAdd)- Returns a copy of this instant with the specified duration in seconds added.
public class InsantExample {
  public static void main(String[] args) {
    Instant instant = Instant.parse("2019-10-28T11:35:15.245Z");
    System.out.println("Instant- " + instant);
    
    System.out.println("Instant second addition- " + instant.plusSeconds(15));
    System.out.println("Instant millis addition- " + instant.plusMillis(2000));
    System.out.println("Instant nanos addition- " + instant.plusNanos(45));
    
    // Uses plus(long amountToAdd, TemporalUnit unit)
    System.out.println("Instant days addition- " + instant.plus(10, ChronoUnit.DAYS));
    // Uses plus(TemporalAmount amountToAdd)
    System.out.println("Instant days addition- " + instant.plus(Period.ofDays(10)));
    System.out.println("Instant Hours addition- " + instant.plus(Duration.ofHours(3)));
  }
}
Output
Instant- 2019-10-28T11:35:15.245Z
Instant second addition- 2019-10-28T11:35:30.245Z
Instant millis addition- 2019-10-28T11:35:17.245Z
Instant nanos addition- 2019-10-28T11:35:15.245000045Z
Instant days addition- 2019-11-07T11:35:15.245Z
Instant days addition- 2019-11-07T11:35:15.245Z
Instant Hours addition- 2019-10-28T14:35:15.245Z

Comparing two Instant Instances in Java

  • compareTo(Instant otherInstant)- Compares this instant to the specified instant. Returns negative if less than the passes Instant instance, positive otherwise.
  • equals(Object otherInstant)- Checks if this instant is equal to the specified instant. Returns true if the other instant is equal to this one.
  • isAfter(Instant otherInstant)- Checks if this instant is after the specified instant.
  • isBefore(Instant otherInstant)- Checks if this instant is before the specified instant.
public class InsantExample {
  public static void main(String[] args) {
    Instant instant1 = Instant.parse("2019-10-28T11:35:15.245Z");
    System.out.println("Instant1- " + instant1);
    
    Instant instant2 = Instant.parse("2019-09-22T16:25:10.245Z");
    System.out.println("Instant2- " + instant2);
    
    // Should return + value
    System.out.println("Instant comparison- " + instant1.compareTo(instant2));
    System.out.println("Instanct Instances equal- " + instant1.equals(instant2));
    System.out.println("Instant After- " + instant1.isAfter(instant2));
    System.out.println("Instant Before- " + instant1.isBefore(instant2));
  }
}
Output
Instant1- 2019-10-28T11:35:15.245Z
Instant2- 2019-09-22T16:25:10.245Z
Instant comparison- 1
Instanct Instances equal- false
Instant After- true
Instant Before- false

That's all for the topic Instant in Java With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 14, 2022

issubclass() in Python With Examples

The Python issubclass() is a built-in function that checks whether the passed class is a subclass of the specified another class or not.

Syntax of the Python issubclass() is as given below-

issubclass(class, classinfo)

Function returns True if class is a subclass (direct, indirect or virtual) of classinfo.

classinfo may be a class or a tuple of class objects, if it is a tuple of class objects then class (first argument) is checked against each entry in the tuple.

Python issubclass() examples

1. As obvious you will be using issubclass() function when you are using inheritance in Python. Here is a hierarchical structure where Employee class inherits from Person and Manager class inherits from Employee, so we have Multi-level inheritance scenario here. Let’s check different combinations using issubclass().

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def display_info(self):
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, emp_id, department, name, age):
    super().__init__(name, age)
    self.emp_id = emp_id
    self.department = department

  def display_info(self):
    super().display_info()
    print('Id:', self.emp_id)
    print('Department:', self.department)


class Manager(Employee):
  def __init__(self, perks, emp_id, department, name, age):
    super().__init__(emp_id, department, name, age)
    self.perks = perks

  def display_info(self):
    super().display_info()
    print('Perks:', self.perks)


print("issubclass(Employee, Person):", issubclass(Employee, Person))
print("issubclass(Manager, Person):", issubclass(Manager, Person))
print("issubclass(Manager, Employee):", issubclass(Manager, Employee))
print("issubclass(Employee, Manager):", issubclass(Employee, Manager))
print("issubclass(Person, Employee):", issubclass(Person, Employee))
Output
issubclass(Employee, Person): True
issubclass(Manager, Person): True
issubclass(Manager, Employee): True
issubclass(Employee, Manager): False
issubclass(Person, Employee): False

Manager indirectly inherits from Person too so it is of type Person also that’s why issubclass(Manager, Person) returns true. Vice-versa is not true though Manager is not subclass of Employee, Person is not a sub-class of Employee too so both these function calls return false.

2. In this example we’ll pass a tuple as second argument in issubclass() function.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def display_info(self):
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, emp_id, department, name, age):
    super().__init__(name, age)
    self.emp_id = emp_id
    self.department = department

  def display_info(self):
    super().display_info()
    print('Id:', self.emp_id)
    print('Department:', self.department)


class Manager(Employee):
  def __init__(self, perks, emp_id, department, name, age):
    super().__init__(emp_id, department, name, age)
    self.perks = perks

  def display_info(self):
    super().display_info()
    print('Perks:', self.perks)


print("issubclass(Employee, (str, list))):", issubclass(Employee, (str, list)))
print("issubclass(Employee, (str, list, Person)):", issubclass(Employee, (str, list, Person)))
print("issubclass(Manager, (str, list)):", issubclass(Manager, Employee))
print("issubclass(Person, (Employee, Manager)):", issubclass(Person, (Employee, Manager)))
print("issubclass(Person, (Employee, Manager, object)):", issubclass(Person, (Employee, Manager, object)))
Output
issubclass(Employee, (str, list))): False
issubclass(Employee, (str, list, Person)): True
issubclass(Manager, (str, list)): True
issubclass(Person, (Employee, Manager)): False
issubclass(Person, (Employee, Manager, object)): True

First call obviously returns false as Employee is not a subclass of either str or list.

Second is true because Employee is subclass of Person and one of the entry in the tuple passes the check.

Last call issubclass(Person, (Employee, Manager, object)) returns true because object is the super class of all classes in Python.

That's all for the topic issubclass() in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 13, 2022

React Example - Remove Object From an Array of Objects

If you want to remove an object from an array of objects in your React application best option is to use filter method which filters out the item that has to be removed and returns a new array containing rest of the elements. Since a new array is returned by filter method so you don't mutate the state rather you set state to this new array.

Remove object from an array of objects using filter

With in the filter method, provide a function that runs for each array element. With in that function, you'll have to write the condition, each array element is tested against that condition, if it passes the condition it is added to the new array otherwise not.

As an example, let's have a shopping cart with certain products added. With each listed product there is also a delete button to remove that product from the list.

There are two components; Products and ProductItem

Products.js

import { Fragment, useState } from "react";
import ProductItem from "./ProductItem";
const productList = [
    { id: 1, name: 'Laptop', price: 455.50 },
    { id: 2, name: 'Mouse', price: 15.89 },
    { id: 3, name: 'USB', price: 10.00 },
    { id: 4, name: 'HDD', price: 55.50 },
  ];
const Products = (props) =>{
    const [products, setProducts] = useState(productList);
    const removeProduct = (id) => {
        setProducts(products.filter((product) => product.id !== id));
    }
    return(
        <Fragment>
            <h2>Product List</h2>
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>
                {
                    products.map((product) => (
                        <ProductItem key={product.id} id={product.id} 
                        name={product.name} price={product.price} onRemoveHandler={removeProduct}/>                  
                    ))
                }
                </tbody>
            </table>
        </Fragment>
    );
}

export default Products;
Important points to note in the code are-
  1. Array is initialized with some product objects. This array is then set as the initial state with in the useState hook.
  2. Array is iterated using map method, in each iteration <ProductItem> component is called with some props data.
  3. removeProduct function gets the id of the product which has to be removed and uses filter method to filter out the specific product. New array created by filter method is then pass to the setProducts() method to update state.

ProductItem.js

This component renders each product as one table row and a delete button with each product.

const ProductItem = (props) => {
    const onClickHandler = () => {
        props.onRemoveHandler(props.id);
    }
    return (
        <tr>
            <td>{props.name}</td>
            <td>{props.price}</td>
            <td><button onClick={onClickHandler}>Delete</button></td>
        </tr>
    )
}

export default ProductItem;

That's all for the topic React Example - Remove Object From an Array of Objects. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 12, 2022

super() in Python With Examples

In this tutorial we’ll see what is super() function in Python and how to use it.

Python super() function

super() is a built-in function in Python which returns a proxy object that can be used to delegate method calls (including call to constructor) to immediate parent class from the child class.

Syntax of super()

Syntax of super() in Python is as following-

super([type[, object-or-type ]])

Suppose you have a child class C inheriting from base class B defined as given below-

class C(B):
  def method(self, arg):
    super().method(arg)

In the method there is a call to method which is defined in the super class using the super() function. Since both of the parameters are optional so this call super().method(arg) is equivalent to super(C, self).method(arg) where both of the parameters are passed.

Use of super()

1. Since super() is a proxy object (indirect reference) to the super class so rather than using the super class name directly you can use super() to call super class method. If we take the following class structure

class C(B):
  def method(self, arg):
    super().method(arg)

Here you could have also called method of the parent class by using this statement-

B.method(arg)

Using super() sub-class is not tightly bound to the same super class. In case you later want to change the class class C inherits from, super() will point to the new super class.

class C(A):
  def method(self, arg):
    super().method(arg)

Now super().method(arg) calls the method of the class A.

2. Usage of super() also helps in reducing the code redundancy. See the example to know more.

Python example using super()

In the example there is a class called Person that acts as a base class and another class Employee that inherits from Person class. Employee class also adds properties of its own and also overrides the parent class method.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def display_info(self):
    print('In display_info method of Person class')
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, person_id, department, name, age):
    self.name = name
    self.age = age
    self.person_id = person_id
    self.department = department

  def display_info(self):
    print('In display_info method of Employee class')
    print('Id:', self.person_id)
    print('Name:', self.name)
    print('Age:', self.age)
    print('Department:', self.department)

In the child class Employee you can notice the redundancy of initializing the parent class’ fields in the constructor though there is a constructor in the parent class which is already doing that. Same way in the display_info () method we have print statement to print name and age too though there is a method in Person class which is already doing that.

Here we can use super() to call constructor of the parent class to initialize fields that are there in the parent class. Using super() we can also call the parent() class method. Here is the modified Employee class-

class Employee(Person):
  def __init__(self, person_id, department, name, age):
    super().__init__(name, age)
    self.person_id = person_id
    self.department = department

  def display_info(self):
    super().display_info()
    print('In display_info method of Employee class')
    print('Id:', self.person_id)
    print('Department:', self.department)

As you can see super() is used to call the parent class constructor with the fields that can be initialized there.

super().__init__(name, age)

From the overridden display_info() method there is a call to the method of the parent class.

super().display_info()

On creating an object of Employee class and calling the method using that object

e = Employee(1, "IT", "Michael Weyman", 42)
e.display_info()
Output
In display_info method of Person class
Name: Michael Weyman
Age: 42
In display_info method of Employee class
Id: 1
Department: IT

That's all for the topic super() in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 7, 2022

React Example - Insert New Object in an Array

In this tutorial we'll see how to insert an object into an array of objects in your React application especially when you are storing that array in state. What you need to keep in mind is that the you should not mutate an array when you store them in state. You should rather create a new array with the required changes and set state to use this new array.

Inserting into an array - React

As stated above you need to create a new array which should contain the existing elements and then the new element at the end.

Preferred way to do it, in React, is to use spread operator to get all the existing elements and then add the new array element.

As an example, we'll have a ProductForm component to add a new Product to an array. There is also a Products component that shows the products which are in the array.

Products.js

import { Fragment } from "react";
const Products = (props) =>{
    return(
        <Fragment>
            <h2>Product List</h2>
            <ul>{
                props.products.map((product) => (
                    <li key={product.id}>
                        Id- {product.id} <br />
                        Name- {product.name}                    
                    </li>
                ))
            }
            </ul>
        </Fragment>
    );
}
export default Products;

List of products is passed to the Products component from the parent component (App.js), that is why it is accessible here through props.

ProductForm.js

import { Fragment } from "react";
import { useState } from "react";

const ProductForm = (props) => {
    const [prodName, setProdName] = useState('');
    const clickHandler = () => {
        props.onAddProduct(prodName);
        setProdName('');
    }
    return(
        <Fragment>
            <h2>Product Form</h2>
            <input
                value={prodName}
                onChange={e => setProdName(e.target.value)}
            />
            <button onClick={clickHandler}>Add Product</button>
        </Fragment>
    );
}

export default ProductForm;

Some point to note in ProductForm component-

  1. Uses useState hook to maintain the product name state.
  2. On the click of the button, clickHandler() function is called which in turn calls the onAddProduct function in another component, that is why it is called using props.

App.js

const productList = [
  { id: 1, name: 'Laptop', price: 455.50 },
  { id: 2, name: 'Mouse', price: 15.89 },
  { id: 3, name: 'USB', price: 10.00 },
  { id: 4, name: 'HDD', price: 55.50 },
];
function App() {
  const [products, setProducts] = useState(productList);
  const addProductHandler = (productName) => {
    setProducts([
      ...products,
      { id: Math.round(Math.random() * 10), name: productName }
    ]);
  }
  return (
    <div>
      <ProductForm onAddProduct={addProductHandler}/>
      <hr />
      <Products products={products}/>

    </div>
  );
};

export default App;

Important points to note here-

  1. An array is created with some existing product objects in it.
  2. The array is stored in the state.
  3. In addProductHandler() function new array is created by adding the existing products (using spread operator) and then adding the new product object (Math.random is used to create new ID here, not an ideal way but does the job here!). This new array is then set as new state by passing it to setProducts().
    setProducts([
      ...products,
      { id: Math.round(Math.random() * 10), name: productName }
    ]);
    
  4. Reference to the addProductHandler function is passed in <ProductForm> tag as props.
    <ProductForm onAddProduct={addProductHandler}/>
    
  5. products Array is passed in Products tag as props.
    <Products products={products}/>
    

With everything done if a new product "Keyboard" is added through the form, it should be added to the products array.

That's all for the topic React Example - Insert New Object in an Array. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 5, 2022

React Example - Update Object in an Array of Objects

It is a very common requirement to update one or more objects in an array of objects in your React application. What you need to keep in mind is that the you should not mutate an array when you store them in state. You should rather create a new array with the required changes and set state to use this new array.

Update object in array of objects

If you want to modify one or more items of an array, you can use map() to create a new array. Steps are as given below.

  1. Iterate the array using map() function.
  2. Function passed as first argument with in map() should have the condition to decide whether object should be updated or not.
  3. If condition is satisfied then update that object otherwise return the object as is. That way you will have a new array with all the objects including updated one.

As an example, let's have a shopping cart with certain products added. On the click of the button increase or decrease the product count.

import { useState } from "react";

const INITIAL_PRODUCTS = [{
    id: 1,
    name: 'Samosa',
    count: 5,
  }, 
  {
    id: 2,
    name: 'Roti',
    count: 4,
  }, 
  {
    id: 3,
    name: 'Chicken Tikka Masala',
    count: 2,
  },
  {
    id: 4,
    name: 'Matar Paneer',
    count: 1,
  }];

const ShoppingCart = () => {
    const [products, setProducts] = useState(INITIAL_PRODUCTS);
    const handleIncreaseClick = (productId) => {
        const updatedProducts = products.map(product => {
            // update count property of the matching object
            if(product.id === productId){
              return {...product, 
                      count:product.count + 1};
            }else{
              // return object unchanged
              return product;
            } 
          });
        setProducts(updatedProducts);
    }
    const handleDecreaseClick = (productId) => {
        let updatedProducts = products.map(product => {
            if(product.id === productId){
              return {...product, 
                      count:product.count - 1};
            }else{
              return product;
            } 
        });
        updatedProducts = updatedProducts.filter(p => p.count > 0);
        setProducts(updatedProducts);
    }
    return (
        <ul>
            {
                products.map(product => (
                    <li key={product.id}>
                        {product.name}
                        -
                        {product.count}
                        <button onClick={()=> {
                            handleIncreaseClick(product.id)
                        }}>+</button>
                        <button onClick={()=> {
                            handleDecreaseClick(product.id)
                        }}>-</button>
                    </li>
                ))
            }
        </ul>
    );
}

export default ShoppingCart;

Important points to note in the code are-

  1. Array is initialized with some product objects.
  2. Clicking the ‘+’ button triggers the handleIncreaseClick() function with the id of the product passed as an argument. In the handleIncreaseClick() function create a new array by going through all the products, the product for which the id matches increase the count by 1, otherwise return the product without any change.
  3. Then set the products state by passing this new array.
  4. Clicking the ‘-’ button triggers the handleDecreaseClick() function with the id of the product passed as an argument. If the product id matches, decrease the count by 1. One more thing is to later check for the products with count as 0 to remove them from the array. For that filter() is used.
Update object in array of objects

That's all for the topic React Example - Update Object in an Array of Objects. If something is missing or you have something to share about the topic please write a comment.


You may also like

December 2, 2022

How to Split a String in Java

This post shows how you can split a String in Java using split() method. String you need to split may be delimited using pipe, tab or spaces so let’s see how to use split() method to split such delimited data strings in Java. Note that if you are using any special symbol with in the regular expression then you do need to escape it using escape character (\).

Splitting String delimited using pipe(|) symbol - Java code

public class SplitString {
  public static void main(String[] args) {
    String str = "A001|BOA|Ronda|5000";
    String[] data = str.split("\\|");
    System.out.println("Name- " + data[2]);
  }
}
Output
Name- Ronda

Splitting data in Java delimited using tab (\t)

public class SplitString {
  public static void main(String[] args) {
    String str = "A001	BOA	Ronda	5000";
    String[] data = str.split("\t");
    System.out.println("Amount- " + data[3]);
  }
}
Output
Amount- 5000

Splitting data delimited using spaces- Java code

public class SplitString {
  public static void main(String[] args) {
    String str = "A001  BOA Ronda 5000";
    // Matches any number of spaces
    String[] data = str.split("\\s+");
    System.out.println("Amount- " + data[3]);
  }
}
Output
Amount- 5000

Splitting data delimited using single space

public class SplitString {
  public static void main(String[] args) {
    String str = "A001 BOA Ronda 5000";
    // Matches any number of spaces
    String[] data = str.split("\\s");
    System.out.println("Name- " + data[2]);
  }
}
Output
Name- Ronda

That's all for the topic How to Split a String in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like