• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple inheritance question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When writing a child class such as:
,

I was unable to compile the child class unless I created a constructor for the child class. Is this always the case? I thought it would just use the parent's constructor.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leonard Brandeis wrote:
I was unable to compile the child class unless I created a constructor for the child class. Is this always the case? I thought it would just use the parent's constructor.



If your child class doesn't have a constructor, Java will create a no-arg constructor that will call the parent's no-arg constructor. If your parent class doesn't have a no-arg constructor, then yes, you will have to create a constructor, in the child class, that calls the correct parent constructor.

Henry
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever you want to use a child class.. you have to add this lie in your child class..

"parentclassname objectname= new parentclassname();"

that is you have to create one object for parent class within the child class.

if parentclass=a
child class=b

then

class a
{
}
class b extends a
{
a a1=new a();
}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s.palanivel rajan wrote:whenever you want to use a child class.. you have to add this line in your child class..

"parentclassname objectname= new parentclassname();"

Where? I have never seen that. That looks mistaken, I am afraid.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Leonard

The only cause of compilation error I could think of on your example is that Class Parent is implementing a not default constructor. Then the compiler will not create a default constructor for you Class Child because is expecting you to provide explicitly the implementation for all constructor defined at Parent class level that do not have the default constructor signature for the class.

Compilation Error: Implicit super constructor Parent() is undefined for default constructor. Must define an explicit constructor



Works: Example 1


Works: Example 2

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java™ Tutorials. Look at the bits about "super" particularly.
 
Ruben Guillen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another important point.

1) Only instance methods are inherited.

2) Never inherited, but shadowed are instance and static variables, static method, constructors.

Regards.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leonard Brandeis wrote:When writing a child class such as:



Note that "inheritance" in object oriented programming has a different meaning than in biology. If you create a class "child" that extends class "parent", you're using the biological meaning of the word "inheritance", which doesn't translate to OO programming.

In object oriented programming, inheritance implies an is-a relation between the subclass and the superclass. When you apply this to your example, what you're saying with that line of code is:

A child is a parent.

But as you see, that's nonsense - a child is not a parent. And subclasses are not "children" of their superclasses - we don't call subclasses "child classes". It's better to use other class names if you're talking about inheritance in object oriented programming. For example, call your superclass "Animal" and your subclass "Dog". A Dog is an Animal.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

2) Never inherited, but shadowed are instance and static variables, static method, constructors.



Another item not inherited are instance methods, where the subclass doesn't have access rights to the superclass' version of that method.

1. If the superclass instance method is private.
2. If the superclass instance method has default access, and the subclass is in a different package.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic