+ 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

26th Dec 2016, 4:36 AM
Amol Jandial
Amol Jandial - avatar
19 odpowiedzi
+ 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.
26th Dec 2016, 4:42 AM
Nahuel
Nahuel - avatar
+ 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
26th Dec 2016, 10:32 AM
Caffeinated Gamer YT
Caffeinated Gamer YT - avatar
+ 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.
27th Dec 2016, 7:23 AM
Jenny Lance
Jenny Lance - avatar
+ 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.
27th Dec 2016, 1:50 AM
Neel Patel
Neel Patel - avatar
+ 3
Because it's worth some inefficiency to have a million-line program that scales.
26th Dec 2016, 11:31 AM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
26th Dec 2016, 12:57 PM
harish swami
harish swami - avatar
+ 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.
26th Dec 2016, 11:25 PM
Andre Hutchinson
Andre Hutchinson - avatar
+ 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.
26th Dec 2016, 2:08 PM
Surendra Kumar
Surendra Kumar - avatar
+ 2
modularisation. To make the program easier to maintain and edit in the future
28th Dec 2016, 7:34 AM
freakshow
freakshow - avatar
+ 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..
26th Dec 2016, 8:57 PM
Yahye Ahmed
Yahye Ahmed - avatar
+ 1
multiple classes can categorize methods which is main purpose of oop based languages to make problem more closer to real world
27th Dec 2016, 1:25 PM
Anuj Tiwari
Anuj Tiwari - avatar
+ 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.
27th Dec 2016, 7:48 PM
Joe Sewell
Joe Sewell - avatar
+ 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.
30th Dec 2016, 4:59 AM
Uday Krishna
Uday Krishna - avatar
0
woow.. completely newbie question, I suggest you need to learn OOP first before asking something like that..
27th Dec 2016, 12:34 AM
MUHAMMAD HASBI
MUHAMMAD HASBI - avatar
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.
27th Dec 2016, 8:38 AM
Chakshu Bhateja
Chakshu Bhateja - avatar
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.
27th Dec 2016, 4:27 PM
Dashum
Dashum - avatar
- 1
modularity dude
27th Dec 2016, 7:32 AM
sorajit nath
- 1
nothing....you can just add that class with specifiers needed....but here you have to call them using their respective objects....
27th Dec 2016, 1:40 PM
Deepak Sharma
Deepak Sharma - avatar
26th Dec 2016, 5:13 PM
subhjeet bej
subhjeet bej - avatar