Overriding Examples

1. What is the output ?

class Animal{
 void show() {
  System.out.println("animal");
 }
}
class Dog extends Animal{
 void show() {
  System.out.println("dog");
 }
}

public class Test {
public static void main(String[] args) {
 Animal a=new Animal();
 a.show();
}
}

2. What is the output ?

class Animal{
 void show() {
  System.out.println("animal");
 }
}
class Dog extends Animal{
 void show() {
  System.out.println("dog");
 }
}

public class Test {
public static void main(String[] args) {
 Animal a=new Dog();
 a.show();
}
}

3. What is the output ?

class Animal{
 void show() {
  System.out.println("animal");
 }
}
class Dog extends Animal{
 private void show() {
  System.out.println("dog");
 }
}

public class Test {
public static void main(String[] args) {
 Animal a=new Dog();
 a.show();
}
}

4. What is the output ?

class Animal{
 void show() {
  System.out.println("animal");
 }
}
class Dog extends Animal{
 protected void show() {
  System.out.println("dog");
 }
}

public class Test {
public static void main(String[] args) {
 Animal a=new Dog();
 a.show();
}
}

5. What is the output ?

class Animal{
 void show(int i) {
  System.out.println("animal");
 }
}
class Dog extends Animal{
 void show(Integer i) {
  System.out.println("dog");
 }
 void show(int i) {
  System.out.println("hello");
 }
 
}

public class Test {
public static void main(String[] args) {
 Animal a=new Dog();
 a.show(1);
}
}

6. What is the output ?

class Animal{
 void show(int i) {
  System.out.println("animal");
 }
}
class Dog extends Animal{
 void show(Integer i) {
  System.out.println("dog");
 }
 void show(int i) {
  System.out.println("hello");
 }
 
}

public class Test {
public static void main(String[] args) {
 Dog a=new Dog();
 a.show(1);
}
}

7. What is the output ?

class Animal {
 float show(int i) {
  System.out.println("animal");
  return 1;
 }
}

class Dog extends Animal {

 int show(int i) {
  System.out.println("dog");
  return 1;
 }

}

public class Test {
 public static void main(String[] args) {
  Dog a = new Dog();
  a.show(1);
 }
}

8. What is the output ?

class Animal {
 private void show(int i) {
  System.out.println("animal");
 }
}

class Dog extends Animal {

 void show(int i) {
  System.out.println("dog");
 }

}

public class Test {
 public static void main(String[] args) {
  Animal a = new Dog();
  a.show(1);
 }
}

9. What is the output ?

class Animal {
 private void show(int i) {
  System.out.println("animal");
 }
}

class Dog extends Animal {

 void show(int i) {
  System.out.println("dog");
 }

}

public class Test {
 public static void main(String[] args) {
  Dog a = new Dog();
  a.show(1);
 }
}

10. What is the output ?

class Animal {
  void show(int i) {
  System.out.println("animal " + i);
 }
  public Animal() {
 show(6);
 }
}

class Dog extends Animal {

 void show(int i) {
  System.out.println("dog "+ i);
 }

}

public class Test {
 public static void main(String[] args) {
  Dog a = new Dog();
 }
}

11. What is the output ?

class Animal {
 int i = 100;

 void show() {
  System.out.println("animal " + i);
 }

 public Animal() {
  show();
 }
}

class Dog extends Animal {
 int i = 1000;

 void show(int i) {
  System.out.println("dog " + i);
 }

}
public class Test {
 public static void main(String[] args) {
  Dog a = new Dog();
 }
}

12. What is the output ?

class Animal {
 int i = 100;

 void show() {
  System.out.println("animal " + i);
 }

 public Animal() {
  show(6);
 }
}

class Dog extends Animal {
 int i = 1000;

 void show(int i) {
  System.out.println("dog " + i);
 }

}

public class Test {
 public static void main(String[] args) {
  Dog a = new Dog();
 }
}

13. What is the output ?

class Animal {
 int i = 100;

 void show() {
  System.out.println("animal " + i);
 }

 public Animal() {
  show();
 }
}

class Dog extends Animal {
 int i = 1000;

 void show() {
  System.out.println("dog " + i);
 }

}

public class Test {
 public static void main(String[] args) {
  Dog a = new Dog();
 }
}

14. What is the output ?

class Animal {
 int i = 100;

 void show() {
  System.out.println("animal " + i);
 }

}

class Dog extends Animal {
 int i = 1000;

 void show() {
  System.out.println("dog " + i);
 }

}

public class Test {
 public static void main(String[] args) {
  Animal a = new Dog();
  a.show();
 }
}

No comments: