» » Can we call sub class methods using super class object

  • Common coding interview question everybody facing in interviews is can we call sub class method using super class object? or can we call child class method using parent class? or can a super class call a subclass' method.
  • The answer is No. but still we can say yes. so we need to what are all those scenarios of calling sub class method from super class and lets discuss about which is the actual possible case to call sub class method using super class object.
  • Before that let me explain what is inheritance and how the super and sub classes related each other.


Inheritance:

  •  Getting the properties from one class object to another class object is known as inheritance.
  • Two types of inheritance
  • Getting the properties from one class object to another with level wise and with some priorities is known as multilevel inheritance.
  • Getting the properties from one class object to another class object with same priority is known as multiple inheritance
  • Java does not supports multiple inheritance
Read this:

Why Java does not supports multiple inheritance

Creating super class object and calling methods

  1. //Multilevel inheritance program
  2. Class A{
  3.  
  4. int a;
  5.  
  6. public void print(){
  7.  
  8. System.out.println("print method of super class A"); 

  9. }
  10.  
  11. }
  12. Class B extends A{
  13.  
  14. public void show(){
  15.  
  16.  System.out.println("show method of sub class B"); 

  17. }
  18. public static void main(String[] args){
  19.   
  20. A obj= new A();
  21. obj.a=10;
  22.  
  23. obj.print();
  24.  //obj.show(); // compile time error

  25.  System.out.println("a="obj.a); 

  26. }
  27. }



Output:
  1. print method of super class  A
  2. 10

  • In above program super class and sub class is there and sub class extending super class.
  • But we created object for super class so we can call only super class methods on that object.
  • If we create sub class object we can call super class and sub class methods on that object now we can able to access super class A methods only.
  • So by creating super class object we can call only super class methods

Creating super class object and calling methods

  1. //Multilevel inheritance program
  2. Class A{
  3.  
  4. int a;
  5.  
  6. public void print(){
  7.  
  8. System.out.println("print method of super class A"); 

  9. }
  10.  
  11. }
  12. Class B extends A{
  13.  
  14. public void show(){
  15.  
  16.  System.out.println("show method of sub class B"); 

  17. }
  18. public static void main(String[] args){
  19.   
  20. B obj= new B();
  21. obj.a=10;
  22.  
  23. obj.print();
  24. obj.show();

  25.  System.out.println("a="obj.a); 

  26. }
  27. }

Output:
  1. print method of super class  A
  2. show method of sub class B
  3. 10


  • Above program shows sub class object using super class methods and variables as we said in inheritance concept all super class members can accessed by the sub class means all super class members are available to sub class if sub class extending super class.
  • So whenever we create object of sub class it will call sub class constructor and from sub class constructor super class constructor will be called so memory will be allocated to all super class non static member and sub class non static members.
  • So we can call super class methods using sub class.
  • B obj= new B();
  • obj.print();
  • So by creating sub class object we can call both super class methods and sub class methods.


 Assigning sub class reference to super class object.

  1. //Multilevel inheritance program
  2. Class A{
  3.  
  4. int a;
  5.  
  6. public void print(){
  7.  
  8. System.out.println("print method of super class A"); 

  9. }
  10.  
  11. }
  12. Class B extends A{
  13.  
  14. public void show(){
  15.  
  16.  System.out.println("show method of sub class B"); 

  17. }
  18. public static void main(String[] args){
  19.   
  20. A obj= new B();
  21. obj.a=10;
  22.  
  23. obj.print();
  24. //obj.show(); // compile time error

  25.  System.out.println("a="obj.a); 

  26. }
  27. }

Output:
  1. print method of super class  A
  2. 10


  • Yes we can assign sub class object to super class reference.
  • So here Sub class object is created so memory allocated to all super class members
  • so can we call all methods ?  No eventhough sub class object is created we can not call sub class methods because we assigned sub class object to super class reference.
  • Then how its possible to call sub class methods?
  • Yes its possible to call sub class methods using super class by type casting to sub class object .
  • By type casting super class object to sub class object we can access all corresponding sub class and all super class methods on that reference.


 Assigning sub class reference to super class object.

  1. //Multilevel inheritance program
  2. Class A{
  3.  
  4. int a;
  5.  
  6. public void print(){
  7.  
  8. System.out.println("print method of super class A"); 

  9. }
  10.  
  11. }
  12. Class B extends A{
  13.  
  14. public void show(){
  15.  
  16.  System.out.println("show method of sub class B"); 

  17. }
  18. public static void main(String[] args){
  19.   
  20. A obj= new B();
  21. obj.a=10;
  22.  
  23. obj.print();
  24. //obj.show();   compile time error
  25. ((B)obj).show(); // works fine


  26.  System.out.println("a="obj.a); 

  27. }
  28. }

Output:
  1. print method of super class  A
  2. show method of sub class B
  3. 10

 Assigning sub class reference to super class object. We can call sub class methods if overridden

Otherwise we need to typecast to call sub class methods

  1. //Multilevel inheritance program
  2. Class A{
  3.  
  4. int a;
  5.  
  6. public void print(){
  7.  
  8. System.out.println("print method of super class A"); 

  9. }
  10.  
  11. }
  12. Class B extends A{
  13.  
  14. public void show(){
  15.  
  16.  System.out.println("show method of sub class B"); 

  17. }
  18. public void print(){
  19.  
  20. System.out.println("Print method of Sub class B"); 

  21. }
  22.  
  23. public static void main(String[] args){
  24.   
  25. A obj= new B();
  26. obj.a=10;
  27.  
  28. obj.print(); // print method is overridden in sub class so it will execute sub class method.
  29. //obj.show();   compile time error
  30. ((B)obj).show(); // works fine


  31.  System.out.println("a="obj.a); 

  32. }
  33. }

Output:
  1. Print method of Sub class B
  2. show method of sub class B
  3. 10

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

4 comments for Can we call sub class methods using super class object

  1. //But it is working fine.A reference is calling the B put().
    class A{

    void put(){System.out.println("HI A");}
    }

    public class Call extends A{
    void put(){
    System.out.println("HI Call");
    //super.put();
    }

    public static void main(String args[])
    {
    //Call ob2;
    //A ob = new A();
    //Call ob = new Call();
    A ob= new Call();
    ob.put();
    }
    }

    ReplyDelete
    Replies
    1. its working fine because you have overriden the method 'put' in class call.If u wont override then u cannot call any method of subclass.

      Delete
  2. Thank you, this helped me with a problem I was having.

    ReplyDelete
  3. Can you please let me know what is the use of superclass super = new subclass() ?

    ReplyDelete

Select Menu