0% found this document useful (0 votes)
49 views

Java 8

There are 4 main ways to provide an implementation for a Java interface according to the document: 1) By writing a separate implementation class; 2) By writing an anonymous inner class; 3) By writing lambda expressions; 4) By writing method references. Lambdas allow providing a concise implementation without maintaining a separate class file, and are useful when working with collections. Interfaces can have default, static, and object-class methods from Java 8 onward.

Uploaded by

Aniruddha Chunne
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Java 8

There are 4 main ways to provide an implementation for a Java interface according to the document: 1) By writing a separate implementation class; 2) By writing an anonymous inner class; 3) By writing lambda expressions; 4) By writing method references. Lambdas allow providing a concise implementation without maintaining a separate class file, and are useful when working with collections. Interfaces can have default, static, and object-class methods from Java 8 onward.

Uploaded by

Aniruddha Chunne
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

How many ways we can provide implementation for interface from Java 8

a. by writing a seperate implementation class


b. by writing a annonymous inner class
c. by writing lambdas
d. by writing method references

Video 1:

FunctionInterface - from Java 8

def: A interface , which is allows only one abstract method.

we can write more than one default method


we can write more than one static method
we can write java.lang.Object class method

To developing functional programming in java.

interfaces can be implemented in 4 ways


1. create a class and implement a interface
2. implementation through annonymous class
3. implementation through lambda expression
4. implementation through method references

we should write @FunctionalInterface annotation

//we should always go for static method in the interface


// because for default we need implementation class
//and for static method we do not need implementation class
package com;

@FunctionalInterface
interface I
{
public abstract void m1();

public default void m2()


{
System.out.println("m2-default-I");
}

public default void m3()


{
System.out.println("m3-default-I");
}

public static void m4()


{
System.out.println("m4-static-I");
}
public static void m5()
{
System.out.println("m5-static-I");
}
}

public class Main implements I


{
public void m1()
{
System.out.println("m1..abstract method");
}

public static void main(String[] args)


{

I acc = new Main();

acc.m1();

acc.m2();
acc.m3();

I.m5();
I.m4();

}
}

Video 2:

B. by writing a annonymous inner class

annonymous class is a class which present inside the another class without having
any identity or a name. is called annonymous inner class

//making annonymous class for providing implementation for Interface

//making annonymous class for providing implementation for Interface

Annonymous class provide implementation for interface

package com;

interface Test
{
public abstract void m1();
}

public class video2


{

public static void main(String[] args)


{
System.out.println("we are testing the method");

Test a = new Test() {


@Override
public void m1()
{
System.out.println("we are m1");
}
};

a.m1();
}
}

Video No. 3

Default methods in Interface:

without doing modification on implementation class of interface,


if we want add new serivce/logic then we should write that logic in interface by
using default methods...

Video No. 4

package com;

interface i
{
public abstract void m1();

public default void m2()


{
System.out.println("we are m2 of interface i");
}
}

class A implements i
{
@Override
public void m1() {
System.out.println("we are A-m1");
}
}

class B implements i
{
@Override
public void m1()
{
System.out.println("we are B-m2");
}
}

public class video4


{

public static void main(String[] args)


{
i e = new A();
i f = new B ();

i g = new i() {
@Override
public void m1()
{
System.out.println(" we are annonymouns inner class");
}
};

g.m2();
e.m2();
f.m2();

g.m1();
e.m1();
f.m1();
}
}

Video No. 5

we can not override java.lang.object class method as a default method.

Video No. 6

package com;

interface A
{
public default void call()
{
System.out.println("we are A default class");
}
}
interface B
{
public default void call()
{
System.out.println("we are A default class");
}
}

class Onee implements A, B


{

@Override
public void call() {
A.super.call();
B.super.call();
System.out.println("we are Onee.");
}
}

public class Video6 {

public static void main(String[] args)


{
A s = new Onee();

s.call();
}

constructor - main use is to initialize the non static variable.

Video No. 7

What is the difference between interface and abstract class.

Interface
Abstract class
1. Variables are public static final
1. Variables are private public, static, final, protected
2. We can't write the constructors 2. We
can write the constructors
3. Interface doesn't talk about the state of the object
3. Abstract talks about the state of the object
4. We can't write blocks/initializers[instance/static]
4. abstract class can allows both static and instance initializers/blocks.
5. It can allows abstract methods but not the allows the
5. it can allows both concrete and abstract method
concerte methods
6. We can logic in interface by using default method
6. We can't write default methods in interface
7. It will supports multiple inheritance 7. It
will not support multiple inheritance.
8. We can't write object class method in interface
8. We can write the object class method in abstract class.

Abstract class vs Interface


Type of methods:
Interface can have only abstract methods. An abstract class can have abstract and
non-abstract methods. From Java 8, it can have default and static methods also.

Final Variables:
Variables declared in a Java interface are by default final. An abstract class may
contain non-final variables.

Type of variables:
Abstract class can have final, non-final, static and non-static variables. The
interface has only static and final variables.

Implementation: ************
Abstract class can provide the implementation of the interface. Interface can’t
provide the implementation of an abstract class.

Inheritance vs Abstraction:
A Java interface can be implemented using the keyword “implements” and an abstract
class can be extended using the keyword “extends”.

Multiple implementations:
An interface can extend another Java interface only, an abstract class can extend
another Java class and implement multiple Java interfaces.

Accessibility of Data Members:


Members of a Java interface are public by default. A Java abstract class can have
class members like private, protected, etc.

Video No. 8

Static method in interface from JDK 1.8

/*
No need to take the suppoort of class for static method to execute the logic, we
can do it in interface, abstract class, enum also.
//Static method in interface.

*/
/*if you want to execute any logic without any implementation or annonymous class
that should must be write in the
static method.*//*

package com;

interface i2
{
public default void m2()
{
System.out.println("we are m2 of the default i2 interface");
}
public void m1();
public static void m3()
{
System.out.println("we are a static method");
}
}

class O implements i2
{
@Override
public void m1() {
System.out.println("we re O implemtned class");
}
}

public class Video8


{
public static void main(String[] args) {

i2 a = new i2() {
@Override
public void m1() {
m2();
}
} ;

a.m2();
a.m1();

i2.m3();

O e = new O();
e.m2();

}
}
*/

interface i
{
public static void main(String[] args) {
System.out.println("we are ok");
}
}

interface i
{
public static void main(String[] args) {
System.out.println("we are ok");
}
}
abstract class i
{
public static void main(String[] args) {
System.out.println("we are ok");
}
}
enum i
{
public static void main(String[] args) {
System.out.println("we are ok");
}
}
class i
{
public static void main(String[] args) {
System.out.println("we are ok");
}
}

Video NO. 9

package com;

interface j
{
public abstract void m4();
public abstract String toString();// method of java.lang.object
public abstract int hashCode();// method of java.lang.object
}

class Impl implements j


{
@Override
public void m4() {
System.out.println("we are inteface of the j");
}

@Override
public String toString() {
return "we are toString";
}

@Override
public int hashCode() {
return 21468;
}
}

public class Video9


{
public static void main(String[] args) {
Impl se = new Impl();
se.m4();
System.out.println(se.hashCode());
System.out.println(se.toString());
}

Video No. 10
Q. How many ways can we able to provide implementation for interface from JDK 1.8

a. by writing a seperate class


b. by writing annonymous inner class
c. by writing lambda expression
d. by writing method reference.

Video No. 11 - Lambda method

package com;

// here we are using lambda method


it can only be use with functional interface;
we create a implementation class
But dont maintain the .class file
it provide the concise and specific applicaiton of method.
it help or used mostly for collections objects.

interface Ie
{
public abstract void learn();
}

public class practise


{

public static void main(String[] args)


{

Ie a = () -> {

System.out.println("we are ok");

};
a.learn();
}
}

Video No. 12

//functional interface participate in the inheritance


but the limitation is functional interface should maintain the single method inside
of it.

package com;

interface a1
{
public abstract void r1();
}

interface b1 extends a1
{

public class Video12


{
public static void main(String[] args)
{
b1 b = new b1() {
@Override
public void r1()
{

System.out.println("we are internal metter");

}
}
}
}

Video No. 13

Lambda Expression in java JDK - 1.8

what: annonymous function/unnamed function/anonymous expression


class.object.

why: providing implementation for only Functional Interface writing less and
perform more operations suitable for Collections Frameworks objects.

syntax:
(datatypes parameter, datatypes parameter) --> {statement};

note: not creating any .class files.

Video No. 15

package com;

interface A1 <T>
{
public abstract T m2( T x);
}

public class video15


{
public static void main(String[] args)
{
A1<Integer> com = (Integer x)->
{
System.out.println(x);
return x;
};

com.m2(121);

Video No. 20

/*Method References in java

1. Provides implementation for Functional Interface


2. Method references always pointing or referencing implementation of a interface
method.
3.It is simple way of providing implementation for functional interface by writing
less code.
4.Method reference internally creates lambda expression.
5.Method reference always uses existed method logic as implementation for
functional interface
6. Method reference internally creates all four types of lambdas...*/

package com;

interface A1 <T>
{
public abstract T m2( T x);
}

public class video15


{
public static void main(String[] args)
{

A1<Integer> com = (Integer x)->


{
System.out.println(x);
return x;
};

com.m2(121);

package com;

interface i
{
public abstract void a();
}
class ax
{
public void c()
{
System.out.println(" we are c");
}
}

public class Video20


{
public static void main(String[] args)
{
i o = new ax() ::c;

o.a();

You might also like