File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ public void speak () {
2+ System .out .println ("Human speaking" );
3+ }
Original file line number Diff line number Diff line change 1+
2+ class Monkey {
3+ public void jump () {
4+ System .out .println ("Monkey juming" );
5+ }
6+
7+ public void bite () {
8+ System .out .println ("Monkey biting" );
9+ }
10+ }
11+ interface BasicAnimal {
12+ void Sleep ();
13+ void Eat ();
14+ }
15+
16+ class Human extends Monkey implements BasicAnimal {
17+
18+ public void speak () {
19+ System .out .println ("Human speaking" );
20+ }
21+
22+ @ Override
23+ public void Sleep () {
24+ System .out .println ("Human sleeping" );
25+ }
26+
27+ @ Override
28+ public void Eat () {
29+ System .out .println ("Human eating" );
30+ }
31+ }
32+
33+ public class testHuman {
34+ public static void main (String [] args ) {
35+ Human human = new Human ();
36+ human .speak ();
37+ human .jump ();
38+ human .bite ();
39+ human .Sleep ();
40+ human .Eat (); // Polymorphism in action. Monkey's methods are overridden in Human class.
41+ Monkey m = new Human ();
42+
43+ m .bite (); // Monkey calls this method when the user requests a bite
44+ }
45+ }
Original file line number Diff line number Diff line change 1+ abstract class Pen {
2+ abstract void write ();
3+ abstract void refill ();
4+
5+ }
6+ class FountainPen extends Pen {
7+ void write () {
8+ System .out .println ("Writing on fountain pen" );
9+ }
10+ void refill () {
11+ System .out .println ("Refilling fountain pen" );
12+ }
13+ void changeNib () {
14+ System .out .println ("Changing Nib" );
15+ }
16+
17+ }
18+
19+ public class testPen {
20+ public static void main (String [] args ) {
21+ FountainPen pen = new FountainPen () ;
22+ pen .changeNib ();
23+ pen .write ();
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments