Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help Me - Inheritance  RSS feed

Amol Chavan
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a doubt related to inheritance in Java.
I have a Class extends is created runtime and it has all the info about the tables and its

columns and field types, etc.
As more no of tables in the database size of is getting bigger &

bigger which is creating problem at the time of compilation.

So i wrote a code which will create two files at runtime like which will have info of first 50 table and which will have info of next 50 tables.
But the problem here is DBOperation is extending class so now which class it should extend?

I tried like this:

DBOperation extends Schema2

Schema2 extends Schema1

DBOperation call getTableInfo(tableName) method of its superclass which return HashMap with all the info of the table which is passed to that method.

public HashMap getTableInfo(tableName) is present in both as well

but then it is giving NullPointer Exception when DBOpereation getting information of tables which is not in Schema2 but present in Schema1.

If it is getting null from Schema2 then it must check it in
How inheritance will help in this case?

Is there any other way to do it?

How to solve this problem?
Please help me.....
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear whether you're loading schema data at compile time or runtime. Is it hard coded into the class as constants or read from the database? It is possible to make a source file too large to compile, I think, but at runtime loading a lot of data into one object or two won't make much difference.

But more fundamentally, ask yourself why you're using "extends" at all. If you made a new class that just extends Object you could use composition, hold references to two Schema objects, and write a method:

You could generalize this to work on an array of Schema[] for any number of instances.

Is it really necessary that any code that works with Schema will also work with DBOperation? That's the kind of polymorphism inheritance gives you, and if it's not needed, use composition instead.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!