Skip to content

Commit 8301ce4

Browse files
authored
Add files via upload
1 parent 385f61d commit 8301ce4

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Inheritence/ObjectCreation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//A Java program to demonstrate that both super class and subclass constructors refer to same object
2+
class Parent
3+
{
4+
Parent()
5+
{
6+
System.out.println("Super class Constructor");
7+
System.out.println("Super class object hashcode:"+this.hashCode());
8+
}
9+
}
10+
class Child extends Parent
11+
{
12+
Child(){
13+
System.out.println("Sub Class Constructor");
14+
System.out.println("Sub class object Hashcode:"+this.hashCode());
15+
}
16+
}
17+
class ObjectCreation
18+
{
19+
public static void main(String[] args) {
20+
Child obj = new Child();
21+
}
22+
}

Inheritence/SingleInheritence.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class A
2+
{
3+
int i,j;
4+
A()
5+
{
6+
7+
}
8+
A(int a,int b)
9+
{
10+
i=a;
11+
j=b;
12+
}
13+
void Showij()
14+
{
15+
System.out.println("i="+i);
16+
System.out.println("j="+j);
17+
}
18+
}
19+
class B extends A{
20+
int k;
21+
B(int a,int b,int c)
22+
{
23+
i=a;
24+
j=b;
25+
k=c;
26+
}
27+
void Showk()
28+
{
29+
System.out.println("k=" + k);
30+
}
31+
}
32+
33+
/**
34+
* SingleInheritence
35+
*/
36+
public class SingleInheritence {
37+
public static void main(String[] args) {
38+
//when an object of B class is created, a copy of the all methods and fields of the superclass acquire memory in this object. That is why, by using the object of the subclass we can also access the members of a superclass.
39+
//During inheritance only object of subclass is created, not the superclass
40+
B subobj=new B(5,10,15);
41+
subobj.Showij();
42+
subobj.Showk();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)