I am in the object oriented part i understand everything but the methods can you please explain what they are for and i am also having a hard time figuring out what i want it to be like void, int, string etc. and also the this.something = something i dont understand why we do that.
Thanks
Methods are like actions or functions. They contain code that operates on the data.
If you don’t want to have the method return something to the caller, its return type is void
. Otherwise it’s the type of the data you want it to return.
With this.
we make clear that we are referring to a member of the current object. We need that when a statement would otherwise be ambiguous. You see an example in the “Classes” section, lecture “Creating Classes” where there is a field called text
and a parameter that’s also named text
. With this.text
we make clear that we are talking about the field instead of the parameter.
Thank you for your help!
But i have another question, can you explain method overriding and comparing objects because i don’t really understand them.
Thanks
If a class inherits a method from its base class that method’s implementation may not fully fulfill the child classes needs. E.g. we may have introduced a new field in the child class that the method needs to consider. In this case we can override the method and provide an implementation that is suitable for the child class. We just need to implement the logic that is specific to our child class and can call the base
implementation for the behaviour that is common between the child and the parent class.
The standard implementation of the Equals
method (inherited from Object
) compares object references. So by default two instances of a class are not “equal”. Technically they are different. From a logical point of view (seen from the problem’s domain) we may want to consider two instances to be equal if they represent the same thing. E.g. we may have two instances of a User
object created from this forums posts that both represent a user with the id “Intijir”. Technically they are different and user1.Equals(user2)
would by default return false
. Since they both represent the same person, we would want to override User.Equals
and compare the ids instead of the references so we don’t e.g. accidentially add the same user twice to a collection.
i know i ask a lot but the last thing is can you explain dependency injections because i don’t understand them at all. And how to make interfaces work with classes and how to create methods in them.
Thanks alot
You are going through the course quite fast. Maybe you should do some excercises or experiments in between and/or revisit videos you didn’t understand. And BTW we’re getting quite off topic…
Dependency injection is a concept to seperate concerns, reduce coupling and make classes open for extension while being closed for modification,…
Say you have a business logic class that needs to write some logs. You could code some System.out.println
or new
up an instance of a ConsoleLogger class you wrote. What if you later need to write the logs to a database or send them via email? You would need to change your business logic class every time you want to change the logging target and maybe later end up with tons of code in your class to make logging configurable although logging should not be a concern of your business logic class at all.
With dependency injection you don’t new
up an instance of a specific logger class in your business logic class but inject a logger instance from the outside. This way your business logic and the logging logic are decoupled and you can change the logging behaviour without changing the business logic class.