How to hide the public method in derived class? - oop

I have three public method in base class. When i inherit the base class to derived class, the three public method should not get accessed.
How to achieve this?
Can someone hep me out to find answer for this?

According to basic OOP.
If a class have some methods and you inherit that base class to derived class. You can do override these methods BUT. You can only maintain it's accessibility or you can improve it.

Related

OOP approach name when you override only private methods

Could someone tell me the OOP approach name when you override only private methods? I would like to read more on it, but I do not know what to look for since I forgot the name of the approach.
The approach is about having only a single entry point in the base class in public methods, which in turn call virtual private methods which are overwritten by the children classes.
Template method design pattern (or, as Herb Sutter calls it, Non-Virtual Interface Idiom)

what is main/major difference between interface and abstract class in oop

I face this question in many interviews but here i exactly want to know the big difference between interface and abstract class in object oriented programming.
Anyone there?
Interface:
1.Interface is not class
2.Interface use for – Data Abstraction, Future implementation.
3. Interface contains only the abstract members.
4.Interface contains the declaration, the class which will implement it will have to define/add the logic on it.
Abstract Class
1.Abstract class is a class
2.Abstract class is use for a base class.
3. Abstract class contains both Abstract + normal members
4.The abstract members have only the declaration (like Interface),but the normal members are getting define.
One major difference of interface and abstract class is:
if interface contains 9 methods and we implements that interface then we need to override all 9 methods in our class.
if we take one abstract class and we extends that abstract class no need to override all 9 methods override only that method that wee need.
The main difference is that in interface we can only define the methods and variable and give body/values where it is implemented by overriding.
Where abstract class has the property of a normal class and an interface. We can just make the variables and methods abstract like interface and we can also add variables and methods with body/value in the same class.

Can we create a abstract class without using abstract keyword?

This question is asked by an interviewer in one of my interviews.
we can avoid creating instance of the class by using private constructor then we cannot inherit that class
Well you can use the private constructor, but that does not make your class abstract.
It just means that other classes cannot create a new instance of your class.
But your class can still call its own constructor through a static method, therefore creating a new instance of itself.
If you don't want a class to be instanciated at all, never ever, use the Abstract keyword...
If you want to use an object only in the "inheritance-hierachy" you need the protected-keyword (see also for access modifiers in general https://msdn.microsoft.com/en-Us/library/wxh6fsc7.aspx)
If you want to have an abstract behaviour, whats the reason for not take abstract keyword?

Differences between interface and abstract class

I was going through the differences between Interface versus Abstract class on MSDN but got some confusion on below difference:
If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface
I have confusion on this line
on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface**
Could you help explain?
methodes you write in the interface you should obligatory put it in your class Which implements in the iterface
and the abstract class is the opposit of interface the methodes you put in class abstract you can use it in your class or not.
An abstract base class is a class whose sole intent is to be derived. An interface is the set of public methods of a concrete class.
While they differ very little in actual functionality, I like to think of interfaces as an adjective while an abstract class is a noun. For the classic instance of an animal, an interface is used do describe attributes, like pet-able, friendly, dangerous. On the other hand, an abstract class would describe the thing itself more narrowly, like canine, feline or human.

What the need for abstract class here?

One of the interview questions which they asked me very recently.
What is an abstract class in C#?
Abstract Class - is a class which cannot be instantiated & can include abstract or instance methods, constructors etc.
Instance methods can be used for implementing common functionality for all derived classes which inherits this abstract class.
Ok...Now the question is why we need an abstract class here for implementing the common functionality. The same common functionality i can put in a base class and let the derived class can access the base class methods..right?? if thats the case, instead of having an abstract class.. can i go for base class?? or to put the common functionality in abstract class and let the derive class do access those methods?
If my base class can solve the implementing of common functionality, then why do we need an abstract class for common functionality???
Need help in proper understanding....
An abstract class is a base class - but it's a base class which typically has some abstract members. This allows you to write code in the abstract class which calls those abstract members, but without any implementation... it then forces concrete classes deriving from the abstract class to implement those members.
The benefits of having an abstract class rather than just a concrete base class where some members throw NotImplementExceptions are:
Derived classes are forced (at compile-time) to implement the relevant members
Clients can't just create an instance of the base class; they have to use a concrete class which provides the appropriate "missing" members.
As an example, the other day I wrote an abstract class class extending JsonConverter in Json.NET. The aim was for it to be a base class for any converters for structs within my project - it would handle all the nullable type conversions (and deserializing empty JSON to a null value), leaving the derived classes free to just concentrate on the type-specific part. Have a look at it (NodaConverterBase) and see if it makes things any clearer.
An Abstract Class is used as a Base class. There are times when you may want to have common functionality be implemented but not directly instantiate an instance of your Base Class Type so you would use an Abstract Class instead.
Look at it this way, if you were writing an app to keep track of zoo animals you would have classes for Monkey, Elephant, Crocodile, etc... They are all animals, so you would have them inherit a base class of Animal (because they all need basic functionality like eating, breathing, etc...). But you would never have an animal that's just an animal, so you would make Animal abstract because you would never need an instance of it.
The tricky phrase is
"partial implementation."
In addition to what you mention in your post, the abstract class does not have to implement all the functions AND the children MUST implement the unimplemented functions or be themselves declared abstract.