473,785 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class Modifiers and Member Modifiers

20 New Member
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?
Mar 4 '08 #1
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.
Mar 4 '08 #2
tburger
58 New Member
Rule of thumb for a new programmer:

Data is private within a class, methods are public.

If we have the class Thing, for instance:

Expand|Select|Wrap|Line Numbers
  1. public class Thing{
  2.     // private data
  3.     private String name;
  4.     private int age;
  5.  
  6.    // public accessor methods
  7.   public String getName(){
  8.       return name;
  9.   }
  10.  
  11.   public int getAge(){
  12.       return age;
  13.   }
  14.  
  15.   // public mutator methods
  16.  
  17.   public void setName(String n){
  18.       name = n;
  19.   }
  20.  
  21.   public void setAge(int a){
  22.      age = a;
  23.   }
  24.  
  25. }
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
Mar 5 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1750
by: kchalla | last post by:
Can anybody let me know the difference between public and private class access modifiers? Thanks in advance
18
1433
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; }
11
5722
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
5
14438
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?
4
4642
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
5
3164
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
19
1981
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
10
3310
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
8
2597
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...
0
10336
Oralloy
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...
0
10155
jinu1996
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...
0
9953
tracyyun
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...
0
8978
agi2029
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...
1
7502
isladogs
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...
0
6741
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();...
0
5383
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...
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.