Java8 New - Study Guide - Part 1
Java8 New - Study Guide - Part 1
Author
Srinivas Dande
1)Animal.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface Animal {
public abstract void eating();
public abstract void sleeping();
@Override
public void eating() {
System.out.println("Dog is eating");
}
@Override
public void sleeping() {
System.out.println("Dog is sleeping");
}
@Override
public void running() {
System.out.println("Dog is running");
}
}
3)Cat.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Cat implements Animal{
@Override
public void eating() {
System.out.println("Cat is eating");
}
@Override
public void sleeping() {
System.out.println("Cat is sleeping");
}
@Override
public void thinking() {
System.out.println("Cat is thinking");
}
}
1)A.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
3)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A,B{
@Override
public void show() {
System.out.println("Hello- show() ");
}
public void test() {
System.out.println("Hello- test() ");
show();
A.super.show();
B.super.show();
}
}
4)Demo1.java
package com.jlcindia.demo2;
1)A.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
/*
default boolean equals(Object obj) {
System.out.println("A- equuals() ");
}
*/
}
2)Hello.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A{
1)A.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
default void m1() {
System.out.println("A- m1() ");
}
}
2)B.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface B extends A {
default void m2() {
System.out.println("B- m2() ");
m1();
}
}
4)Demo4.java
package com.jlcindia.demo4;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
Hello hello=new Hello();
hello.m1();
hello.m2();
}
}
1)A.java
package com.jlcindia.demo5;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
default void m1() {
System.out.println("A- m1() ");
}
}
3)Hello.java
package com.jlcindia.demo5;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements B{
4)Demo4.java
package com.jlcindia.demo5;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo4 {
public static void main(String[] args) {
Hello hello=new Hello();
hello.m1();
hello.m2();
}
}
Q6) Can I mark the Overriden Default Methods as Default in the Sub Class?
Ans:
Q7) Can I Override Object class methods as Default Methods inside Interface?
Ans:
Q9) What happens when Sub Class is implementing two interfaces which are having same default
method?
Ans:
Q12) Can I call one Default Method from another Default Methods of same Interface?
Ans:
Q13) Can I Override Interface Default Method extended from other Interface?
Ans:
Concrete methods (Methods with Body) Defined in the Interface with static keyword
are called as Static Methods.
Static Methods are public by default.
Static Methods will not be inherited to Sub classes.
Sub class can not override the Interface Static Methods.
If we write the Static Method of Interface in Sub Class then That will be treated as New
Method in Sub Class.
1)A.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
int P=101;
public final static int Q=102;
void m1();
public abstract void m2();
2)Hello.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A {
System.out.println(P); //Inherited
System.out.println(Q); //Inherited
m1(); //Overriden
m2(); //Overriden
m3(); //Overriden
A.super.m3();
m4(); //Inherited
A.super.m4();
A.m5();
A.m6();
//A.super.m6();
}
@Override
public void m1() {
System.out.println("Hello -m1");
}
@Override
public void m2() {
System.out.println("Hello -m2");
}
}
/*
@Override
public static void m5() {
System.out.println("Hello -m5");
}
*/
}
3)Demo1.java
package com.jlcindia.demo1;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Demo1 {
public static void main(String[] args) {
Hello hello=new Hello();
hello.test();
}
}
1)A.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
static void m1() {
System.out.println("A - m1()");
}
}
3)Hello.java
package com.jlcindia.demo2;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Hello implements A,B {
m1();
A.m1();
B.m1();
}
Hello hello1=null;
hello1.show();
//2. Calling Static Method with Ref. Variable having Object address
//A aobj = new Hello();
//aobj.m1();
1)A.java
package com.jlcindia.demo3;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public interface A {
System.out.println("main method");
m1();
//m2(); // Can not call Instance Method in Static
Q7) What happens when Sub Class is implementing two interfaces which are having same Static
method?
Ans:
Q11) Can I call one Static Method from another Default Methods of same Interface?
Ans:
Q12) Can I call one Default Method from another Static Methods of same Interface?
Ans: