Posts Tagged: Java8

So what’s that difference again? Abstract class vs Interface

Everything was nice and pretty clear before beautiful Java 8 was born.  One could define:

(1) Abstract class as being a class declared abstract (usually, but not necessary, contains at least one abstract method).

(2) Abstract method as being one method that is declared, but for which no implementation was provided.

(3) Interface as a 100% abstract class.

Even though, not all the differences are explained in these statements, these would have been acceptable definitions.

And then Java 8 was brought into the world, and the new feature (considered by some the best new enhancement) : default and static methods for interfaces. Non-abstract methods defined directly in the interface. This implies that (3) became obsolete and the magic question arise:

What is the difference between an interface and abstract class? 

Abstract class Interface
Default methods any method that is not abstract can be considered defaultit is not marked with default modifier marked withdefault modifier
Static methods static modifier required see below examples static modifier required
Non-Constants variables can be defined all the variables are implicitly public static final
A Java class can extend just one class (abstract or concrete) implement multiple interfaces
A Java interface cannot extend/implement an abstract class can extend multiple interfaces
The first concrete class extending the abstract class must implement all abstract methods implementing the interface must implement all abstract methods
Implicit modifiers for variables No implicit modifiers public static final
Implicit modifier for methods No implicit modifiers the non-static & non-default methods are implicitly public abstract
When to use … ? when the code is shared amongst related classes when the code is shared among unrelated classes

So, let’s revisit the definition of an interface. I would like to go for this one:

Interfaces are abstract data types that provide a contract between its users and its providers.

Here you have a nice example to start discovering the differences between abstract classes and interfaces!

import static java.lang.System.out;
class Main {
 public static void main (String[] args) throws Exception { //Mother mother = new Mother(); DOES NOT COMPILE
 //AnInterface interface = new AnInterface(); DOES NOT COMPILE
 Mother child1 = new Child();
 AnInterface child2 = new Child();
 Child child3 = new Child();
 //calling static fields


 //caling static methods
 //calling default methods

 //child2.staticInterfaceMethod(); DOES NOT COMPILE
 //child3.staticInterfaceMethod(); DOES NOT COMPILE
 //Child.staticInterfaceMethod(); DOES NOT COMPILE
 //calling default methods



abstract class Mother {

 public static final String ABSTRACT_CLASS_NAME = "Mum name";

 public static void staticMotherMethod() { //the access modifier is not implied
 out.println("static Mother Method");

 public void defaultMotherMethod() { //the access modifier is not implied
 out.println("Mother default");

 public void defaultMotherMethod1() {
 out.println("Mother default 1"); //the access modifier id not implied

 public abstract void abstractMotherMethod() ;

interface AnInterface {

 String INTERFACE_NAME = "Interface name"; //implicit public static final

 default void defaultInterfaceMethod() { //implicit public
 out.println("default interface method");

 default void defaultInterfaceMethod1() {
 out.println("default interface 1");

 static void staticInterfaceMethod() { //implicit public
 out.println("static inteface method");

 void abstractInterfaceMethod(); //implicit public abstract

class Child extends Mother implements AnInterface {
 public void abstractInterfaceMethod() {
 out.println("Child abstractInterfaceMethod");

 public void abstractMotherMethod() {
 out.println("Child abstractMumMethod");

 public void defaultMotherMethod1() {
 out.println("Child default method");

 public void defaultInterfaceMethod() {
 out.println("Child interface method");


Hope you had fun! 🙂 Till the next post hope you enjoy Java!

And I fell in love with the date…Java 8 Date

Love is in the air year after year in the period when winter and spring fight for power.

In the week that Java8 turns one I am going to speak about the date I fell in love with…Java 8 Date.

Even though I have never considered the previous implementation of Dates as being a pain (just a little bit verbose :p) I am really appreciative and using it made me fall instanlty in love with the new Date.

What do I really like about Date? You may find listed below some(not all) reasons:

  • The possibility to use classes specify/use exactly what you need:

Why should I be forced to store the specific date (usually 1st January 1970) when I just need the hours?  was a question that arose probably in many situations. [I would really want to know how many GB of data occupies the 1st January 1970]

Java 8 resolved the issue with adding specific classes LocalDateTime, LocalDate and LocalTime classes for the “un-timezoned” feature and for each of these provides the zoned class, which stores also the timezone for each of them.

Also, the precision one needs to use can be specified

  • The easiness to get a reference to a new instance of a Date.

Seems like a dream not to use a Calendar class to get a new instance of a Date class:

LocalDateTime thisMoment =;

LocalDate java8LauchDate = LocalDate.of(18, Month.MARCH, 2014)

LocalDate forTheNostalgics = LocalDate.ofEpochDay(1);

  • The beauty of the Period and Duration

Until this moment we specified precise moment in time, but sometimes we need to use an interval rather than specific points in time.

Period oneYearTwoMonthsThreeDays = Period.of(1,2,3);

and Period’s little brother (used for intervals within a day):

Duration snoozeDuration = Duration.ofMinutes(10);

  • One can do maths![How can one not love that]

The addition/substraction of specific periods is just a bliss:

LocalDateTime now =;

  •  The usage of the “normal” number for the month.

Just imagine in a previous Java version my birthday would have been declared like. Ok, I am  a developer and used with 0-index, but I could not just set my brain to use 5 0 for my birthday[Now that you know when my birthday is, you can send me birthday cards or presents and I do not mind receiving them before of after my bithday…so it’s still not too late 🙂 ]

Happy birthday, Java8!

And for you: hope you all have dates as good as the Java 8 ones!