Even if you write a simple class (i.e. it is not extending any base-class), it implicitly extends built-in Object class. So the Object class is at the root of all the hierarchies in Java. Thus the features of this class will be available in all the classes.
Example: The following example illustrates the basic concepts of inheritance.
class A
{ int i,j; // or protected int i,j; or public int i,j;
void showij()
{ System.out.println(i); System.out.println(j);
}
}
class B extends A
{ int k;
void showk()
{ System.out.println(k);
}
void sum()
{ System.out.println(i+j+k);
}
}
class InheritanceDemo
{ public static void main(String args[])
{ A a = new A();
B b = new B();
a.i = 10; a.j = 20;
a.showij();
b.i = 7; b.j = 8; b.k = 9;
b.showij(); b.showk();
a.showij();
b.sum();
}
}
Output:
10
20
7
8
9
10
20
24
Example: The following example illustrates that although private members are not inherited but
they can be accessed through methods.
1 class A
2 { private int i,j;
3 void showij()
4 { System.out.println(i); System.out.println(j);
5 }
6 void setI(int x)
7 { i = x;
8 }
9 void setJ(int x)
10 { j = x;
11 }
12 int getI()
13 { return i;
14 }
15 int getJ()
16 { return j;
17 }
18 }
19 class B extends A
20 { int k;
21 void showk()
22 { System.out.println(k);
23 }
24 void sum()
25 { System.out.println(getI()+getJ()+k);
26 }
27 }
28 class InheritanceDemo1
29 { public static void main(String args[])
30 { A a = new A();
31 B b = new B();
32 a.setI(10); a.setJ(20);
33 a.showij();
34 b.setI(7); b.setJ(8); b.k = 9;
35 b.showij(); b.showk();
36 a.showij();
37 b.sum();
38 }
39 }
Output:
10
20
7
8
9
10
20
24
Example: The following example defines class Stack with two operations push() and pop(). We
will then extend this class to provide some more features (like adding operations display(), isStackEmpty() and isStackFull().
1 class Stack
2 {
3 int top = -1;
4 int a[] = new int[10];
5 void push(int x)
6 {
7 if(top == 9)
8 { System.out.println("Stack Full");
9 return;
10 }
11 top = top + 1;
12 a[top] = x;
13 }
14 int pop()
15 {
16 if(top == -1)
17 { System.out.println("Stack Empty");
18 return -1; //assuming that –1 is not a valid data
19 }
20 int x = a[top];
21 top = top - 1;
22 return x;
23 }
24 }
The following class makes use of the Stack class defined above. In the main method, we have created an instance of the class Stack, pushed 10 elements on the stack and then finally we are removing the elements from the stack using pop() and displaying them. Accepting input from the user to decide what operation is to be performed can further extend the program.
1 class StackDemo
2 { public static void main(String args[])
3 { Stack s = new Stack();
4 for(int i = 0; i < 10; i++)
5 { s.push(100+i);
6 }
7 int x;
8 for(int i = 0; i < 10; i++)
9 {
10 x = s.pop();
11 System.out.println(x);
12 }
13 }
14 }
The following class adds new features to the class Stack by extending it. The methods push() and pop() and instance variables top and a[] are available in the extended class because of inheritance.
1 class ExtStack extends Stack
2 { void display()
3 { for(int i = top; i >= 0; i--)
4 { System.out.println(a[i]);
5 }
6 }
7 boolean isStackEmpty()
8 { if(top == -1)
9 return(true);
10 else
11 return(false);
12 }
13 boolean isStackFull()
14 { if(top == 9)
15 return(true);
16 else
17 return(false);
18 }
19 }
The following class makes use of the ExtStack class, which is derived by extending the class Stack. You can see that it uses the features of the base-class as well as derived class.
1 class StackDemo1
2 { public static void main(String args[])
3 { ExtStack s = new ExtStack();
4 int i=0;
5 for(i = 0; i < 10; i++)
6 { s.push(100+i);
7 }
8 s.push(i); //will lead to stack overflow
9 if(s.isStackFull())
10 System.out.println("Stack is full");
11 s.display(); //Stack will be displayed without deleting
12 int x; //elements are popped() and displayed
13 for(i = 0; i < 10; i++)
14 { x = s.pop(); System.out.println(x);
15 }
16 s.pop(); //will lead to stack underflow
17 if(s.isStackEmpty())
18 System.out.println("Stack is empty");
19 }
20 }
Example: The following example illustrates the basic concepts of inheritance.
class A
{ int i,j; // or protected int i,j; or public int i,j;
void showij()
{ System.out.println(i); System.out.println(j);
}
}
class B extends A
{ int k;
void showk()
{ System.out.println(k);
}
void sum()
{ System.out.println(i+j+k);
}
}
class InheritanceDemo
{ public static void main(String args[])
{ A a = new A();
B b = new B();
a.i = 10; a.j = 20;
a.showij();
b.i = 7; b.j = 8; b.k = 9;
b.showij(); b.showk();
a.showij();
b.sum();
}
}
Output:
10
20
7
8
9
10
20
24
Example: The following example illustrates that although private members are not inherited but
they can be accessed through methods.
1 class A
2 { private int i,j;
3 void showij()
4 { System.out.println(i); System.out.println(j);
5 }
6 void setI(int x)
7 { i = x;
8 }
9 void setJ(int x)
10 { j = x;
11 }
12 int getI()
13 { return i;
14 }
15 int getJ()
16 { return j;
17 }
18 }
19 class B extends A
20 { int k;
21 void showk()
22 { System.out.println(k);
23 }
24 void sum()
25 { System.out.println(getI()+getJ()+k);
26 }
27 }
28 class InheritanceDemo1
29 { public static void main(String args[])
30 { A a = new A();
31 B b = new B();
32 a.setI(10); a.setJ(20);
33 a.showij();
34 b.setI(7); b.setJ(8); b.k = 9;
35 b.showij(); b.showk();
36 a.showij();
37 b.sum();
38 }
39 }
Output:
10
20
7
8
9
10
20
24
Example: The following example defines class Stack with two operations push() and pop(). We
will then extend this class to provide some more features (like adding operations display(), isStackEmpty() and isStackFull().
1 class Stack
2 {
3 int top = -1;
4 int a[] = new int[10];
5 void push(int x)
6 {
7 if(top == 9)
8 { System.out.println("Stack Full");
9 return;
10 }
11 top = top + 1;
12 a[top] = x;
13 }
14 int pop()
15 {
16 if(top == -1)
17 { System.out.println("Stack Empty");
18 return -1; //assuming that –1 is not a valid data
19 }
20 int x = a[top];
21 top = top - 1;
22 return x;
23 }
24 }
The following class makes use of the Stack class defined above. In the main method, we have created an instance of the class Stack, pushed 10 elements on the stack and then finally we are removing the elements from the stack using pop() and displaying them. Accepting input from the user to decide what operation is to be performed can further extend the program.
1 class StackDemo
2 { public static void main(String args[])
3 { Stack s = new Stack();
4 for(int i = 0; i < 10; i++)
5 { s.push(100+i);
6 }
7 int x;
8 for(int i = 0; i < 10; i++)
9 {
10 x = s.pop();
11 System.out.println(x);
12 }
13 }
14 }
The following class adds new features to the class Stack by extending it. The methods push() and pop() and instance variables top and a[] are available in the extended class because of inheritance.
1 class ExtStack extends Stack
2 { void display()
3 { for(int i = top; i >= 0; i--)
4 { System.out.println(a[i]);
5 }
6 }
7 boolean isStackEmpty()
8 { if(top == -1)
9 return(true);
10 else
11 return(false);
12 }
13 boolean isStackFull()
14 { if(top == 9)
15 return(true);
16 else
17 return(false);
18 }
19 }
The following class makes use of the ExtStack class, which is derived by extending the class Stack. You can see that it uses the features of the base-class as well as derived class.
1 class StackDemo1
2 { public static void main(String args[])
3 { ExtStack s = new ExtStack();
4 int i=0;
5 for(i = 0; i < 10; i++)
6 { s.push(100+i);
7 }
8 s.push(i); //will lead to stack overflow
9 if(s.isStackFull())
10 System.out.println("Stack is full");
11 s.display(); //Stack will be displayed without deleting
12 int x; //elements are popped() and displayed
13 for(i = 0; i < 10; i++)
14 { x = s.pop(); System.out.println(x);
15 }
16 s.pop(); //will lead to stack underflow
17 if(s.isStackEmpty())
18 System.out.println("Stack is empty");
19 }
20 }
No comments:
Post a Comment