The Artima Developer Community
Sponsored Link

Java Buzz Forum
Can we call sub class methods using super class object

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Can we call sub class methods using super class object Posted: Jul 12, 2015 5:04 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Can we call sub class methods using super class object
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Common 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

Read: Can we call sub class methods using super class object

Topic: Jsp Scriptlet Example Previous Topic   Next Topic Topic: How to add element at first and last position of linked list in Java?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use