Runtime Polymorphism ( method overriding ) with Multilevel Inheritance

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

5180

Program:

<span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Profession</span><span class="pun">{</span><span class="pln">
	</span><span class="kwd">void</span><span class="pln"> check</span><span class="pun">(){</span><span class="pln">
		</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"profession class"</span><span class="pun">);</span><span class="pln">
		</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Employee</span><span class="pln"> </span><span class="kwd">extends</span><span class="pln"> </span><span class="typ">Profession</span><span class="pun">{</span><span class="pln">
	</span><span class="kwd">void</span><span class="pln"> check</span><span class="pun">(){</span><span class="pln">
		</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Employee class"</span><span class="pun">);</span><span class="pln">
		</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">Teacher</span><span class="pln"> </span><span class="kwd">extends</span><span class="pln"> </span><span class="typ">Employee</span><span class="pun">{</span><span class="pln">
	</span><span class="kwd">void</span><span class="pln"> check</span><span class="pun">(){</span><span class="pln">
		</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Teacher class"</span><span class="pun">);</span><span class="pln">
		</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> args</span><span class="pun">[]){</span><span class="pln">
	</span><span class="typ">Profession</span><span class="pln"> obj1 </span><span class="pun">,</span><span class="pln">obj2</span><span class="pun">,</span><span class="pln"> obj3</span><span class="pun">;</span><span class="pln">
	obj1</span><span class="pun">=</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Profession</span><span class="pun">();</span><span class="pln">
	obj2</span><span class="pun">=</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Employee</span><span class="pun">();</span><span class="pln">
	obj3</span><span class="pun">=</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Teacher</span><span class="pun">();</span><span class="pln">
	obj1</span><span class="pun">.</span><span class="pln">check</span><span class="pun">();</span><span class="pln">
	obj2</span><span class="pun">.</span><span class="pln">check</span><span class="pun">();</span><span class="pln">
	obj3</span><span class="pun">.</span><span class="pln">check</span><span class="pun">();</span><span class="pln">
	</span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span>

Output:

profession class
Employee class
Teacher class
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.