Skip to content

Commit 7579d86

Browse files
committed
Default methods
1 parent e1828ac commit 7579d86

13 files changed

Lines changed: 317 additions & 0 deletions

defaultmethods/BookDemo.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
3+
public class BookDemo {
4+
public static void main(String[] args) {
5+
Book book = new Book();
6+
book.add(new Chapter("Chapter 1"));
7+
book.add(new Chapter("Chapter 2"));
8+
book.add(new Chapter("Chapter 3"));
9+
for(Chapter ch : book) {
10+
System.out.println(ch);
11+
}
12+
}
13+
}
14+
15+
class Book implements Iterable<Chapter> {
16+
private List<Chapter> chapters = new ArrayList<>();
17+
18+
public void add(Chapter chapter) {
19+
this.chapters.add(chapter);
20+
}
21+
22+
@Override
23+
public Iterator<Chapter> iterator() {
24+
return chapters.iterator();
25+
}
26+
}
27+
28+
class Chapter {
29+
private String title;
30+
31+
public Chapter(String title) {
32+
this.title = title;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return this.title;
38+
}
39+
}

defaultmethods/DefaultInClass.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class DefaultInClass {
2+
/*
3+
* Compile time error.
4+
* Cannot have default method inside a class.
5+
*/
6+
default void doStuff() {
7+
System.out.println("Hello world");
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public interface DefaultWithoutBody {
2+
/*
3+
* Compile time error.
4+
* No method body for the default method.
5+
*/
6+
default void doStuff();
7+
}

defaultmethods/DiamondProblem.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class DiamondProblem {
2+
public static void main(String[] args) {
3+
A obj = new D();
4+
obj.print();
5+
}
6+
}
7+
8+
interface A {
9+
default void print() {
10+
System.out.println("A");
11+
}
12+
}
13+
14+
interface B extends A {
15+
default void print() {
16+
System.out.println("B");
17+
}
18+
}
19+
20+
interface C extends A {
21+
default void print() {
22+
System.out.println("C");
23+
}
24+
}
25+
/*
26+
* Compile time error.
27+
* Diamond problem since there is a clash in the print() methods.
28+
*/
29+
class D implements B, C {
30+
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class DiamondSolutionOne {
2+
public static void main(String[] args) {
3+
A obj = new D();
4+
obj.print();
5+
}
6+
}
7+
8+
interface A {
9+
default void print() {
10+
System.out.println("A");
11+
}
12+
}
13+
14+
interface B extends A {
15+
default void print() {
16+
System.out.println("B");
17+
}
18+
}
19+
20+
interface C extends A {
21+
default void print() {
22+
System.out.println("C");
23+
}
24+
}
25+
26+
/*
27+
* The overridden method will get selected
28+
*/
29+
class D implements B, C {
30+
@Override
31+
public void print() {
32+
System.out.println("D");
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class DiamondSolutionTwo {
2+
public static void main(String[] args) {
3+
A obj = new D();
4+
obj.print();
5+
}
6+
}
7+
8+
interface A {
9+
default void print() {
10+
System.out.println("A");
11+
}
12+
}
13+
14+
interface B extends A {
15+
default void print() {
16+
System.out.println("B");
17+
}
18+
}
19+
20+
interface C extends A {
21+
default void print() {
22+
System.out.println("C");
23+
}
24+
}
25+
26+
class P {
27+
public void print() {
28+
System.out.println("P");
29+
}
30+
}
31+
32+
/*
33+
* Super class provides the implementation of print() method
34+
*/
35+
class D extends P implements B, C {
36+
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public interface FinalDefaultMethod {
2+
/*
3+
* Compile time error.
4+
* final and default cannot be used together.
5+
*/
6+
public final default void print() {
7+
System.out.println("Hello world");
8+
}
9+
}

defaultmethods/MultiSales.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class MultiSales {
2+
public static void main(String[] args) {
3+
doStuff(new Pen(10.00), 5);
4+
doStuff(new Book(250.00, 5.00), 2);
5+
}
6+
7+
public static void doStuff(Sellable item, int count) {
8+
double income = item.sell(count);
9+
System.out.println("Income: $" + income);
10+
}
11+
}
12+
13+
interface Sellable {
14+
double sell();
15+
16+
default double sell(int count) {
17+
double total = 0.0;
18+
for(int i = 1; i <= count; i++) {
19+
total += this.sell();
20+
}
21+
return total;
22+
}
23+
24+
default String toString() {
25+
return "df";
26+
}
27+
}
28+
29+
class Book implements Sellable {
30+
private double price;
31+
private double discount;
32+
33+
public Book(double price, double discount) {
34+
this.price = price;
35+
this.discount = discount;
36+
}
37+
38+
@Override
39+
public double sell() {
40+
return price - (price * discount / 100.00);
41+
}
42+
}
43+
44+
class Pen implements Sellable {
45+
private double price;
46+
47+
public Pen(double price) {
48+
this.price = price;
49+
}
50+
51+
@Override
52+
public double sell() {
53+
return price;
54+
}
55+
}

defaultmethods/OverrideDemo.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class OverrideDemo {
2+
public static void main(String[] args) {
3+
Note note = new Note("Hello world");
4+
note.read();
5+
}
6+
}
7+
8+
interface Readable {
9+
default void read() {
10+
System.out.println("Read");
11+
}
12+
}
13+
14+
15+
class Note implements Readable {
16+
private String content;
17+
18+
public Note(String content) {
19+
this.content = content;
20+
}
21+
22+
@Override
23+
public void read() {
24+
System.out.println(content);
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public interface OverrideToString {
2+
/*
3+
* Compile time error.
4+
* Cannot override any methods of java.lang.Object class.
5+
*/
6+
public default String toString() {
7+
return "Hello";
8+
}
9+
}

0 commit comments

Comments
 (0)