answersLogoWhite

0


Best Answer

You don't. The whole point of private methods or fields is that you can't access them directly from outside the class. You can call the public methods, and in some cases protected methods, and those might indirectly invoke the private methods.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you call private method outside the class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you access a base class method from a derived class in Cpp?

Use the scope resolution operator (::) to explicitly call the base class method. Note that the base class method must be protected or public in order for a derived class to access it. Private members are only accessible to the class itself and to friends of the class, regardless of whether the derivative uses public, protected or private inheritance. It is quite normal for a base class to provide a "default" implementation for a virtual method for which a derived class may override. Although the derived class will generally provide its own implementation of the method, it may also call the base class method either before, during or after performing its own implementation. The following shows minimal class declarations demonstrating a call to a protected base class method from a derived class override. class base { protected: // accessible to all instances of this class, its friends and its derivatives. virtual void method(){ /* do something */ } }; class derived : public base { public: // full-accessible outside of class. virtual void method(){ /* do something (or do nothing) */ base::method(); // call base class method. /* do something else (or do nothing) */ } };


How do you call a member function of one class in another class using friend function?

class B; // forward declaration. class A { private: void myFunction(B b){b.myFunction();} // Calls private method in b. }; class B { friend void A::myFunction(B b); // Friend function declaration. private: void MyFunction(); };


Can you have virtual constructor in cpp?

A virtual function is a method defined and implemented in a base class that we are expected to override in our derived classes.When we derive a class from a base class, we automatically inherit all the public and protected members of the base class, but we are free to override any and all the methods, including private methods (we can even change the access type if we wish).However, when we override a method in the base class, we can only call that override when we actually have a reference or pointer to the derived class itself. If we cast the reference or pointer to the base class we will end up calling the base class method. This is expected behaviour and is normally fine, but what happens if we have a pointer to a base class but we actually want to call the derived class method? This is particularly important when that call comes from the base class itself. How will it determine its actual type and make the correct call?We could use runtime information within the base class to determine the actual type, but this would break a fundamental rule of encapsulation: a base class should NEVER be concerned about the inner workings of any of its derived classes. If we allow this, we'd be forced to update the base class every time we derived a new class from it. Apart from the performance penalty incurred with accessing runtime information, maintaining the base class code will quickly become unmanageable.That is where virtual functions come in. Even though we are pointing at a base class, when we call a virtual function we actually call the derived class method instead. This is extremely powerful: we are no longer concerned with the actual type of object any more. We can treat the base class as if it were a generic data type, and the compiler will know exactly which version of a function to call regardless of whether the call was made from the base class or not.But what if we want to call the base class method? Simple: make an explicit call to it. We don't need runtime information for this since every derived class is also a base class as well. The only exception is when the virtual function is declared private in the base class. In this case, we are not expected to call the base class method at all (only the base class and friends of the base class can make an explicit call to its private methods).


How do you call base class method using derived class object?

If the base class method is non-private, the derived class can call the base class method implicitly. However, if the derived class overrides or overloads the method, the base class method must be called explicitly. The following demonstrates explicit calls to base class methods: #include <iostream> using namespace std; class Base { public: Base(){} void Foo(){ cout << "Base::Foo" << endl; } }; class Derived : public Base { public: Derived(){} void Foo(){ cout << "Derived::Foo" << endl; Base::Foo(); } }; int main() { Derived derived; derived.Foo(); derived.Base::Foo(); // Explicit call to base class method. return(0); } Output: Derived::Foo Base::Foo Base::Foo


What is function overriding in Java?

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.

Related questions

Can you implement a constructor in private part of code?

no we cannot initialize a constructor in private in order to call a constructor from outside of a class it must be a public member.in order to create an object we should call the constructor .so only private members can implement outside of the class.


How do you access a base class method from a derived class in Cpp?

Use the scope resolution operator (::) to explicitly call the base class method. Note that the base class method must be protected or public in order for a derived class to access it. Private members are only accessible to the class itself and to friends of the class, regardless of whether the derivative uses public, protected or private inheritance. It is quite normal for a base class to provide a "default" implementation for a virtual method for which a derived class may override. Although the derived class will generally provide its own implementation of the method, it may also call the base class method either before, during or after performing its own implementation. The following shows minimal class declarations demonstrating a call to a protected base class method from a derived class override. class base { protected: // accessible to all instances of this class, its friends and its derivatives. virtual void method(){ /* do something */ } }; class derived : public base { public: // full-accessible outside of class. virtual void method(){ /* do something (or do nothing) */ base::method(); // call base class method. /* do something else (or do nothing) */ } };


How do you call a member function of one class in another class using friend function?

class B; // forward declaration. class A { private: void myFunction(B b){b.myFunction();} // Calls private method in b. }; class B { friend void A::myFunction(B b); // Friend function declaration. private: void MyFunction(); };


How do you call main class with in main class in java?

We can't call a class. We always call a method in java.


Can you create a class in Call of Duty 3?

no, im private first class and i dont have it. and that is level 4 (private first class)


How do you call a method from inside the main method?

I assume the question is about Java. How you call a method from inside the main method depends on whether this is an instance method or a static method. To call an instance method, you need to have a valid instance. Say foo is an instance of class Foo, which was created inside the main method. Then to call the instance method implode() use foo.implode() To call the static method bar() of class Foo, use Foo.bar().


What is the basic need of method overwritting in JAVA?

Maybe what you mean is method over loading. And that basically means that you can write different methods with the same name, which for example will allow you to call a method with few parameters, and let tat method fill the default values for the real thing. Sample private String my_method (String a, Integer b, SomethingElse c) { do your stuff... return "I did it"; } private String my_method (String a) { Integer defaultB = new Integer (5); SomethingElse defaultC = new SomethingElse (); return (my_method (a, defaultB, defaultC)); } .... // You can call: System.out (my_method("Who did that?")); There is no concept of Method Overwriting in Java. We have Method overloading & method Overriding. Overloading is already explained. If you are asking about Method Overriding pls read on... Overriding is a feature in Java where you can override (mask) the features of the parent class in the child class. Lets say you have a method printName() in the parent class which prints the name of that class. When you extend this class in a child class if you call the method printName() the name of the parent would be displayed. Which you may want to change. If you want to display only the name of the child class then you can write the method printName() in the child class. On invocation the name of the child class would be printed. Tip: If you want to display the name of the parent class somewhere you can invoke it by using super.printName() because once you override that method in the child class you cannot access the parent class method directly.


What are the different ways in which an object can access another object in a language like C plus plus?

Accessibility to class members is determined by whether those members are declared private, protected or public. Private members of a class are accessible to all instances of that same class, as well as friends of the class. Protected members of a class are accessible to all instances of that same class, as well as friends of the class, and also classes derived from the class. Public members are accessible, both inside and outside of the class, including other classes. How you access the members of a class depends on whether you're dealing with an object reference or a pointer to an object and whether the methods are virtual or not. Accessing members of an object reference is via the reference member operator (.), while members of a pointer to an object are accessed via the indirection operator (->). If a member method is virtual, the most-derived member method will be invoked, regardless of whether a reference or pointer refers to a derived class or one of its base classes. To specifically call a base class virtual method, you must use the scope resolution operator (::) to explicitly specify which class method you actually want to call.


What do you call a soldier who just started?

Private First Class or "Rookie".


A subclass can call constructor method defined by its super class?

True


Can you have virtual constructor in cpp?

A virtual function is a method defined and implemented in a base class that we are expected to override in our derived classes.When we derive a class from a base class, we automatically inherit all the public and protected members of the base class, but we are free to override any and all the methods, including private methods (we can even change the access type if we wish).However, when we override a method in the base class, we can only call that override when we actually have a reference or pointer to the derived class itself. If we cast the reference or pointer to the base class we will end up calling the base class method. This is expected behaviour and is normally fine, but what happens if we have a pointer to a base class but we actually want to call the derived class method? This is particularly important when that call comes from the base class itself. How will it determine its actual type and make the correct call?We could use runtime information within the base class to determine the actual type, but this would break a fundamental rule of encapsulation: a base class should NEVER be concerned about the inner workings of any of its derived classes. If we allow this, we'd be forced to update the base class every time we derived a new class from it. Apart from the performance penalty incurred with accessing runtime information, maintaining the base class code will quickly become unmanageable.That is where virtual functions come in. Even though we are pointing at a base class, when we call a virtual function we actually call the derived class method instead. This is extremely powerful: we are no longer concerned with the actual type of object any more. We can treat the base class as if it were a generic data type, and the compiler will know exactly which version of a function to call regardless of whether the call was made from the base class or not.But what if we want to call the base class method? Simple: make an explicit call to it. We don't need runtime information for this since every derived class is also a base class as well. The only exception is when the virtual function is declared private in the base class. In this case, we are not expected to call the base class method at all (only the base class and friends of the base class can make an explicit call to its private methods).


How do you call base class method using derived class object?

If the base class method is non-private, the derived class can call the base class method implicitly. However, if the derived class overrides or overloads the method, the base class method must be called explicitly. The following demonstrates explicit calls to base class methods: #include <iostream> using namespace std; class Base { public: Base(){} void Foo(){ cout << "Base::Foo" << endl; } }; class Derived : public Base { public: Derived(){} void Foo(){ cout << "Derived::Foo" << endl; Base::Foo(); } }; int main() { Derived derived; derived.Foo(); derived.Base::Foo(); // Explicit call to base class method. return(0); } Output: Derived::Foo Base::Foo Base::Foo