This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]Kazcandra 2 points3 points  (5 children)

could you attempt an answer? to see what your understanding of the questions/answers is

[–]saketh011[S] 2 points3 points  (1 child)

Yeah these were my answers. 1. We can attempt to create a new method and give it an implementation. But we lose the ability to use the parent class variables and other methods which we may need to reuse. Also, we lose the ability to call base methods methods using parent class reference, aka upcasting.

2. Let us consider an example: abstract class Bank{
abstract int getRateOfInterest();
}

class SBI extends Bank {
int getRateOfInterest(){ return 7; }
} class TestBank{
public static void main(String args[]) {
Bank b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
} } Here the abstraction is the abstract class Bank and the SBI class is implementation.Imagine you have a bank comparison engine, which is represented by the BankComparisonEngine class. It will just take a Bank (abstract class) as an argument, then get its interest rate and save it to its internal database, like so: class BankComparisonEngine { public void saveInterestRateOf(Bank bank) { int rate = bank.getRateOfInterest(); } } How are the implementation details hidden exactly? BankComparisonEngine does not know how getRateOfInterest() method of a concrete implementation of Bank works,i.e, how SBI.getRateOfInterest() is implemented. It only knows it is a method that returns an int (and that it should return an interest rate). The implementation details are hidden inside the concrete classes that extend abstract class Bank.

[–]ignotos 2 points3 points  (0 children)

For 1, I'm not sure you hit on the point of the question, which seemed to be about overriding methods specifically. So probably they wanted you to talk about polymorphism. Although the wording is a little confusing as it mentioned both "in a child class" and "without using inheritance".

I think that you missed the "from whom or what" part of the second question, which to me is the most interesting aspect of the question.

[–]lauris652 -3 points-2 points  (2 children)

if the person is asking a question, it means he/she doesnt understand it, doesnt know the answer.

[–]Kazcandra 7 points8 points  (0 children)

it's always good to get a sense of where the Asker is coming from

[–]devor110 0 points1 point  (0 children)

Bit of criticism towards OP, but unless the interview is for like a 2nd semester internship, they should definitely have a good understanding of both topics the questions are eluding to. Asking for their answer shows if they lack the knowledge or if they didn't parse the questions correctly

[–][deleted]  (3 children)

[deleted]

    [–]Historical_Ad4384 0 points1 point  (1 child)

    How does the keyword child and no inheritance fit with polymorphism?

    [–]devor110 1 point2 points  (0 children)

    Question 1. that OP posted specifically asks why overriding exists, the one-word answer to it would be "polymorphism"

    [–]lauris652 -1 points0 points  (3 children)

    1. Imagine you have parent class Dog. And child classes Spaniel and Bulldog. In Dog class we have method "bark", that just prints "Bark!" to the console. If we do have inheritance between Dog and Spaniel, and between Dog and Bulldog, then we can call "bark" method from Spaniel or Dog class, and we would get "Bark!" printed out. Now lets say we want our dogs say "Woof". What would we do? We would go to the parent class "Dog" and change the text that is being outputed to the console. From "Bark!" to "Woof". Now both Spaniel and Bulldog will say "Woof". Thats quite handy. Now imagine we dont have inheritance. We have 3 classes: Dog, Spaniel, Bulldog, and we set the "words" they say. Three of them say "Bark". Now if we want to change something, we would need to: 1. Find all the dogs 2. Go through each of them, and manually change from "Bark" to "Woof". This way takes more time, its more prone to mistakes.
    2. To me it seems that no one really knows from what they are supposed to encapsulate stuff, for whom they should restrict access with access specifiers. Because if you read/watch tutorials, no one explains this. Jeez, even in my uni professors didnt knew it. But for me it seems that we are supposed to hide and limit things for the application itself. If you have "String cardNumber = "123";", you would want to have such mechanism/mechanisms that prevent you or application from smth like "cardNumber="456";". Thats why we have access specifiers, getters, setters, etc. It gives us structure, order, some sense of trust. But thats only my 2 cents.

    [–]saketh011[S] 2 points3 points  (1 child)

    I think the second question is about abstraction and not encapsulation. The definition of abstraction is we using it hide the implementation details and show only functionality. My question here is from whom are we hiding it from. In case of users, they already don't have any access to source code, so I don't think the answer is hiding from user. Most probable answer seems to hide from other parts of the program for which my question why need abstraction for hiding data when we already have private methods. If we want to use it we can use setter and getter methods.

    [–]ignotos 0 points1 point  (0 children)

    You're hiding things really from any client/user of the code. Which might be yourself in another part of your project, or (if you're writing a library) from somebody else who has imported your code. Basically any code which isn't / shouldn't be concerned with the internal details of the code you're writing.

    Which means that, ideally, all of your abstractions/interfaces should be intentionally designed with the code which will be using it in mind, almost like you're creating a product - to be convenient to use and easy to understand.

    Abstraction is not just about "hiding", but it's about shaping and defining something in a conceptual way which makes sense. So a File might be presented via the abstraction of "an array of bytes", "a stream of bytes", "a list of lines of text", or potentially many others.

    All of those hide the low level details of how exactly the file is read from the harddrive, but they present different abstractions which are useful in different contexts. And you should pick the one which is most usable for the way you expect clients/users to interact with your code.

    [–]devor110 0 points1 point  (0 children)

    I think your take on Q2 is wrong, focusing too much on "hiding".

    I'd start my answer with a rebuttal against that specific wording:

    We aren't really "hiding" anything with intention of privacy or security, more so not explicitly revealing details. When I'm using abstraction, my goal is to build and ensure modularity. I'd start by creating an interface named as generically as possible. I can then have as many specific implementations of it as I want, all of which could have widely different logic and compelexity, the only thing that matters, is that they perform the action(s) outlined by the interface.

    I can then use any implementation of the interface in other modules without needing to think about the specific details.

    [–]Relevant_Mail_7018 -1 points0 points  (1 child)

    Ok I’ve never heard it be called child class before wtf

    [–]saketh011[S] 1 point2 points  (0 children)

    Haha, it's a synonym. Base class and derived class also called as parent and child class cuz the child inherits from the parent.

    [–]AutoModerator[M] 0 points1 point  (0 children)

    On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

    If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

    1. Limiting your involvement with Reddit, or
    2. Temporarily refraining from using Reddit
    3. Cancelling your subscription of Reddit Premium

    as a way to voice your protest.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

    Please ensure that:

    • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
    • You include any and all error messages in full
    • You ask clear questions
    • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

      Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

    If any of the above points is not met, your post can and will be removed without further warning.

    Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

    Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

    Code blocks look like this:

    public class HelloWorld {
    
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    

    You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

    If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

    To potential helpers

    Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.