-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOPS.java
75 lines (74 loc) · 1.7 KB
/
OOPS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
TODO: Understand some OOPS concepts
1. Class
2. Object
3. Inheritance
4. Constructor
5. Overloading
6. Overriding.
*/
class Dummy
{
void ext()
{
System.out.println("I am grand-parent function");
}
void ext(int a) // Overloading
{
System.out.println("I am overloaded grand-parent function");
}
}
public class OOPS extends Dummy // Inheritance
{
int marks;
String subj, name; // Instance variable
// A variable is instance variable when declared in class outside the function
// Constructior is another way to intialize the variables.
public OOPS()
{
marks = 0;
subj = "";
name = "";
}
public static void main(String args[])
{
OOPS obj = new OOPS();
OOPS obj1 = new OOPS(), obj2 = new OOPS(); // Creating multiple objects
System.out.println("Marks-"+obj.marks);
System.out.println("Subj = "+obj.subj+"\nName: "+obj.name);
Dummy obj3 = new Dummy();
obj3.ext();
obj3.ext(7);
}
void fun(int m, String n)
{
marks = m;
subj = n;
}
void ext() // Overriding
{
System.out.println("I am parent function");
}
}
class Abc extends OOPS // inherited class OOPS (Multi-level inheritance)
{
public static void main(String[] args)
{
OOPS obj = new OOPS();
OOPS obj1 = new OOPS();
// A way to initialize the object
obj.marks = 10;
obj.marks = 100;
obj.name = "Siddarth";
obj1.name = "Jha";
System.out.println("Marks: "+obj.marks);
System.out.println("Subj: "+obj.subj+"\nName: "+obj.name);
System.out.println("Another name: "+obj1.name);
// Another way to initialize object via method
obj1.fun(95, "Compiler");
System.out.println("Subj: "+obj1.subj+"\nMarks: "+obj1.marks);
new OOPS().fun(10, "Anonymous object");
// Annonymous object
obj.ext();
}
}