Skip to content

Commit b4e0d69

Browse files
committed
java day 7 examples
1 parent 7f2a770 commit b4e0d69

File tree

15 files changed

+1122
-0
lines changed

15 files changed

+1122
-0
lines changed

java/day7/day7.txt

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
* relationships between classes
2+
real life scenario we have to handle multiple classes that exists seperately/individually and are related to each other.
3+
it implements reusability
4+
5+
classes communicate with each other through their objects.
6+
7+
3 types of relationship between the classes
8+
** agreegation - object reference variable of one class is member variable of another class
9+
"has a" relationship
10+
11+
order - food
12+
emp - gadget
13+
student - study_material
14+
library - books
15+
dept - emp
16+
17+
18+
** association
19+
Association
20+
in association the classes communicate through methods. i.e. for functionality of one class it can use object of another class
21+
22+
"uses a" relationship
23+
24+
** aggregation VS association
25+
emp has gadget aggregation
26+
salary uses employee association
27+
28+
order has food aggregation
29+
bill uses order association
30+
31+
32+
** inheritance => one of the pillars of OOP
33+
"is type of" relationship
34+
parent - child relationship
35+
child class inherits non-private properties of parent class
36+
parent class => base class/ super class
37+
child class => derived class/ sub class
38+
In java inheriting properties from only one parent class is supported.
39+
all parts of parent class that are non-private properties including member varibles, constructors, methods of parent class are directly accessible in child class as if these are its own properties.
40+
41+
* to implement inheritance betwwen classes extends keyword is used
42+
43+
* non-private members are directly accessible in child class. private members are accessible through non-private methods of parent class
44+
45+
* a child class can inherit properties of only one class
46+
47+
* parent class do not have access to child class
48+
49+
* when an object of child class is created , by default, parameterless constructor of parent class is called followed by execution of child class constructor.
50+
to call parameterized constructor of parent class
51+
use super keyword with appropriate parameters.
52+
call to parameterized constructor of parent class should be the first statement in the child class constructor.
53+
54+
* if static block is present in parent and/or child class then the sequence of execution at the time of child class object creation is
55+
static block of parent class
56+
static block of child class
57+
constructor of parent class
58+
constructor of child class
59+
60+
* convention
61+
according to OOP when inheritance is used, then object of parent class is very rarely used. properties and functionalities of parent class are used through object of child class.
62+
63+
64+
* types of inheritance
65+
# single P - C
66+
one super class one sub class
67+
Emp(eno,ename,sal) - Programmer (skill)
68+
69+
# hierarchical
70+
P - C1, P - C2 => P - C1,C2
71+
one superclass, 2 or more sub classes
72+
Emp(eno,ename,sal) - Programmer (skill), Tester (type)
73+
74+
# multilevel
75+
P-C-G
76+
one superclass one subclass , subclass - another subclass
77+
Emp(eno,ename,sal) - Programmer (skill)-TeamLead (teanSize)
78+
79+
#multiple inheritance
80+
P1,P2 - C not supported in JAVA for classes
81+
82+
83+
84+
85+

java/day7/inh1.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Parent
2+
{
3+
private int pv;
4+
protected int pt;
5+
int pk;
6+
public int pb;
7+
8+
public Parent()
9+
{
10+
this.pv=1;
11+
this.pt=2;
12+
this.pk=3;
13+
this.pb=4;
14+
}
15+
public int getPV(){return pv;}
16+
public void displayParent()
17+
{
18+
System.out.println("private pv = "+this.pv);
19+
System.out.println("protected pt = "+this.pt);
20+
System.out.println("default/package pk = "+this.pk);
21+
System.out.println("public pb = "+this.pb);
22+
}
23+
24+
}
25+
class Child extends Parent
26+
{
27+
private int y;
28+
public Child(){this.y=100;}
29+
public void dispY(){System.out.println(" y = "+this.y);}
30+
public void displayAll()
31+
{
32+
//System.out.println("private pv = "+this.pv); // only non-private properties of parent class are directly accessible
33+
System.out.println("private pv = "+this.getPV()); // private properties of parent class are accessible through non-private methods of parent class
34+
System.out.println("protected pt = "+this.pt);
35+
System.out.println("default/package pk = "+this.pk);
36+
System.out.println("public pb = "+this.pb);
37+
System.out.println(" y = "+this.y);
38+
}
39+
40+
}
41+
class Demo
42+
{
43+
public static void main(String[] args)
44+
{
45+
Parent p1=new Parent();
46+
p1.displayParent();
47+
//p1.dispY(); // properties of child class are not accessible to parent class
48+
Child ch1=new Child();
49+
ch1.displayParent();
50+
ch1.dispY();
51+
ch1.displayAll();
52+
53+
54+
}
55+
}
56+

java/day7/inh2.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// inheritance demo example 2 constructors
2+
3+
class Parent
4+
{
5+
private int pv;
6+
7+
public Parent()
8+
{
9+
System.out.println("parameterless constructor of parent class");
10+
this.pv=1;
11+
12+
}
13+
public Parent(int pv)
14+
{
15+
System.out.println("parameterised constructor of parent class");
16+
this.pv=pv;
17+
18+
}
19+
20+
public int getPV(){return pv;}
21+
public void setPV(int pv){this.pv=pv;}
22+
23+
public void displayParent()
24+
{
25+
System.out.println("private pv = "+this.pv);
26+
}
27+
28+
}
29+
class Child extends Parent
30+
{
31+
private int y;
32+
public Child()
33+
{
34+
System.out.println("constructor of child class");
35+
this.y=100;
36+
}
37+
public void dispY()
38+
{
39+
System.out.println(" y = "+this.y);
40+
}
41+
}
42+
class Demo
43+
{
44+
public static void main(String[] args)
45+
{
46+
Child ch1=new Child();
47+
48+
}
49+
}
50+

java/day7/inh3.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// inheritance demo example 3 parameterized constructor of parent
2+
3+
class Parent
4+
{
5+
private int pv;
6+
7+
public Parent()
8+
{
9+
System.out.println("parameterless constructor of parent class");
10+
this.pv=1;
11+
12+
}
13+
public Parent(int pv)
14+
{
15+
System.out.println("parameterised constructor of parent class");
16+
this.pv=pv;
17+
18+
}
19+
20+
public int getPV(){return pv;}
21+
public void setPV(int pv){this.pv=pv;}
22+
23+
public void displayParent()
24+
{
25+
System.out.println("private pv = "+this.pv);
26+
}
27+
28+
}
29+
class Child extends Parent
30+
{
31+
private int y;
32+
public Child()
33+
{
34+
//super(555); // to call parameterized constructor of parent class
35+
System.out.println("constructor of child class");
36+
this.y=100;
37+
super(444);
38+
}
39+
public void display()
40+
{
41+
System.out.println(" pv = "+this.getPV());
42+
System.out.println(" y = "+this.y);
43+
}
44+
}
45+
class Demo
46+
{
47+
public static void main(String[] args)
48+
{
49+
Child ch1=new Child();
50+
ch1.display();
51+
52+
}
53+
}
54+

java/day7/inh4.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// inheritance demo example 4 static block
2+
3+
class Parent
4+
{
5+
private int pv;
6+
private static int cnt;
7+
8+
static
9+
{
10+
System.out.println("static block of parent class");
11+
cnt=10001;
12+
}
13+
public Parent()
14+
{
15+
System.out.println("parameterless constructor of parent class");
16+
this.pv=1;
17+
18+
}
19+
public Parent(int pv)
20+
{
21+
System.out.println("parameterised constructor of parent class");
22+
this.pv=pv;
23+
24+
}
25+
26+
public int getPV(){return pv;}
27+
public void setPV(int pv){this.pv=pv;}
28+
29+
public void displayParent()
30+
{
31+
System.out.println("private pv = "+this.pv);
32+
}
33+
34+
}
35+
class Child extends Parent
36+
{
37+
private int y;
38+
39+
static
40+
{
41+
System.out.println("static block of child class");
42+
}
43+
public Child()
44+
{
45+
super(555); // to call parameterized constructor of parent class
46+
System.out.println("constructor of child class");
47+
this.y=100;
48+
// super(444); // call to super should be the first statement
49+
50+
}
51+
public void display()
52+
{
53+
System.out.println(" pv = "+this.getPV());
54+
System.out.println(" y = "+this.y);
55+
}
56+
}
57+
class Demo
58+
{
59+
static
60+
{
61+
System.out.println("static block of demo class");
62+
63+
}
64+
public static void main(String[] args)
65+
{
66+
Child ch1=new Child();
67+
ch1.display();
68+
69+
}
70+
}
71+

0 commit comments

Comments
 (0)