wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes confusion in implementation of oops concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confusion in implementation of oops concept" Watch "confusion in implementation of oops concept" New topic
Author

confusion in implementation of oops concept

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
basically i am creating a online examination system, and i think i should implement oops concept in that, but i am a bit confused in implementation.
what i am thinking is, i can create one super class "Course.java" and their subclasses "Mba.java", "Ms.java".
and subclasses of "Mba.java" can be "Semester1.java", "Semester2.java" so on.
but i am not getting how to implement it, actually i am confused with what methods should be in which class.
below image-1 is some static content my friend putted with html, but i am thinking i should use oops concept here:







[Thumbnail for onlineexam.png]

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:basically i am creating a online examination system, and i think i should implement oops concept in that, but i am a bit confused in implementation.
what i am thinking is, i can create one super class "Course.java" and their subclasses "Mba.java", "Ms.java".
and subclasses of "Mba.java" can be "Semester1.java", "Semester2.java" so on.
but i am not getting how to implement it, actually i am confused with what methods should be in which class.
below image-1 is some static content my friend putted with html, but i am thinking i should use oops concept here:


First of all stop calling Course.java, Mba.java, Ms.java as classes because they are source files.

actually i am confused with what methods should be in which class.


Show me some efforts and point of confusions. I mean make a rough architecture of what you are trying to do.


OCPJP 6 86%
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Actually this is what i was trying to implement, but i think it's not seems to be making any sense, i think i am doing it wrong.



[Thumbnail for effort.png]

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:Actually this is what i was trying to implement, but i think it's not seems to be making any sense, i think i am doing it wrong.



What i can see are only getters and setters in all classes.
You want to implement inheritance here that is a very good decision but the purpose of making a subclass is to make a more specialized class.
think about those methods which you want to share among all classes and put them in Course class. Then write some more specific methods for each class like, chooseSubjects() is a very specific method for each class.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:Actually this is what i was trying to implement, but i think it's not seems to be making any sense, i think i am doing it wrong.



I think Course should be an interface.So, now you can declare some methods in this interface and all courses must implement this interface and if there is a new course in future you or anybody want to add in your application then he/she can make a new course class by implementing this Course interface. This can improve maintainability too.
Course interface just for an idea-


Note- Subject should be an interface too, and all the subjects should be the inplemented class of Subject interface.
in subject interface you can declare methods like above for example,
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Thanks nikhil.
why you think that this should be an interface, because this difficulty i face many of times "what to implement where".

also i was just thinking to implement oops concept and inheritance there, instead of that static html text,
this makes it dynamic and the situation seems like inheritance, apart from this i don't have any sound/strong reason to replace that static text with java classes, what you think?


Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Consider whether an MBA is a kind of course, or an instance of a course. If it is an instance, then it doesn’t need a class of its own.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:Thanks nikhil.
why you think that this should be an interface

Because by doing this you can make a backbone of your design. Course interface is a key interface for all of your courses if there is a MBA course in your design so it must have all the implemented but its own specialized methods of Course interface.

this difficulty i face many of times "what to implement where".

That means your OOP concepts are not cleared yet. try tutorials and work hard to make them strong.

also i was just thinking to implement oops concept and inheritance there, instead of that static html text

I am not getting it ??
i don't have any sound/strong reason to replace that static text with java classes, what you think?

how can you replace static text with class ??
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2

how can you replace static text with class ??

as earlier in the first image, they were static texts, putted with some html tags.
now i am using java to make this also dynamic.
class i mean is with java actually.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Campbell Ritchie wrote:Consider whether an MBA is a kind of course, or an instance of a course. If it is an instance, then it doesn’t need a class of its own.


yes it should be an instance of the course, but if it's an instance of the course how do i implement course interface to this?
and creating classes for each course is also not seems to be practical because number of courses are there.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216



class i mean is with java actually.



Use jsp or servlets rather.
Okay, for the page you posted in the very first post, there is no need to make that dynamic because there are only hyperlinks on that page.

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:
Campbell Ritchie wrote:Consider whether an MBA is a kind of course, or an instance of a course.If it is an instance, then it doesn’t need a class of its own.


yes it should be an instance of the course, but if it's an instance of the course how do i implement course interface to this?
and creating classes for each course is also not seems to be practical because number of courses are there.


No punit you are taking it wrong. Carefully read Sherrif's statement.
Consider whether an MBA is a kind of course, or an instance of a course.


Sherrif is giving you choice to choose MBA either a kind of Course(class or interface) or an instance of a Course(class).
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Sherrif is giving you choice to choose MBA either a kind of Course(class or interface) or an instance of a Course(class).

ohh i got it.
but better it should be instance, right?
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

creating classes for each course is also not seems to be practical because number of courses are there.


This is what we call cohesion in java.

But if you do not want to make a separate class for each course then as Sherrif already told you that you can make an instance of Course class for each course.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:
Sherrif is giving you choice to choose MBA either a kind of Course(class or interface) or an instance of a Course(class).

ohh i got it.
but better it should be instance, right?


It all depends on your design pattern.
But there are different subjects for different courses and further for different semesters, it will make your project hard to maintain, think about it.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2

But there are different subjects for different courses and further for different semesters, it will make your project hard to maintain, think about it.

yes if i make class for each of them, there will be number of classes, may be hundreds of.


It all depends on your design pattern.


so far i am not implementing any design pattern, because i don't have that much knowledge about design patterns to implement them to my project.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Yes, but harder to maintain, this should be better:
Courses: Interface {Courses}
For each course: Class {MBA, MS}
For each Semester: Class {semester1, semester2}
For each Subject: this should be instance of semester class.

so basically my each course implement Courses interface, semester extends each courses (ie. if it's semester of MBA Course than it will extend MBA), or may be each course (MBA, MS) can also be an interface.
and each subject will extend semester?

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:and each subject will extend semester?



Subject is a type of Semester ??
think about it.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Why not you make a ArrayList instance variable in each Semester class.
To store Subjects for that Semester.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Draw a diagram with all the classes on.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Here is the rough class diagram, please let me know if it's not readable i will make it in paint.


[Thumbnail for Photo0304.jpg]

Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Punit Jain wrote:Here is the rough class diagram, please let me know if it's not readable i will make it in paint.

Punit as i have already told you that it all depends on your design pattern.
by design pattern i don't mean that you follow a standard design pattern already available but i mean to say that your very own design pattern.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
It is illegible, I am afraid, which has probably got to do with your scanner. But you have your arrows pointing down, so I think the whole class diagram is drawn upside‑down. Please don’t use paint; use a word processor which can export the result as a pdf.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
cannot attach pdf.
Here is more cleared image.
i have one more confusion, what could the methods in Mba or Ms or any course?
i know they are up to me, but i am not sure.
any ideas !!!


[Thumbnail for cd.png]

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
oops again not visible.
this is some template code/implementation of that image.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
I think you are going to have to go back to the drawing board. As I suggested last week, MBA is probably an instance of course rather than a type of course.
A method like listCourses() does not fit in a Course interface. That fits in a College class. I think you are going to think long and hard about how courses relate to one another, to colleges, students etc. I think you are going to have to create a new design.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
yes certainly i am going for a new design.
also implementing side by side.
but facing one issue.
here is what i implemented:





but it's not showing me added course, ie(mba).
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

For starters, this is a mistake:


You shouldn't have your test harness extend the class you're testing. And then creating a new Courses object inside the createCourses() method of a class that is also a Courses is just asking for confusion.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Punit Jain wrote:yes certainly i am going for a new design.
also implementing side by side. . . .
Probably a bad idea, unless you want to have to use the combination of ctrl-A→delete on your code later.
Don’t write any more code until you have some sort of decent design.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Jeff Verdegan wrote:For starters, this is a mistake:


You shouldn't have your test harness extend the class you're testing. And then creating a new Courses object inside the createCourses() method of a class that is also a Courses is just asking for confusion.


yes, but if i do not extend the class, i can't call Courses class methods directly, and i have to use the instance mba to call the methods.
i removed that extends, but still these two lines confusing me.


Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Campbell Ritchie wrote:
Punit Jain wrote:yes certainly i am going for a new design.
also implementing side by side. . . .
Probably a bad idea, unless you want to have to use the combination of ctrl-A→delete on your code later.
Don’t write any more code until you have some sort of decent design.


i agree, it's certainly a bad idea, but i have created design for this module only not for the whole project, and probably creating design for module by module and implementing that is not that much bad.
because creating design for the whole project and then implementing that, seems a bit difficult.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:
Jeff Verdegan wrote:For starters, this is a mistake:


You shouldn't have your test harness extend the class you're testing. And then creating a new Courses object inside the createCourses() method of a class that is also a Courses is just asking for confusion.


yes, but if i do not extend the class, i can't call Courses class methods directly,


So create an instance of Courses. Code sharing is not a reason to use inheritance.


but still these two lines confusing me.


They're confusing me too. I have no idea what you're trying to do here. But if you're having a hard time figuring out how to use your own code, that's a sign that your design has some problems.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Jeff Verdegan wrote:
Punit Jain wrote:
Jeff Verdegan wrote:For starters, this is a mistake:


You shouldn't have your test harness extend the class you're testing. And then creating a new Courses object inside the createCourses() method of a class that is also a Courses is just asking for confusion.


yes, but if i do not extend the class, i can't call Courses class methods directly,


So create an instance of Courses. Code sharing is not a reason to use inheritance.


but still these two lines confusing me.


They're confusing me too. I have no idea what you're trying to do here. But if you're having a hard time figuring out how to use your own code, that's a sign that your design has some problems.


okay, here i am creating instance of class Courses, say mba, and wanted to add that mba Courses class with addCourse method, so whenever i call listCourses, it prints all the courses.
but here i am adding the instance of the class (mba), that's why i am getting output like this: [com.semester5.test.Courses@affc70].
but as type of my arraylist is of Courses, i also cannot add string "mba" to it.?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Gall's Law: A complex system that works is invariably found to have evolved from a simple system that worked. The inverse proposition also appears to be true: A complex system designed from scratch never works and cannot be made to work. You have to start over, beginning with a working simple system.

You haven't even begun to have a system that works and you already have a fairly complicated set of interfaces that have some pretty serious problems. I would say with almost 100% certainty that any code you write to implement these interfaces and the design you have now will not work. Try to start with a simple design first, then as you add tests and functionality, you constantly refactor as you go. This is how a complex system that works evolves.

Your basic assumptions are already flawed and are not in line with their normal, real-world concepts. This is what adds to your confusion. The way you are telling your story is confusing the plot. You don't have the right characters and you don't have the right interactions.

Try this to start with:

1. MBA and MS are NOT courses, they are advanced DEGREES awarded to a CANDIDATE/STUDENT who has satisfactorily completed a set of required COURSES and perhaps some optional ones as well.

2. A SEMESTER is a period of time during which a COURSE can be offered and taken.

3. A STUDENT can enroll for/take a COURSE to fulfill a requirement for obtaining a DEGREE.

4. During a SEMESTER, a CANDIDATE/STUDENT for an advanced DEGREE can take one or more COURSES

I would start by writing tests that specify the behavior of your system around one of the important concepts mentioned above, then build out the system from there.

Perhaps it's time to move this from Beginning Java to the Object-Oriented Programming forum.

----
Edit: Re COURSE vs DEGREE -- I realize that these can be used interchangeably: e.g. A four-year course in engineering vs. a Bachelor's Degree in engineering. A course can also be a synonym for a class or subject, e.g. an introductory COURSE in chemistry == an introductory CLASS in chemistry.

The point is to be consistent with your definitions and to be able to differentiate the general concept from its instances. For example, to define separate classes for Semester1 and Semester2 is not correct. Semester1 and Semester2 are instances of the same concept: a SEMESTER.


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Punit Jain wrote:


The return type of the listCourses() method is declared as a concrete class: ArrayList. This is bad form. Prefer to program to interfaces, not implementations. This a better definition:

Going back to my earlier point about consistency in terms, you need to decide whether you're going to use "Course" to mean the degree programs that are offered or a particular subject. Then stick with that definition and don't use it for any other concept. Frankly, I wouldn't start with an interface. I would build out a College class and see what kind of interesting behavior it has. Turning a class into a concrete implementation of an interface is an easy enough refactoring but I wouldn't start with interfaces right away because that tends to add premature complexity to your system. Let your tests lead you to a decision to add an interface.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

I would also question whether a SEMESTER has any interesting behavior that would qualify it to become a class. What interesting behavior would a SEMESTER object have? If all your answers are of the form: "To tell me what the ???information??? is." then that's not interesting behavior. "To tell me what the start date is," "To tell me what the end date is," "To tell me what courses are offered." -- these are all related to the information that a SEMESTER would have, not its behavior. Does a SEMESTER really have any interesting behavior? Think about it a little and if you can't answer it, you probably shouldn't create a SEMESTER class just yet.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:
but here i am adding the instance of the class (mba), that's why i am getting output like this: [com.semester5.test.Courses@affc70].


Yes. It's printing out what you added to it.

but as type of my arraylist is of Courses, i also cannot add string "mba" to it.?


Of course you can't add the String "mba". Why would you want to? You want a list of Courses, not a list of Strings, right?

I'm really confused as to what you're trying to accomplish and what your classes actually represent. The only guess I can offer here is that maybe you need to override toString() in your Courses class (bad name for the reason Junilu described and also because it should be singular, not plural) so that when you print out the list it shows something meaningful? That will address what I think might be the problem you're asking about here, but that is, by far, the least of your problems with these classes.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Punit Jain wrote: . . . i have created design for this module only not for the whole project, and probably creating design for module by module and implementing that is not that much bad.
because creating design for the whole project and then implementing that, seems a bit difficult.
I had a boss once who said
Smith’s rule of holes: “When in one, stop digging.”
Smith was not his real name.

I think you have got yourself into a hole and are still digging. The farther you go down a wrong road, the more difficult it will be to go back.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
so basically there would be three classes so far,
Course
Semester
Student

A Semester has a relationship with Course and, (still not sure with the behaviour of semester).
and Student has a relationship with Course and Student, so Student has instance of Course (say: mba, ms..) and instance of Semester (say: semester1, semester2...)

also i am confused in when to use classes and when to use interface, therefore not able to decide why Course would be class.
also confused in "how to catch for the behaviour of any class". (just as you said for Semester class)

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
Jeff Verdegan wrote:
Punit Jain wrote:
but here i am adding the instance of the class (mba), that's why i am getting output like this: [com.semester5.test.Courses@affc70].


Yes. It's printing out what you added to it.

but as type of my arraylist is of Courses, i also cannot add string "mba" to it.?


Of course you can't add the String "mba". Why would you want to? You want a list of Courses, not a list of Strings, right?

I'm really confused as to what you're trying to accomplish and what your classes actually represent. The only guess I can offer here is that maybe you need to override toString() in your Courses class (bad name for the reason Junilu described and also because it should be singular, not plural) so that when you print out the list it shows something meaningful? That will address what I think might be the problem you're asking about here, but that is, by far, the least of your problems with these classes.

yes i want list of all the courses, but i want to display that list (name of all courses) on the webpage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion in implementation of oops concept
 
Similar Threads
Abstract methods-why to use them?
Protected access specifier.
inner class
Object "Savvy"
why interfaces?