+ 12
What is the need of making multiple classes in java if you can do all those things in one main class?
working with multiple classes
19 Answers
+ 30
Imagine a bank system.
You need to have a class User, and a class Administrator.
Adminisrator will charge money on accounts.
User can check the current money in their accounts and substract money.
See? they have different actions, so they need to have different behaviors.
+ 13
Would you want a single book containing all the subjects in this world or just the one u r looking for?
Would it make sense to put the contents of Physics Maths, Biology, Environment, Sports Magazine, News Magazine, X-Men comics, Almirah, Chair, Table, Bed, Stone in one place?
Maybe, but you would most probably prefer them to be separately defined classes
+ 8
The answer is very simple, Since Java is an Object oriented Language, it is following ENCAPSULATION, ABSTRACTION, INHERITANCE, POLYMORPHISM
making multiple Classes Concept is based on these principles only.
if Understand the Object oriented programming then you will understand the reason behind dividing program into multiple Classes.
To achieve Modularity, Readability, Identifying Exception we divide our code in classes and Methods.
Classes are nothing but a module which perform particular operation.
+ 4
in single line..
WE NEED MULTIPLE CLASSES TO CREATE DIFFERENT TYPES OF OBJECTS.
first of all Java is simple object oriented language.
object oriented means you focus on managing data more than functionality.
classes are not mendetory in OOP, objects are...
as I said before main function of classes to provide proper structure. so we can create objects which are RELATED to each other. although there are many other functionalities provided by classes, this is most important & this is the one for which classes exist.
if you use one class than your code mostly look like process oriented programming rather than object oriented programming.
there is many OOP language exist which do not support classes.
+ 3
Because it's worth some inefficiency to have a million-line program that scales.
+ 3
Single class would make the program clumpy and unscalable if need be. Java is to be Simple and easy to be understood. So, better make it that way.
+ 3
imagine each page in an encyclopedia is a class by itself, now put them together in the main and you end up with over 10k lines of code and then close your eyes and press delete.... save the code, open it then let someone run and debug it. It is easier to debug a function or a class based on logic or some form of abstraction than going through all of them at once. additionally it supports code reuse and makes it easier for other programmers to digest your code.
+ 2
In a nutshell, each OOP source file (regardless of language) is a self-sufficient entity. Of course, allowing access to that entity (such as a Java Class) requires that it be declared PUBLIC - likewise with any of its methods which are intended to be accessible. Depending on the specifics of the compiler, any non-declared Classes/methods default to PRIVATE or PROTECTED.
The point of it all being that any ADDITIONAL CLASSES included in the same file are intended to only be used by the NAMED CLASSES and forbidden to any outside accessibility. Generally, such 'helper Classes' are a sign of poor programming - the few rare instances where such is actually needed/useful, one should properly use a STRUCT rather than a CLASS.
+ 2
modularisation. To make the program easier to maintain and edit in the future
+ 1
There is always the rule of relationships in every subject, making different classes apart from the neatness and clarity it improves, it generates cousin methods, and siblings, parents, grand parents and all kinds of relationships you could think of..
+ 1
multiple classes can categorize methods
which is main purpose of oop based languages
to make problem more closer to real world
+ 1
To add on to Amol Jandial's example, consider a full bank system. It would have a generic User that can do the minimum stuff anybody could do.A Teller would be a User with the ability to take deposits and handle withdrawals. A different type of User works have the ability to open accounts. An InvestmentManager could work with different types of accounts and other functions.
By noting where capabilities are shared, a class hierarchy can be constructed that would allow the common bits between each type of person to be centralized into a tree of classes, while isolating the unique functions of each. If the common stuff needs to change, it gets adopted by the proper classes with far less pain than cramming everything into one blob.
+ 1
As java is object oriented, each behaviour is defined in each class and their actions are defined in respective classes as methods... Its easy to manage code that way.... And once u have several classes n respective methods, the compilation time comes down.... By directly reaching to the class method called.
0
woow.. completely newbie question, I suggest you need to learn OOP first before asking something like that..
0
The Clear-Cut answer you can say..
Starting with the meaning of class: - The class have the attribute and behaviour of the object...
You can have the attribute and behaviour of a person/alien using a class say attributes like(nose, head, eyes) and behaviour(talking,sitting,walking..) but you can't set all these things of a chair or table or (summed up)Furniture..you cant say if a Furniture has nose(attribute) or Furniture walk very fast(Behaviour)..OR we can't say if an Alien is Made up of wood(attribute) or hoding files weighing 2kgs.(behaviour).
So for Furniture/another type of object, you hava to make another class..WHICH HAVE THE ATTRIBUTES AND BEHAVIOUR OF THAT DIFFRENT THING(in our case Furniture).
I hope this hilarious way made the concept clear of CLASSES and hence I Hope made you understand why we need multiple classes in java.
0
A primary purpose for multiple classes is extensibility. If you write everything in one class/file, when the need to extend the functionality of your program arises, it is much easier to extend a simple class file rather than a complex file with multiple inner classes and methods. It can also make things much easier to read and understand. If you had a class Engine() and SparkPlug() and the method fire(), it makes a LOT more sense contextually to put that method in the SparkPlug() class.
In the end, however, it is entirely a design choice made by the programmer; you decide if it's a good choice when looking towards the future of your program. If there are or will be others working on your code, making things easier to read and understand is always a good thing. Even if it's going to be a simple program, though, it's good to maintain the practice so you stay in that mindset when you work on larger projects.
- 1
modularity dude
- 1
nothing....you can just add that class with specifiers needed....but here you have to call them using their respective objects....
- 2
Join my programming group
https://chat.whatsapp.com/1ItUX4UaQxMAI7i31lA84W