6 Replies - 152 Views - Last Post: 11 January 2013 - 11:06 AM Rate Topic: -----

#1 joshuajobin08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-September 12

i have problem to understand this code snippet

Posted 11 January 2013 - 09:05 AM

Parent p = new GrandChild();
Is This A Good Question/Topic? 0
  • +

Replies To: i have problem to understand this code snippet

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: i have problem to understand this code snippet

Posted 11 January 2013 - 09:08 AM

Declare a variable of type Parent called p and assign it a new object of the GrandChild class.

This would work if GrandChild inherits from Parent as a Parent variable can hold instances of either itself or any subclasses, but that really doesn't make any sense as a GrandChild is not a Parent.
Was This Post Helpful? 1
  • +
  • -

#3 joshuajobin08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-September 12

Re: i have problem to understand this code snippet

Posted 11 January 2013 - 09:08 AM

in this above code
parent is superclass and grandchild is subclass
i have seen this
grandchild p=new grandchild

where p is the object created for class grand child.
but what about the above code.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i have problem to understand this code snippet

Posted 11 January 2013 - 10:20 AM

We cannot determine what you try to attempt based on a single line of code.

Parent p = new GrandChild();

could work which means creates a new GrandChild instance ans stores it into a Parent object pointer.
For that to work, Parent needs to be a super class of GrandChild which does not really make sense... can be written

class GrandChild extends Parent {


but there will be a flaw in the design of those classes, at least in the naming convention
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,176
  • Joined: 21-June 11

Re: i have problem to understand this code snippet

Posted 11 January 2013 - 10:22 AM

View PostRyano121, on 11 January 2013 - 05:08 PM, said:

but that really doesn't make any sense as a GrandChild is not a Parent.


I don't think the Parent and GrandChild classes are named that way because they represent actual people. I rather think that Parent is called Parent because its a parent class (i.e. a class that other classes inherit from) and GrandChild is called GrandChild because it's a subclass of Child, which is a subclass of Parent.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i have problem to understand this code snippet

Posted 11 January 2013 - 10:31 AM

That makes sense:

ParentClass
ChildClass
GrandChildClass

class Animal {                   // parent
...

class Mammal extends Animal {    // child
...

class Bird extends Animal {      // child
...


class Dog extends Mammal {       // grand child
...

class Eagle ends Bird {          // grand child
...


Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: i have problem to understand this code snippet

Posted 11 January 2013 - 11:06 AM

View Postsepp2k, on 11 January 2013 - 06:22 PM, said:

View PostRyano121, on 11 January 2013 - 05:08 PM, said:

but that really doesn't make any sense as a GrandChild is not a Parent.


I don't think the Parent and GrandChild classes are named that way because they represent actual people. I rather think that Parent is called Parent because its a parent class (i.e. a class that other classes inherit from) and GrandChild is called GrandChild because it's a subclass of Child, which is a subclass of Parent.


Ah haha that makes so much more sense :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1