472,791 Members | 978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

Class Modifiers and Member Modifiers

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 1557
BigDaddyLH
1,216 Expert 1GB
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
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
by: kchalla | last post by:
Can anybody let me know the difference between public and private class access modifiers? Thanks in advance
18
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...
11
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...
5
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...
4
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...
5
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...
19
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"...
10
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...
8
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.