diff --git a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects.java b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects.java index 0905f5e..9bc860a 100644 --- a/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects.java +++ b/Chapter7/theoretical/Super_Class_References_and_Subclass_Objects.java @@ -86,7 +86,7 @@ public Triangle_2(String s, double w, double h) { } Triangle_2(Triangle_2 ob){ - + super(ob); } diff --git a/Chapter8/Interface/Implementing_Interfaces.java b/Chapter8/Interface/Implementing_Interfaces.java new file mode 100644 index 0000000..905d08e --- /dev/null +++ b/Chapter8/Interface/Implementing_Interfaces.java @@ -0,0 +1,139 @@ +package Interface; + +import Cpp.this_keyword; + +//An interface specifies what must be done, but not how to get it done. +interface Series4{ + + int getNext(); + void reset(); + void setStart(int x); +} + + +//Implement Series. +class ByTwos2_ implements Series4{ + + // It's both permissible and common for classes that implement interfaces to define additional members + // of their own. (It absolutely is. Without it, how to implement methods in an interface?) + int start; + int value; + + public ByTwos2_() { + start = 0; + value = 0; + } + + // Whenever you implement a method defined by an interface, it must be implemented as public; + // because all members of an interface are implicitly public. + public int getNext() { + value += 2; + return value; + } + + public void reset() { + value = start; + } + + @Override + public void setStart(int x) { + + start = x; + value = x; + + } +} + + +class ByTwos4 implements Series4{ + int start; + int value; + int previous; + + public ByTwos4() { + start = 0; + value = 0; + } + + ByTwos4(int previous) { + + this.previous = previous; + + start = 0; + value = 0; + } + + // Whenever you implement a method defined by an interface, it must be implemented as public; + // because all members of an interface are implicitly public. + public int getNext() { + value += 2; + return value; + } + + public void reset() { + value = start; + } + + @Override + public void setStart(int x) { + + start = x; + value = x; + + } + + // Even though the interface does not define this method, it's both permissible and common for classes + // that implement interfaces to define additional members of their own. + int getPrevious() { + return previous; + } +} + + +class ByThrees2 implements Series4{ // implement Series in a different way. + int start; + int value; + + ByThrees2() { + // TODO Auto-generated constructor stub + start = 0; + value = 0; + } + + public int getNext() { + value += 3; + return value; + } + + public void reset() { + value = start; + } + + public void setStart(int x) { + start = x; + value = x; + } +} + + +// If a class includes an interface but does not fully implement the methods defined by that interface, then +// that class must be declared as abstract. +abstract class implicate implements Series4{ + + int start; + + public void setStart(int start) { + this.start = start; + } + +} + + +public class Implementing_Interfaces { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} diff --git a/Chapter8/Interface/Series_Demo.java b/Chapter8/Interface/Series_Demo.java new file mode 100644 index 0000000..8e259e4 --- /dev/null +++ b/Chapter8/Interface/Series_Demo.java @@ -0,0 +1,71 @@ +package Interface; + +// An interface specifies what must be done, but not how to do it. +interface Series2{ + + int getNext(); + void reset(); + void setStart(int x); +} + + +// Implement Series. +class ByTwos2 implements Series2{ + int start; + int value; + + public ByTwos2() { + start = 0; + value = 0; + } + + // Whenever you implement a method defined by an interface, it must be implemented as public; + // because all members of an interface are implicitly public. + public int getNext() { + value += 2; + return value; + } + + public void reset() { + value = start; + } + + @Override + public void setStart(int x) { + + start = x; + value = x; + + } +} + +public class Series_Demo { + + public static void main(String[] args) { + + ByTwos2 oByTwos2 = new ByTwos2(); + + for (int i = 0; i < 5; i++) { + + System.out.println("Next value is " + oByTwos2.getNext()); + } + + System.out.println("\nResetting"); + + oByTwos2.reset(); + + for (int i = 0; i < 5; i++) { + System.out.println("Next value is " + oByTwos2.getNext()); + } + + System.out.println("\nStarting at 100"); + + oByTwos2.setStart(100); + + for (int i = 0; i < 5; i++) { + System.out.println("Next value is " + oByTwos2.getNext()); + } + + } + +} diff --git a/Chapter8/Interface/Series_Demo2.java b/Chapter8/Interface/Series_Demo2.java new file mode 100644 index 0000000..cc89a73 --- /dev/null +++ b/Chapter8/Interface/Series_Demo2.java @@ -0,0 +1,86 @@ +package Interface; + +//An interface specifies what must be done, but not how to do it. +interface Series3{ + + int getNext(); + void reset(); + void setStart(int x); +} + + +//Implement Series. +class ByTwos3 implements Series3{ + int start; + int value; + int previous; + + public ByTwos3() { + start = 0; + value = 0; + } + + ByTwos3(int previous) { + + this.previous = previous; + + start = 0; + value = 0; + } + + // Whenever you implement a method defined by an interface, it must be implemented as public; + // because all members of an interface are implicitly public. + public int getNext() { + value += 2; + return value; + } + + public void reset() { + value = start; + } + + @Override + public void setStart(int x) { + + start = x; + value = x; + + } + + int getPrevious() { + return previous; + } +} + +public class Series_Demo2 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + ByTwos3 obByTwos3 = new ByTwos3(9); + + + for (int i = 0; i < 5; i++) { + System.out.println("Next value is " + obByTwos3.getNext()); + } + + System.out.println("\nResetting"); + + obByTwos3.reset(); + + for (int i = 0; i < 5; i++) { + System.out.println("Next value is " + obByTwos3.getNext()); + } + + System.out.println("\nStarting at 100"); + + obByTwos3.setStart(100); + + for (int i = 0; i < 5; i++) { + + System.out.println("Next value is " + obByTwos3.getNext()); + } + + } + +}