Java
/
OOP
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
Contract
p225 Every class that implements an interfece must override all interface methods.
/**
* An interface is forcing the developer to implement methods ...
* it is like a contract.
*
* In an interface everything must be an abstract method.
*/
package com.minte9.oop.interfaces;
public class Contract {
public static void main(String[] args) {
Dog d = new Dog();
d.play();
// Dog is playing
}
}
class Dog implements Canine { // Look Here
@Override
public void play() {
System.out.println("Dog is playing");
}
@Override
public void eat() {
System.out.println("Dog is eating");
}
}
interface Canine {
// no class keyword
public abstract void play();
public void eat();
// abstract by default
// public void move() {};
// Error: Interface methods cannot have body
}
interface Feline {
public abstract void play();
}
Legacy
Extending interfaces is not recommended.
/**
* When you add methods to an interface, current related classes will break.
* You'll need to create more interfaces that extend initial interface.
*
* Where is possible, it is better to createa a new interface.
*/
package com.minte9.oop.interfaces;
public class Legacy {
public static void main(String[] args) {
new LA().setvalue(); // LA - setvalue
new LB().settype(); // LB - settype
}
}
class LA implements A {
@Override public void setvalue() {
System.out.println("LA - setvalue");
}
}
class LB implements B {
@Override public void setvalue() {
System.out.println("LB - setvalue");
}
@Override public void settype() {
System.out.println("LB - settype");
}
}
interface B extends A { // Look Here
public void settype();
}
interface A {
public void setvalue();
}
Default Method
Alternatively, you can define your new methods as default methods.
/**
* Default keyword allows you to add implementation in interfaces.
* Non default methods must be all define as abstract.
*/
package com.minte9.oop.interfaces;
public class DefaultMethod {
public static void main(String[] args) {
new AC().output(); // A - output
new BC().output(); // B - output
new AC().output_new(); // C - default output_new
new BC().output_new(); // C - default output_new
}
}
class AC implements C {
@Override public void output() {
System.out.println("A - output");
}
}
class BC implements C {
@Override public void output() {
System.out.println("B - output");
}
}
interface C {
void output();
default void output_new() { // Look Here
System.out.println("C - default output_new");
};
}
➥ Questions