This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a doubt related to inheritance in Java. I have a Class DBOperation.java extends Scema.java. Schema.java 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 Schema.java 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 Schema1.java which will have info of first 50 table and Schema2.java which will have info of next 50 tables. But the problem here is DBOperation is extending Schema.java 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 Schema1.java as well Schema2.java.
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 Schema1.java How inheritance will help in this case?
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi