Skip to content

Commit 0159b61

Browse files
authored
Add files via upload
1 parent 66c987f commit 0159b61

97 files changed

Lines changed: 5906 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
author: Jaydatt Patel
3+
Abstract Classes and Methods:
4+
Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.
5+
The abstract keyword is a non-access modifier, used for classes and methods:
6+
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
7+
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
8+
*/
9+
10+
public class Abstract_class_and_methods {
11+
12+
static abstract class Base {
13+
abstract void fun(); // Abstract method (does not have a body)
14+
public void fun2() // Regular method
15+
{
16+
System.out.println("Base fun2() called");
17+
}
18+
19+
}
20+
21+
static class Derived extends Base {
22+
void fun()
23+
{
24+
System.out.println("Derived fun() called");
25+
}
26+
}
27+
28+
public static void main(String args[])
29+
{
30+
Base b = new Derived();
31+
b.fun();
32+
b.fun2();
33+
34+
}
35+
36+
}
342 Bytes
Binary file not shown.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Java annotations are used to provide meta data for Java code. Being meta data, Java
5+
annotations do not directly affect the execution of the code, although some types of
6+
annotations can actually be used for that purpose. Java annotations were added to Java from
7+
Annotations are used to provide supplemental information about a program.
8+
9+
- Annotations start with ‘@’.
10+
- Annotations do not change the action of a compiled program.
11+
- Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors methods, classes, etc.
12+
- Annotations are not pure comments as they can change the way a program is treated by the compiler. See below code for example.
13+
- Annotations basically are used to provide additional information, so could be an alternative to XML and Java marker interfaces.
14+
15+
Predefined/ Standard Annotations:
16+
Java popularly defines seven built-in annotations as we have seen up in the hierarchy diagram.
17+
Four are imported from java.lang.annotation:
18+
@Retention, @Documented, @Target, @Inherited.
19+
Three are included in java.lang:
20+
@Deprecated, @Override and @SuppressWarnings
21+
22+
1. @Override: Indicates that a method in a subclass is intended to override a method in its superclass.
23+
2. @Deprecated: Marks a method, class, or field as deprecated, indicating that it is no longer recommended for use.
24+
3. @SuppressWarnings: Suppresses compiler warnings for a specific part of the code.
25+
4. @FunctionalInterface: Indicates that an interface is intended to be a functional interface, which can have only one abstract method (SAM - Single Abstract Method).
26+
5. @Retention: The @Retention annotation is used to specify how long an annotation should be retained. There are three retention policies:
27+
RetentionPolicy.SOURCE: The annotation is retained only in the source code and is discarded during compilation.
28+
RetentionPolicy.CLASS: The annotation is retained in the .class files but not available at runtime.
29+
RetentionPolicy.RUNTIME: The annotation is retained in the .class files and is available at runtime.
30+
31+
6. @Documented: The @Documented annotation indicates that the annotation should be included in the documentation generated by tools like Javadoc.
32+
7. @Target: The @Target annotation specifies where the annotation can be applied, such as on classes, methods, fields, etc.
33+
8. @Inherited: The @Inherited annotation indicates that an annotation type should be inherited to subclasses if it is annotated on a superclass.
34+
35+
36+
There are three types of annotations.
37+
1. Marker Annotation
38+
2. Single-Value Annotation
39+
3. Multi-Value Annotation
40+
41+
1. Marker Annotation : An annotation that has no method, is called marker annotation.
42+
@interface MyAnnotation{}
43+
44+
2. Single-Value Annotation : An annotation that has one method, is called single-value annotation.
45+
@interface MyAnnotation{
46+
int value() [optional : default 0];
47+
}
48+
49+
3. Multi-Value Annotation : An annotation that has more than one method, is called Multi-Value annotation. Default is optional.
50+
@interface MyAnnotation {
51+
int value1() default 1;
52+
String value2() default "";
53+
String value3() default "xyz";
54+
}
55+
56+
Let's see the code to apply the multi-value annotation :
57+
@MyAnnotation(value1=10,value2="ArunKumar",value3="Ghaziabad")
58+
*/
59+
60+
61+
class oldClass{
62+
63+
@Deprecated //deprecated gives warnning for no longer recommended to used.
64+
void fun(){
65+
System.out.println("---fun---");
66+
}
67+
}
68+
69+
class Annotation_Deprecated{
70+
71+
public static void main(String args[]){
72+
oldClass obj = new oldClass();
73+
obj.fun();
74+
75+
}
76+
}
77+
78+
/**
79+
Output :
80+
81+
Note: Annotation_Deprecated.java uses or overrides a deprecated API.
82+
Note: Recompile with -Xlint:deprecation for details.
83+
---fun---
84+
85+
*/
490 Bytes
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Java annotations are used to provide meta data for Java code. Being meta data, Java
5+
annotations do not directly affect the execution of the code, although some types of
6+
annotations can actually be used for that purpose. Java annotations were added to Java from
7+
Annotations are used to provide supplemental information about a program.
8+
9+
- Annotations start with ‘@’.
10+
- Annotations do not change the action of a compiled program.
11+
- Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors methods, classes, etc.
12+
- Annotations are not pure comments as they can change the way a program is treated by the compiler. See below code for example.
13+
- Annotations basically are used to provide additional information, so could be an alternative to XML and Java marker interfaces.
14+
15+
Predefined/ Standard Annotations:
16+
Java popularly defines seven built-in annotations as we have seen up in the hierarchy diagram.
17+
Four are imported from java.lang.annotation:
18+
@Retention, @Documented, @Target, @Inherited.
19+
Three are included in java.lang:
20+
@Deprecated, @Override and @SuppressWarnings
21+
22+
1. @Override: Indicates that a method in a subclass is intended to override a method in its superclass.
23+
2. @Deprecated: Marks a method, class, or field as deprecated, indicating that it is no longer recommended for use.
24+
3. @SuppressWarnings: Suppresses compiler warnings for a specific part of the code.
25+
4. @FunctionalInterface: Indicates that an interface is intended to be a functional interface, which can have only one abstract method (SAM - Single Abstract Method).
26+
5. @Retention: The @Retention annotation is used to specify how long an annotation should be retained. There are three retention policies:
27+
RetentionPolicy.SOURCE: The annotation is retained only in the source code and is discarded during compilation.
28+
RetentionPolicy.CLASS: The annotation is retained in the .class files but not available at runtime.
29+
RetentionPolicy.RUNTIME: The annotation is retained in the .class files and is available at runtime.
30+
31+
6. @Documented: The @Documented annotation indicates that the annotation should be included in the documentation generated by tools like Javadoc.
32+
7. @Target: The @Target annotation specifies where the annotation can be applied, such as on classes, methods, fields, etc.
33+
8. @Inherited: The @Inherited annotation indicates that an annotation type should be inherited to subclasses if it is annotated on a superclass.
34+
35+
36+
There are three types of annotations.
37+
1. Marker Annotation
38+
2. Single-Value Annotation
39+
3. Multi-Value Annotation
40+
41+
1. Marker Annotation : An annotation that has no method, is called marker annotation.
42+
@interface MyAnnotation{}
43+
44+
2. Single-Value Annotation : An annotation that has one method, is called single-value annotation.
45+
@interface MyAnnotation{
46+
int value() [optional : default 0];
47+
}
48+
49+
3. Multi-Value Annotation : An annotation that has more than one method, is called Multi-Value annotation. Default is optional.
50+
@interface MyAnnotation {
51+
int value1() default 1;
52+
String value2() default "";
53+
String value3() default "xyz";
54+
}
55+
56+
Let's see the code to apply the multi-value annotation :
57+
@MyAnnotation(value1=10,value2="ArunKumar",value3="Ghaziabad")
58+
59+
*/
60+
61+
62+
public class Annotation_FunctionalInterface{
63+
64+
//FunctionalInterface allow only one abstract method. (SAM - Single Abstract Method)
65+
@FunctionalInterface
66+
interface TestAnnotation {
67+
String Developer();
68+
//String Expirydate(); //Error: only one abstract method allowed. Must remove second method.
69+
}
70+
71+
static class Test implements TestAnnotation{
72+
String Developer() {
73+
return "Rahul";
74+
}
75+
}
76+
77+
public static void main(String args[]){
78+
System.out.println("Hello");
79+
}
80+
}
81+
/*
82+
83+
Output Error:
84+
85+
@FunctionalInterface
86+
^
87+
TestAnnotation is not a functional interface
88+
multiple non-overriding abstract methods found in interface TestAnnotation
89+
1 error
90+
91+
*/
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Java annotations are used to provide meta data for Java code. Being meta data, Java
5+
annotations do not directly affect the execution of the code, although some types of
6+
annotations can actually be used for that purpose. Java annotations were added to Java from
7+
Annotations are used to provide supplemental information about a program.
8+
9+
- Annotations start with ‘@’.
10+
- Annotations do not change the action of a compiled program.
11+
- Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors methods, classes, etc.
12+
- Annotations are not pure comments as they can change the way a program is treated by the compiler. See below code for example.
13+
- Annotations basically are used to provide additional information, so could be an alternative to XML and Java marker interfaces.
14+
15+
Predefined/ Standard Annotations:
16+
Java popularly defines seven built-in annotations as we have seen up in the hierarchy diagram.
17+
Four are imported from java.lang.annotation:
18+
@Retention, @Documented, @Target, @Inherited.
19+
Three are included in java.lang:
20+
@Deprecated, @Override and @SuppressWarnings
21+
22+
1. @Override: Indicates that a method in a subclass is intended to override a method in its superclass.
23+
2. @Deprecated: Marks a method, class, or field as deprecated, indicating that it is no longer recommended for use.
24+
3. @SuppressWarnings: Suppresses compiler warnings for a specific part of the code.
25+
4. @FunctionalInterface: Indicates that an interface is intended to be a functional interface, which can have only one abstract method (SAM - Single Abstract Method).
26+
5. @Retention: The @Retention annotation is used to specify how long an annotation should be retained. There are three retention policies:
27+
RetentionPolicy.SOURCE: The annotation is retained only in the source code and is discarded during compilation.
28+
RetentionPolicy.CLASS: The annotation is retained in the .class files but not available at runtime.
29+
RetentionPolicy.RUNTIME: The annotation is retained in the .class files and is available at runtime.
30+
31+
6. @Documented: The @Documented annotation indicates that the annotation should be included in the documentation generated by tools like Javadoc.
32+
7. @Target: The @Target annotation specifies where the annotation can be applied, such as on classes, methods, fields, etc.
33+
8. @Inherited: The @Inherited annotation indicates that an annotation type should be inherited to subclasses if it is annotated on a superclass.
34+
35+
36+
There are three types of annotations.
37+
1. Marker Annotation
38+
2. Single-Value Annotation
39+
3. Multi-Value Annotation
40+
41+
1. Marker Annotation : An annotation that has no method, is called marker annotation.
42+
@interface MyAnnotation{}
43+
44+
2. Single-Value Annotation : An annotation that has one method, is called single-value annotation.
45+
@interface MyAnnotation{
46+
int value() [optional : default 0];
47+
}
48+
49+
3. Multi-Value Annotation : An annotation that has more than one method, is called Multi-Value annotation. Default is optional.
50+
@interface MyAnnotation {
51+
int value1() default 1;
52+
String value2() default "";
53+
String value3() default "xyz";
54+
}
55+
56+
Let's see the code to apply the multi-value annotation :
57+
@MyAnnotation(value1=10,value2="ArunKumar",value3="Ghaziabad")
58+
59+
*/
60+
61+
62+
import java.lang.annotation.Documented;
63+
import java.lang.annotation.Retention;
64+
import java.lang.annotation.RetentionPolicy;
65+
66+
// User-defined annotation
67+
@Documented
68+
@Retention(RetentionPolicy.RUNTIME)
69+
@ interface TestAnnotation
70+
{
71+
String Developer() default "Rahul";
72+
String Expirydate();
73+
} // will be retained at runtime
74+
75+
76+
public class Annotation_Interface_Custom{
77+
78+
@TestAnnotation(Developer="Rahul", Expirydate="01-10-2020")
79+
void fun1(){
80+
System.out.println("Test method 1");
81+
}
82+
83+
@TestAnnotation(Developer="Anil", Expirydate="01-10-2021")
84+
void fun2(){
85+
System.out.println("Test method 2");
86+
}
87+
88+
public static void main(String args[]){
89+
System.out.println("Hello");
90+
}
91+
}

0 commit comments

Comments
 (0)