Skip to content

Commit 703a63f

Browse files
committed
updated java syntax to contain:
identifiers modifiers variables enums inheritance interfaces
1 parent 3ae3909 commit 703a63f

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

SYNTAX.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,55 @@ Example:
2323
Assume `MyFirstJavaProgram` is the class name, the file should be saved as `MyFirstJavaProgram.java`
2424

2525
### public static void main(String args[])
26-
Java processing starts from the `main()` method which is a mandatory part of every Java program
26+
Java processing starts from the `main()` method which is a mandatory part of every Java program
27+
28+
### Java identifiers
29+
All java components require names. These names (classes, variables, methods) are called identifiers
30+
points to note:
31+
- all identifiers should begin with letters (a-z, A-Z) or currency characters or underscores
32+
- key word cannot be used as an identifier
33+
- Identifiers are case sensitive, (classes can only begin in caps, methods cannot begin in caps)
34+
35+
### Java modifiers
36+
Used to make changes to classes. There are two categories:
37+
- Access modifies - default, public, protected, private
38+
- Non Access modifiers - final, abstract, strictfp
39+
40+
### Java variables
41+
the following are java variables
42+
- Local variables
43+
- Class variables (Static variables)
44+
- Instance variables (non static variables)
45+
46+
### Java enums
47+
They restrict variables to have a few pre-defined values
48+
49+
Example:
50+
51+
if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium, and large. This would make sure that it would not allow anyone to order any size other than small, medium, or large.
52+
53+
```java
54+
class FreshJuice{
55+
enum FreshJuiceSize {Small, Medium, Large}
56+
FreshJuiceSize size;
57+
}
58+
59+
public class FreshJuiceTest {
60+
61+
public static void main(String args[]) {
62+
FreshJuice juice = new FreshJuice();
63+
juice.size = FreshJuice.FreshJuiceSize.Medium;
64+
System.out.println("size" + juice.size);
65+
}
66+
}
67+
```
68+
69+
### Inheritance
70+
In Java, classes can be derived from classes. Basically, if you need to create a new class and here is already a class that has some of the code you require, then it is possible to derive your new class from the already existing code.
71+
72+
This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in a new class. In this scenario, the existing class is called the `superclass` and the derived class is called the `subclass`.
73+
74+
### Interfaces
75+
In Java language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play a vital role when it comes to the concept of inheritance.
76+
77+
An interface defines the methods, a deriving class (subclass) should use. But the implementation of the methods is totally up to the subclass.

0 commit comments

Comments
 (0)