• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Object

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks

Please help me to understand the following code(Its always confusing me):

class A
{
void show()
{
System.out.println("Class A");
}
}
class B extends A
{
void show()
{
System.out.println("Class B");
}

public static void main(String args[])
{
A objOfA=new A();
B objOfB=new B();
B objOf=new A();// my confusion

objOfA.show(); // ok with this code
objOfB.show(); // ok with this code
objOf.show(); // confuse again
}
}

what does the line means----> "B objOf=new A();// my confusion".
How it works---------> "objOf.show(); // confuse again"


Please help me.

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gaurav,

Here is your code with code tag, and proper indenting, that looks good




Gaurav says,
what does the line means----> "B objOf=new A();// my confusion".
How it works---------> "objOf.show(); // confuse again"
Please help me.



See:
B IS-A(n) A (or)
B IS-A subclass of A (or)
A is parent class of class B

A's reference variable can hold reference of an object of its subclass.
That is because "Dog IS-A Animal but Animal is not necessarily Dog" (think like this)

When you write,
Line1 : Animal a = new Dog(); //No problem Dog IS-A Animal
Line2 : Dog b = new Animal(); //Compile time error (because Animal is not necessarily Dog)

You can make Dog b = new Animal(); able to compile but it will fail at run time saying ClassCastException.
Dog b =(Dog) new Animal();

This is only permitted at compile time because there is a IS-A relationship
between Animal and Dog; (in your case between A and B). Compiler can't find
what wrong you are doing because of explicit cast.



B objOf=new A();// my confusion LINE 1

objOfA.show(); // ok with this code
objOfB.show(); // ok with this code LINE 2
objOf.show(); // confuse again



Because LINE 1 won't compile your confusion around Line 2, does not seem obvious to me.



Gaurav,
First understand and practice what reference variable can hold what object and where casting requires to make code compilable and where code fails at run time. Then come to understanding polymorphic call to methods. It will
smooth your path.



If you make line 1 as
B objOf = (B) new A(); //compile but fail at run time

(Dog ref variable can't hold reference of Animal object at runtime)



Regards,
cmbhatt
 
My cellmate was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic