Ans : How to access child class methods through parent object

12 views
Skip to first unread message

MYJAVAHUB

unread,
Oct 8, 2012, 3:23:16 PM10/8/12
to myjavahub

From: suma latha Pinninti 

Q?:   how to access child class method through parent object

 

Ans:It's not possible to access child class method through parent object.

 

By using runtime polymorphism ,Parentclass object points to its child class object.

by using parent class object we can access only parent class methods ,but not child class methods.

 

if we  call ,it gives compilation Error.

 

From: srikanth madupathi 

if u want call child class method with parent reference u can cal(parentclass pc=new childclas())
,but with parent object  u cant call.

 

From: ch karthikv <chkvee...@gmail.com>
hi,
 Child class methods calls with parent class object is not possible
as of knowledge
for example
 class Parent{
  public void add(){
  System.out.println("we are in parent");

   }
 }

 public class Child extends Parent{
 public void  sub(){
  System.out.println("we are in child");

 }
 public static void main(String ar[]){

 Parent p=new Parent();
 p.add();
 p.sub(); // it is not possible bcz  a parent class obj cn't access

the child class members
            //a child class obj can access child & parent class members
           //
}
}
}

Reply all
Reply to author
Forward
0 new messages