i am reading a book and it says:
"Two access modifiers you have looked at so far are public and private. If a
method or property of the base class is exposed as public, it is accessible by both the derived class and
any client of the derived class. If you expose the property or method of the base class as private, it is not
accessible directly by the derived class or the client."
what does the 'client' means?
A client in this context means a piece of code outside the class (neither part of the class nor part of a derived class) which uses the class.
That's it, really. Like the client of a bank or the client of an accounting firm, it's something that uses the thing you're discussing (be it bank, accounting firm or class).
Public and private (and other variations) allow you to control how much of the internals of your class are exposed to the outside world. Minimising this exposure is known as encapsulation (information hiding) and is generally considered a good thing.
Related
I was asked this question in one of my interviews and still dont have answer to it. If by abstraction we only mean , you not able to instantiate object(as applicable for interfaces and abstract classes), so having a private constructor is the answer?
Abstraction is explained in varied ways all over internet . Even using System.out.println is abstraction as we dont know detail behind it .
Using factory classes is also abstraction as we dont know which subclass will be instantiated.
Calling any method within an API is also abstraction.
I am actually confused now, as to what the interviewer wanted as answer.
Abstraction is all about hiding implementation, like how the gas pedal to a car abstracts you from the various complexities in making a car go.
A simple way of doing it is to just use private members. GetActiveServers() could call no private methods, or 5 private methods. That doesn't really matter when we're using it, so long as it works efficiently as needed.
I think he was trying to trick you because of the abstract keyword sort've means something different from abstraction, but it's not entirely unrelated.
I have a series of classes that have a number of analogous Shared methods that I want to ensure are implemented in a consistent manner in each class. These Shared methods are called via reflection in various areas of code, and up to this point I’ve been keeping them in synch manually. As the number of classes grows, ensuring synchronization is becoming a bit tedious – as is the process to ensure, when I add a new Shared method to the paradigm, that it is indeed implemented in all of the classes to prevent run-time errors when reflection comes looking for them.
I’d initially thought this would be an ideal use of interfaces, but per various threads (such as Why we can not have Shared(static) function/methods in an interface/abstract class?) you cannot define Shared methods as part of an interface. I then tried to define a common base class and mark these methods MustOverride, but you cannot do that either. I’ve looked into delegates (which I’ve not used before), but it appears to allow the opposite of what I’m trying to do (namely, delegates seem to allow a common implementation of a common need from multiple classes instead of a unique implementation of a common need from multiple classes).
As such, is there an approach in VB.Net to ensure at compile-time that each of my classes implements a set of common Shared methods and functions in a consistent manner?
As a side note, the classes in question are currently all extensions of Entity Framework classes and are being blended into the EF classes.
EDIT: As a alternative to a language feature (which may not exist), I'd also be open to suggestions on any refactoring-type tools that provide this sort of functionality.
Here is sort of the skeleton of code I'm using:
Public MustInherit Class AbstractUpdateItem
Public MustOverride Function ValidateFields(ByRef dto As BusinessDTO) As Boolean
End Class
Public Class UpdateSomeFields
Inherits AbstractUpdateItem
Public Overrides Function ValidateFields(ByRef dto As BusinessDTO) As Boolean
' some code here to do the real stuff
End Function
End Class
When I comment out one of my Overrides Function (ValidateFields), I get a compile time error: Class x must either be declared 'MustInherit' or override the following inherited 'MustOverride' member(s): ValidateFields. So that would seem to ensure that they implement a set of methods. They aren't declared as shared, but will that do what you want?
I'm modeling a web application, which has some HTML pages, javascript files and 6 servlets.
Right now I have all the servlets in separate classes, but they all share the same methods (doPost, doGet, processRequest, with different implementations) and all of them have the <servlet> stereotype.
I was wondering if there is a way to represent all of them with one class. I'm going to do a text to describe the diagram, so maybe I can explain what that class represents.
As all the methods you cite (doPost, doGet, processRequest) seem to be inherited from Java's HttpServlet, you may as well represent all the implementations with their common parent class HttpServlet.
If the design goes ahead of implementation, I would suggest you create an interface with the common methods(doPost, doGet, processRequest); then different classes implement the same interface. You can optionally introduce an class with default implementation of the interface and let your classes generalize from this class.
If the implementation was already there, to be honest, it is not well designed. A refactor will be much better.
I am trying to define when to name a class as a Context and I am having a very difficult time. Could someone please define "Context" for me and explain when to name a class "Context"?
Dictionary.com (Random House Dictionary, © Random House, Inc. 2011) defines context as:
the parts of a written or spoken statement that precede or follow a specific word or passage, usually influencing its meaning or effect: You have misinterpreted my remark because you took it out of context.
the set of circumstances or facts that surround a particular event, situation, etc.
The second definition is the applicable definition in this case. Taking the HTTPContext class for example, we know that the class contains information about the web site at the current moment (including the request, the response, the session, the user, etc.)
Therefore, I would define a 'Context' in this regard as a class that provides information about a particular application or service; or provides access to objects and methods that describe the current state of an application (or a particular facet of an application, such as a DataContext class in Linq To SQL or Entity Framework).
If you look up the Oxford English Dictionary for "context" it tells us:
the circumstances that form the
setting for an event, statement, or
idea, and in terms of which it can be
fully understood:
In programming a "context" class would be a class that provides your application code or code running within a framework with a set of data or objects relevant and meaningful to the task at hand.
For example, ASP.NET's HttpContext object provides information about the current HttpRequest (cookies, headers, user agent and so on). This data is meaningful only in the "context" of the current request as it may and will change for other requests.
Qt has a build-in supprt for creating objects with integrated reference counting via QSharedData and QSharedDataPointer. All works great, but for each such object I need to write a lot of code: QSharedData-based implementation class with constructor and copy constructor, object class itsef with accessor methods for each filed.
For a simple structures with 5-10 fields this requires really lot of near same code. Is it some ways to automate such classes generation? Maybe it's some generators exists that take a short description and automatically generates implementation class and object class with all accessors?
You usually don't have to implement copy ctor or operator= when using QSharedData/Pointer. The default impls copy/assign the QSharedData-derived member, which usually does the Right Thing (TM).
For the public class, you need to implement the ctor creating the private object, and if the private class is not declared in the header but in the implementation (which is better), a dtor (doing nothing, the only point is that is not inlined and defined in the .cpp, after the private declaration).
For the private class, no method/ctor/dtor implementations are necessary.
For simple value-based classes, writing setters is of course tedious, but the same is true if you use plain private member variables. The overhead in LOC doesn't grow with the number of members.
And no, there is no standard generator solution for that I know of, although writing a script or emacs macro etc. doing it is not that hard. Probably would make sense to add such things to a publicly available toolbox, or QtCreator...
I don't think generators would exist for these things, but I suggest two things:
(ab)use existing shared containers (QVector, QList...)
read the documentation (with examples) on QSharedData, QSharedDataPointer, and QExplicitelySharedDataPointer
The two subclasses have simple examples that show how to implement the shared-ness it seems. I can't help you further though, because I've never had the need to create my own.
On second thought, why not make all data fields public, and use the QSharedData derivative as a struct-like class with reference counting? Maybe not nice on encapsulation, but if you're careful, nothing wrong should happen.