Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 606baba

Browse files
add new core java solutions Aug 10
1 parent ff49a94 commit 606baba

File tree

16 files changed

+258
-0
lines changed

16 files changed

+258
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package CovariantReturnType;
2+
3+
public class Car extends Vehicle {
4+
5+
public int a = 2;
6+
7+
@Override
8+
public Car getInstance() {
9+
return new Car();
10+
}
11+
12+
public static void main(String[] args) {
13+
Vehicle vehicle = new Vehicle();
14+
Car car = new Car();
15+
16+
Vehicle vehicle1 = vehicle.getInstance();
17+
Car car1 = car.getInstance();
18+
19+
System.out.println(vehicle1.b);
20+
System.out.println(car1.a);
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package CovariantReturnType;
2+
3+
public class Vehicle {
4+
5+
public int b = 3;
6+
public Vehicle getInstance() {
7+
return new Vehicle();
8+
}
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package FunctionalInterfacesInJDK;
2+
3+
import java.util.function.Consumer;
4+
import java.util.function.Function;
5+
import java.util.function.Predicate;
6+
import java.util.function.Supplier;
7+
8+
public class FuncInterfacesDemo {
9+
10+
public static void greet(String name) {
11+
System.out.println("Hello " + name);
12+
}
13+
14+
public static void main(String[] args) {
15+
16+
Consumer<String> consumer1 = FuncInterfacesDemo::greet; // Using method reference
17+
consumer1.accept("Biplob"); // No return
18+
Consumer<String> consumer2 = name -> System.out.println("Hello " + name); // Using Lambda Expression
19+
consumer2.accept("Imam");
20+
21+
Predicate<Integer> predicate = number -> number > 10;
22+
System.out.println(predicate.test(4)); // Boolean return
23+
24+
25+
Function<String, Boolean> function = name -> name.contains("m");
26+
System.out.println(function.apply("Imam"));
27+
28+
Supplier<Integer> supplier = () -> 2+2;
29+
System.out.println(supplier.get());
30+
31+
}
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package InheritanceAndInterface;
2+
3+
public interface Colorable {
4+
void setColor(String color);
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package InheritanceAndInterface;
2+
3+
public interface Shape {
4+
double getArea();
5+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package InheritanceAndInterface;
2+
3+
public class Square implements Shape, Colorable {
4+
5+
6+
private double side;
7+
private String color;
8+
9+
public Square(double side) {
10+
this.side = side;
11+
}
12+
13+
public double getSide() {
14+
return side;
15+
}
16+
17+
public void setSide(double side) {
18+
this.side = side;
19+
}
20+
21+
public String getColor() {
22+
return color;
23+
}
24+
25+
@Override
26+
public void setColor(String color) {
27+
this.color = color;
28+
}
29+
30+
@Override
31+
public double getArea() {
32+
return side*side;
33+
}
34+
35+
public static void main(String[] args) {
36+
37+
Square square = new Square(2.0);
38+
39+
square.setColor("Green");
40+
41+
System.out.println(square.getArea());
42+
System.out.println(square.getColor());
43+
44+
}
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package InterfaceDefaultAndStaticMethodsConflict;
2+
3+
public class MyClass implements MyInterface {
4+
static void staticMethod() {
5+
System.out.println("Static Method of Class");
6+
}
7+
8+
@Override
9+
public void myMethod() {
10+
System.out.println("My Method from Class");
11+
}
12+
13+
public static void main(String[] args) {
14+
MyInterface conflict = new MyClass();
15+
conflict.myMethod();
16+
MyInterface.staticMethod();
17+
MyClass.staticMethod();
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package InterfaceDefaultAndStaticMethodsConflict;
2+
3+
public interface MyInterface {
4+
5+
default void myMethod() {
6+
System.out.println("My Method");
7+
}
8+
9+
static void staticMethod() {
10+
System.out.println("Static Method of Interface");
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package InterfaceInheritance;
2+
3+
public interface Drawable {
4+
void draw();
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /