Interface, Early and late binding,static and default method in java 8,Var-args,Enum
Interface, Early and late binding,static and default method in java 8,Var-args,Enum
Interface In Java:
An Interface in Java is defined as an abstract type used to specify the behaviour of a
class. An interface in Java is a blueprint of a class. A Java interface contains static
constants and abstract methods.
The interface in Java is a mechanism to achieve 100% abstraction. There can be only
abstract methods in the Java interface, not the method body. It is used to achieve
abstraction and multiple inheritance in Java. In other words, you can say that
interfaces can have abstract methods and variables. It cannot have a method body.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 1
Syntax:
interface InterfaceName{
Uses Of an Interface:
Since java does not support multiple inheritances in the case of class, by using an
interface it can achieve multiple inheritances.
Note: The Java compiler adds public and abstract keywords before the interface
method. Moreover, it adds public, static and final keywords before data members.
Example:
//Printer.java
int number=10;
void print();
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 2
public static final int number=10;
public abstract void print();
//ConsolePrinter.java
//FilePrinter.java
//Main.java
cp.print();
p1.print();
p2.print();
}
}
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 3
I Problem:
lets create an interface by the name Shape with one method draw and implement this
interface in Rectangle and Circle classes.
//Shape.java
//Rectangle.java
class Rectangle implements Shape{
//Circle.java
class Circle implements Shape{
public void draw(){
System.out.println("drawing circle");
}
}
//Main.java
class Main{
public static void main(String args[]){
shape1.draw();
shape2.draw();
}
}
We Problem:
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 4
Let’s take a method which will accept the Printer object and now we can call this
method by supplying different implementations of the Printer interface.
printSomething(new ConsolePrinter());
printSomething(new FilePrinter());
//printSomething(null); // it will raise NullPointerException
}
}
As shown in the figure given below, a class extends another class, an interface extends
another interface, but a class implements an interface
.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 5
Multiple inheritance in Java by interface:
Example:
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 6
//Printable.java
interface Printable{
void print();
}
//Showable.java
interface Showable{
void show();
}
//Demo.java
}
}
Interface inheritance:
Example:
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 7
//Printable.java
interface Printable{
void print();
}
//Showable.java
interface Showable extends Printable{
void show();
}
//Main.java
class Main implements Showable{
obj.print();
obj.show();
}
}
You Problem:
Create an Interface Animal with one abstract method :
Create 2 implementation classes for the above interface Dog and Cat and
override the above method accordingly:
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 8
Create a Main class with the following method:
From the main method of Main class call the above getNoise() method 2 times
by passing the object of Dog class and the object of Cat class.
Example:
//Intr.java
interface Intr
{
//abstract method
void method1();
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 9
//default method
default void method2()
{
System.out.println("hello");
}
}
//IntrImpl1.java
class IntrImpl1 implements Intr{
//Main.java
class Main{
public static void main(String[] args){
Note: The default method of an interface need not override inside the
implementation class, but if we want we can override the default method also with
some other implementation inside the implementation class.
Another feature that was added in JDK 8 is that we can now define static methods
in interfaces that can be called independently without an object.
Note: these methods are not inherited. We always call the static method with the
help of Interface name.
//Intr.java
interface Intr
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 10
{
//abstract method
void method1();
//default method
default void method2()
{
System.out.println("inside method2");
}
//static method
static void method3()
{
System.out.println("inside method3");
}
}
//IntrImpl1.java
class IntrImpl1 implements Intr{
//Main.java
class Main{
public static void main(String[] args){
Intr.method3();
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 11
Example:
Abstract class and interface both are used to achieve abstraction where we can
declare the abstract methods. Abstract class and interface both can't be
instantiated.
But there are many differences between abstract class and interface that are given
below.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 12
Simply, an abstract class achieves partial abstraction (0 to 100%) whereas an
interface achieves fully abstraction (100%). Not very valid after Java 8.
You expect that classes that extend your abstract class have many common
methods or fields, or require access modifiers other than public (such as
protected and private).
You want to declare non-static or non-final fields. This enables you to define
methods that can access and modify the state of the object to which they
belong.
You expect that unrelated classes would implement your interface. For
example, the interfaces Comparable and Cloneable are implemented by many
unrelated classes.
You want to specify the behavior of a particular data type, but not
concerned about who implements its behavior.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 13
(which means that it can be converted into a byte stream; see the
section Serializable Objects), and has the functionality of a map. In addition,
the Map<K, V> interface has been enhanced with many default methods such
as merge and forEach that older classes that have implemented this interface do not
have to define.
Note that many software libraries use both abstract classes and interfaces;
the HashMap class implements several interfaces and also extends the abstract
class AbstractMap .
int data=30;
Here data variable is a type of int.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 14
Early binding example:
When type of the object is determined at compiled time(by the compiler), it is known
as early/static binding.
If there is any private, final or static method in a class, there is static binding.
class Dog{
private void eat(){
System.out.println("dog is eating...");
}
//Animal.java
class Animal{
void eat(){
System.out.println("animal is eating...");
}
}
//Dog.java
class Dog extends Animal{
void eat(){
System.out.println("dog is eating...");
}
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 15
In the above example, object type cannot be determined by the compiler, because
the instance of Dog is also an instance of Animal. So the compiler doesn't know its
type, only its base type.
var-args:(Variable Argument):
The var-args allows the method to accept zero or multiple arguments. Before var-
args either we use an overloaded method or take an array as the method parameter
but it was not considered good practice, because it leads to the maintenance
problem. If we don't know how many arguments we will have to pass in the method,
var-args is the better approach.
Advantage of var-args:
We don't have to define multiple overloaded methods.
Syntax:
The var-args uses ellipsis i.e. three dots after the data type.
Example:
class Main{
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 16
System.out.println("display method invoked ");
}
display();//zero argument
display("my","name","is","varargs");//four arguments
}
}
Example:
class Main{
display();//zero argument
display("my","name","is","varargs");//four arguments
}
}
• Variable argument (var-args) must be the last argument if we have any other
arguments are there.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 17
Example1:
Example2:
class Main{
System.out.println("number is "+num);
for(String s:values){
System.out.println(s);
}
display(200);//zero var-arg
display(500,"hello");//one var-args
display(1000,"my","name","is","varargs");//four var-args
}
}
Enum in Java:
The Enum in Java is a data type that contains a fixed set of constants.
It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY), directions (NORTH, SOUTH, EAST, WEST),
season (SPRING, SUMMER, WINTER, AUTUMN), colors (RED, YELLOW, BLUE,
GREEN, WHITE, BLACK) etc. According to the Java naming conventions, we
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 18
should have all constants in capital letters. So, we have enum constants in capital
letters.
Here we save an Enum with .java extension, and after compilation, we get a .class
file for an enum also.
//Color.java
enum Color {
RED,
GREEN,
BLUE;
}
//Main.java
public class Main {
//Main.java
public class Main {
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 19
enum Color {
RED,
GREEN,
BLUE;
}
Note:
• Every enum is internally implemented by using Class.
//Color.java
enum Color {
RED,
GREEN,
BLUE;
}
We can declare the main() method inside the enum. and we can run an enum file
directly from the Command Prompt.
//Color.java
enum Color {
RED,
GREEN,
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 20
BLUE;
>javac Color.java
>java Color
values() method can be used to return all values present inside the enum.
Example:
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 21
enum Color {
RED,
GREEN,
BLUE;
}
}
}
Output
RED at index 0
GREEN at index 1
BLUE at index 2
We can define constructors, methods, and fields inside enum types, which makes
them very powerful.
//Item.java
SUGER,RICE,SALT;
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 22
}
//Main.java
for(Item item:itr){
item.info();
}
}
}
//Item.java
public enum Item {
Enum can contain a constructor and it is executed separately for each enum
constant at the time of enum class loading.
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 23
We can’t create enum objects explicitly and hence we can’t invoke enum
constructor directly.
The constructor of enum type is private. If you don't declare private, compiler
internally creates private constructor.
Example:
enum Season{
WINTER(10),SUMMER(20);
Season(int value){
this.value=value;
}
}
Example2:
//City.java
public enum City {
DELHI{
},
MUMBAI,
CHENNAI("50 towers"),
KOLKATA;
//variables in enum
public String numberofTowers;
//constructor
City(){
this.numberofTowers="100 towers";
}
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 24
this.numberofTowers=numberofTowers;
}
//Main.java
if(city != null){
System.out.println("our service is available ");
city.message();
System.out.println(city.numberofTowers);
}
else
System.out.println("invalid city");
m1.printCity(City.CHENNAI);
}
}
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 25
class Main{
enum Day{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
Day day=Day.MONDAY;
switch(day){
case SUNDAY:
System.out.println("sunday");
break;
case MONDAY:
System.out.println("monday");
break;
default:
System.out.println("other day");
}
}
}
References:
https://www.javatpoint.com/
https://www.geeksforgeeks.org/
https://www.programiz.com/
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Day8: Interface, Early and late binding, static and default method in java 8, Var-args, Enums 26