Skip to content

Commit 20083e0

Browse files
init abstract class
added abstract class with: - abstract and non-abstract methods - all access modifiers (default, public, private, protected) - static and non-static fields - final and non-final fields - constructor
1 parent 62d0468 commit 20083e0

File tree

9 files changed

+200
-1
lines changed

9 files changed

+200
-1
lines changed

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

abstract_class/abstract_class.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="17" jdkType="JavaSDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

abstract_class/src/Human.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
abstract class Human {
2+
3+
/*
4+
* METHODS:
5+
* - can be both abstract and non-abstract
6+
* - non-abstract methods can have all access modifiers (default, public, private, protected)
7+
* - abstract methods can have all access modifiers (default, protected, public) EXCEPT private
8+
* */
9+
10+
// DEFAULT + NON-ABSTRACT
11+
void defaultDisplayNonAbstract(){
12+
System.out.println("Hello World from defaultDisplayNonAbstract");
13+
}
14+
15+
// PUBLIC + NON-ABSTRACT
16+
public void publicDisplayNonAbstract(){
17+
System.out.println("Hello World publicDisplayNonAbstract");
18+
}
19+
20+
// PRIVATE + NON-ABSTRACT
21+
private void privateDisplayNonAbstract(){
22+
System.out.println("Hello World from privateDisplayNonAbstract");
23+
}
24+
25+
// PROTECTED + NON-ABSTRACT
26+
protected void protectedDisplayNonAbstract(){
27+
System.out.println("Hello World from protectedDisplayNonAbstract");
28+
}
29+
30+
// DEFAULT + ABSTRACT
31+
abstract void defaultDisplayAbstract();
32+
33+
// PUBLIC + ABSTRACT
34+
abstract public void publicDisplayAbstract();
35+
36+
// PRIVATE + ABSTRACT
37+
// Illegal combination of modifiers: 'abstract' and 'private'
38+
//abstract private void privateDisplayAbstract();
39+
40+
// PROTECTED + ABSTRACT
41+
abstract protected void protectedDisplayAbstract();
42+
43+
44+
/*
45+
* FIELDS:
46+
* - all access modifiers are allowed (default, public, private, protected)
47+
* - fields can be static and non-static
48+
* - fields can be final and non-final
49+
* */
50+
int a;
51+
private int b;
52+
public int c;
53+
protected int d;
54+
55+
final int e = 10;
56+
private final int f = 20;
57+
public final int g = 30;
58+
protected final int h = 40;
59+
60+
static int i = 10;
61+
private static int j = 20;
62+
public static int k = 30;
63+
protected static int l = 40;
64+
65+
static final int m = 10;
66+
private static final int n = 20;
67+
public static final int o = 30;
68+
protected static final int p = 40;
69+
70+
// Constructor ONLY takes fields that are non-static and non-final
71+
// since final and static fields CANNOT BE CHANGED
72+
public Human(
73+
// NON-STATIC AND NON-FINAL
74+
int a, int b, int c, int d,
75+
// STATIC
76+
int i, int j, int k, int l
77+
// FINAL -> Cannot assign a value to final variable
78+
// STATIC + FINAL -> Cannot assign a value to final variable
79+
) {
80+
// NON-STATIC AND NON-FINAL
81+
this.a = a;
82+
this.b = b;
83+
this.c = c;
84+
this.d = d;
85+
86+
// FINAL
87+
// this.e = e; -> Cannot assign a value to final variable 'e'
88+
// this.f = f; -> Cannot assign a value to final variable 'f'
89+
// this.g = g; -> Cannot assign a value to final variable 'g'
90+
// this.h = h; -> Cannot assign a value to final variable 'h'
91+
92+
// STATIC
93+
Human.i = i;
94+
Human.j = j;
95+
Human.k = k;
96+
Human.l = l;
97+
98+
// STATIC + FINAL
99+
// this.m = m; -> Cannot assign a value to final variable 'm'
100+
// this.n = n; -> Cannot assign a value to final variable 'n'
101+
// this.o = o; -> Cannot assign a value to final variable 'o'
102+
// this.p = p; -> Cannot assign a value to final variable 'p'
103+
}
104+
}

abstract_class/src/Main.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
// Zombie zombie = new Zombie(); -> Expected 8 arguments but found 0
4+
5+
Zombie zombie = new Zombie(
6+
10, 20, 30, 40,
7+
50, 60, 70, 80);
8+
9+
// NON-FINAL AND NON-STATIC
10+
System.out.println("NON-FINAL AND NON-STATIC");
11+
System.out.println("zombie.a: " + zombie.a);
12+
// System.out.println(zombie.b); -> 'b' has private access in 'Human
13+
System.out.println("zombie.c: " + zombie.c);
14+
System.out.println("zombie.d: " + zombie.d);
15+
System.out.println();
16+
17+
// FINAL
18+
System.out.println("FINAL");
19+
System.out.println("zombie.e: " + zombie.e);
20+
// System.out.println(zombie.f); -> 'f' has private access in 'Human'
21+
System.out.println("zombie.g: " + zombie.g);
22+
System.out.println("zombie.h: " + zombie.h);
23+
System.out.println();
24+
25+
// STATIC
26+
System.out.println("STATIC");
27+
System.out.println("Zombie.i: " + Zombie.i);
28+
// System.out.println(Zombie.j); -> 'j' has private access in 'Human'
29+
System.out.println("Zombie.k: " + Zombie.k);
30+
System.out.println("Zombie.l: " + Zombie.l);
31+
System.out.println();
32+
33+
// STATIC + FINAL
34+
System.out.println("STATIC + FINAL");
35+
System.out.println("Zombie.m: " + Zombie.m);
36+
// System.out.println(Zombie.n); -> 'n' has private access in 'Human'
37+
System.out.println("Zombie.m: " + Zombie.o);
38+
System.out.println("Zombie.m: " + Zombie.p);
39+
System.out.println();
40+
41+
42+
// ABSTRACT METHODS
43+
System.out.println("ABSTRACT METHODS");
44+
zombie.defaultDisplayAbstract();
45+
zombie.publicDisplayAbstract();
46+
// zombie.privateDisplayAbstract(); -> 'privateDisplayAbstract()' has private access in 'Human'
47+
zombie.protectedDisplayAbstract();
48+
System.out.println();
49+
50+
// NON-ABSTRACT METHODS
51+
System.out.println("NON-ABSTRACT METHODS");
52+
zombie.defaultDisplayNonAbstract();
53+
zombie.publicDisplayNonAbstract();
54+
// zombie.privateDisplayNonAbstract(); -> 'privateDisplayNonAbstract()' has private access in 'Human'
55+
zombie.protectedDisplayNonAbstract();
56+
}
57+
}

abstract_class/src/Zombie.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Zombie extends Human{
2+
3+
4+
@Override
5+
void defaultDisplayAbstract() {
6+
System.out.println("Hello World defaultDisplayAbstract from Zombie class");
7+
}
8+
9+
@Override
10+
public void publicDisplayAbstract() {
11+
System.out.println("Hello World publicDisplayAbstract from Zombie class");
12+
}
13+
14+
@Override
15+
protected void protectedDisplayAbstract() {
16+
System.out.println("Hello World protectedDisplayAbstract from Zombie class");
17+
}
18+
19+
public Zombie(int a, int b, int c, int d, int i, int j, int k, int l) {
20+
super(a, b, c, d, i, j, k, l);
21+
}
22+
}

access_modifiers/src/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import package_1.Demo1;
2-
2+
/*
3+
* Sources:
4+
* https://dxc.udemy.com/course/java-se-programming/learn/lecture/18027383#overview
5+
* https://dxc.udemy.com/course/java-se-programming/learn/lecture/18585551#overview
6+
* */
37
public class Main {
48
public static void main(String[] args) {
59
Demo1 demo1 = new Demo1();
1.72 KB
Binary file not shown.
2.16 KB
Binary file not shown.
1.04 KB
Binary file not shown.

0 commit comments

Comments
 (0)