• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

When to use interface vs abstract class?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone! I am a bit confused about when to use abstract classes vs interfaces. For example, if I need to create a system for education with students and teachers what would be the best way to go about it?

Should I create a person interface, abstract class for teachers (to be able to create teachers for different subjects) and then a simple class for student?

Or should I make a person an abstract class and teacher interface and then create different teachers? What would be the best way to implement this example if I explicitly need to be able to create multiple teachers? I am quite confused of when to use which.
 
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Interfaces are general contracts for behavior. Abstract classes are the same way except they also imply a hierarchical relationship between the base class and its subclasses, where subclasses are specializations of the base class and the base class provides attributes and behaviors that are common to all of the classes in the hierarchy.

I would say this though: don't try to get your design right up front. Expect to discover many more things (including that you were wrong about some things) as you flesh out the details of the design through testing and refactoring. Don't get trapped in analysis and design paralysis. Have a high-level idea of what your main concepts are and be clear about what value you want your users to get. Then start experimenting with different design options. NOBODY, absolutely NOBODY is able to see the big picture of a complex system up front in its entirety, with all the complexity accounted for in a diagram.

Designing without a clear idea of what value you're trying to produce for your users is an open invitation to failure and the honey around the analysis paralysis trap. Once you're drawn into the trap, making premature design decisions based on incomplete understanding of the problem quickly snaps the jaws of the trap shut and you'll be firmly stuck in analysis and design paralysis before you've even had a chance to produce a single line of code.
 
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have a look at our FAQ. I think the concept of an abstract class has to do with inheritance; twenty years ago inheritance was described as an absolutely essential part of object‑oriented programming and nowadays you hear people saying they hardly ever use inheritance.
I suspect I would prefer the Person interface. Why would you want different classes for teachers? Would you really want MathsTeacher extends Teacher and EnglishTeacher extends Teacher? Find out how to create subject objects and give each teacher a reference to their subject. Maybe an enum type would work for sibject, and maybe it wouldn't work. I haven't tried it out.

Did I really write, “subject objects”? Yes, I did.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are already standing with your feet hovering over that trap when you ask those kind of questions.

Tell us who your system's primary users are and what value they are going to get out of using the system.
 
Marshal
Posts: 7323
497
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dom Pearson wrote:Hello everyone! I am a bit confused about when to use abstract classes vs interfaces. For example, if I need to create a system for education with students and teachers what would be the best way to go about it?


Well, first would be a good idea to define the problem and write down the steps what the system supposed to do.

I need to create a system for education with students and teachers


That's not enough to start coding. You need more than that. But as for example, from that you could infer, that you may need a School, Student and a Teacher classes, that's all you know at this moment in time.

What else system needs to do? i.e. Student should be able to enrol to a Course? Here we go, we also know we may need a Course class. Get an idea? You describe what systems needs to do until you have a pretty big picture of the system (probably not the whole).


Dom Pearson wrote:Should I create a person interface, abstract class for teachers (to be able to create teachers for different subjects) and then a simple class for student?

Or should I make a person an abstract class and teacher interface and then create different teachers? What would be the best way to implement this example if I explicitly need to be able to create multiple teachers? I am quite confused of when to use which.


Too early to discuss that. Once you know about the project to some good extent, you can plan the coding part by seeing what you need. Which parts may be repetitive and could be extracted to abstract classes or interfaces if there is a need at all for that.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you succeed in coming up with a design diagram that you feel is "correct" you'll have nothing to validate that design. All you'll have is a complex class hierarchy that is in most likelihood, flawed at best and at worst, sorely insufficient for producing the value for your users that they need. Because when you take the approach of designing around classes and not around value, that's exactly what you'll get.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
That's not enough to start coding. You need more than that. ... What else system needs to do?


That's a better line of questioning: ask about behaviors. But not just behaviors that are in the system but behaviors of your users and how those behaviors interact with the system to produce value for the users. Also think about constraints. How many courses are teachers allowed to teach during a term? Are students allowed to pre-enrol in courses in advance of a term? How well in advance? Do they need to drop from a course or change schedules? What are the main features of the system that you want to provide? Obviously, in any development effort there is a limited amount of time and effort you can apply to delivering value. You have to prioritize what features to build with the budget of time and effort you have so you can maximize the value you're delivering with that investment.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Education is a huge pie. I would ask questions that help me carve out the slice of that pie that I want to serve up first. Are we talking about enrolling for courses? Are we talking about planning students schedules? A grading system? A billing system? A system that tracks progress towards the completion of requirements for a degree? Or maybe this is teacher focused. Do teachers need to analyze exam results to improve the curriculum? Or inform the decision as to what the optimal class size is for a course? Is this going to be a way to manage information for the purposes of grading? Or for the purpose of delivering course content? As you can see, in just a few minutes, you already reveal a lot of complex ideas and relationships.
 
Dom Pearson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is just a simple project that I want to write for myself. Basically it's just a student and teacher. The teacher has to have information about the student and the student should have information of his subjects. The program also needs to be able to create multiple teachers.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of the size, it's still better to think about behaviors before you decide on classes and attributes. An object-oriented system design is not complete if you don't consider behaviors. What are the main things should a user of your program be able to do? If the program's needs to create multiple teachers, what value does that produce and to whose benefit is it?
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dom Pearson wrote:It is just a simple project that I want to write for myself. Basically it's just a student and teacher. The teacher has to have information about the student and the student should have information of his subjects. The program also needs to be able to create multiple teachers.



This small statement already has a few contradictions that will generate some clarifying questions.

You said Its just a student and teacher (singular) and yet you also need to create multiple teachers.

You said the teacher (again singular) has to have information about the student (singular) so there's only going to be one student? Unlikely but the language has to be precise to avoid misunderstanding.

Assuming that you actually meant that there will be multiple students and teachers, what kind of information about a student is relevant? What behaviors, if any, are common between students and teachers?

Generally speaking, I don't know if I'd even worry about deciding between abstract classes vs interfaces if the above is all you're able to clearly articulate.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to think about designing around classes & interfaces versus designing around behaviors and value: classes and interfaces are the cogs and components of the machine you're designing. Until you know how they move and interact with each other, they're just static things that are of little value. You can spend a lot of time polishing and grinding a gear that will eventually go into your engine but until you know what that gear actually does, it's just another gear. Is the gear part of the drive train? Or is it a gear that controls the movement of the engine valves? Or perhaps it's a gear that's used in the differential? Where will this gear go and what is its purpose? Those are behavioral type questions. You can't just say "I'm going to need a Student class and a Teacher class" without clearly knowing what these classes are for and how they will relate to each other. That's just a recipe for over-engineering and wasted effort.

Further to the car design analogy, you might ask "How fast do I want this car to be able to go?" and if that's an indication of the value you want to get out of your design, then you design your gear with speed in mind, favoring weight, dynamic balance, and heat resistance as critical design factors. However, if answering the question "How much load do I want this vehicle to be able to handle?" then that might steer your design towards favoring strength, durability, and reliability at low speeds.

Likewise, designing classes in an education system should take different factors into consideration, factors that are determined by the intended use and behaviors expected from the system.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me ask you this way: If you wanted to finish one basic feature of this program you're planning to create, what would it be? Say you wanted to be done before the weekend is over. That means you have about three days to get from concept to running program with a useful feature. What would that single most important feature be?
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!