I have seen the class with public and without modifiers and method with public, protected or private modifiers. I am confused how to decide modifiers for a class?
2 1608 BigDaddyLH 1,216
Recognized Expert Top Contributor
I have seen the class with public and without modifiers and method with public, protected or private modifiers. I am confused how to decide modifiers for a class?
The choice of modifier, whether it be for a top-level class or interface, a class member or a constructor, always comes does to the same question: how visible should this thing be? The answer to that question can only come from the context of your problem, not from an answer in a forum, but a general rule of thumb is to give as restrictive access as you can: give top-level class's default access and class members and constructors private access. As you develop you will find some of this is too restrictive -- then you think about making access wider. This is easier to do that making access too easy then realizing much alter that was a mistake.
Rule of thumb for a new programmer:
Data is private within a class, methods are public.
If we have the class Thing, for instance: - public class Thing{
-
// private data
-
private String name;
-
private int age;
-
-
// public accessor methods
-
public String getName(){
-
return name;
-
}
-
-
public int getAge(){
-
return age;
-
}
-
-
// public mutator methods
-
-
public void setName(String n){
-
name = n;
-
}
-
-
public void setAge(int a){
-
age = a;
-
}
-
-
}
In most of your typical introductory work in Java, creating classes in this way will allow you to make your classes public 99% time. As stated above, however, more advanced programming (requiring inheritance and interface classes) will cause you to start recognizing more effective ways of declaring things public or private...
Creating these kinds of accessor (get data) methods and mutator (change data) methods is usually considered good practice...
Until again,
Tom
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: kchalla |
last post by:
Can anybody let me know the difference between public and private class
access modifiers? Thanks in advance
|
by: Jason Heyes |
last post by:
Is this class evidence of poor design?
class Rectangle
{
double width, height;
public:
double get_width() const { return width; }
double get_height() const { return height; }
void set_width(double width) { this->width = width; }
|
by: Shea Martin |
last post by:
I have been programming in C++ for over 4 years. I *think* I knew that
a struct could have a constructor but I decided to dig into it a little
more today, and found that there is very little difference between a
struct and a class in C++. Both have inheritance, access modifiers,
etc. The only diff I see is the default access level is public vs.
private.
I have always just used structs as a minimal class, as that is the way
|
by: kuvpatel |
last post by:
Hi
I want to refer a class called LogEvent, and use one of its methods called
WriteMessage without actually having to create an instance of Logevent.
I have tried using the word sealed with the class and this works but I would
also like to know of other ways to do this.
Also are there any performance implacations of using sealed?
|
by: Dennis C. Drumm |
last post by:
Is there a way with C# to allow one class access to a method or field of
another class, without making that method or field visible to all other
classes, as would be the case when making the method or field public?
Thanks,
Dennis
| |
by: Andy |
last post by:
Hi all,
I have a site with the following architecture:
Common.Web.dll - Contains a CommonPageBase class which inherits
System.Web.UI.Page
myadd.dll - Contains PageBase which inherits CommonPageBase
- Contains myPage which inherits PageBase
Each of these classes overrides OnInit and ties an event handler
|
by: hamil |
last post by:
I have a form with one button, Button1, and a Textbox, Textbox1
I have a class, class1 as follows.
Public Class Class1
Public DeForm As Object
Sub doit()
DeForm.Textbox1.text = "It works"
End Sub
End Class
|
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= |
last post by:
Hello everybody,
this is my first post to a newsgroup at all.
I would like to get some feedback on one proposal I am thinking about:
--- begin of proposal ---
Proposal to add
signed/unsigned modifier to class declarations
to next revision of C++ programming language
|
by: Mayur H Chauhan |
last post by:
All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.
Protected Class Book
End Class
Even same for...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |