When I switched from c++ to java I had this same feeling but now that I been working with java for a while it all kinda makes sense 1. What I thought about inheritance is having some methods in parent class, and whenever the same methods are needed in other class(es) too, then those class(es) will inherit the parent class and use it Like the original author did, you can still do multiple inheritance in java you just must use interfaces. Interfaces are like pure virtual classes in c But in interface concept if each derived class(es) has to define its own implementation then what is the use of inheriting it?
The reason you implement an interface in java is so that you guarantee that class has those methods. That way you can have a specific class implement a generic interface and then treat every specific class that implements that generic interface the same Java Design is a bit different then c++ design but after doing several java program's you will become just as good at using multiple interfaces as you are at using multiple inheritance.
When I switched from c++ to java I had this same feeling but now that I been working with java for a while it all kinda makes sense. 1. What I thought about inheritance is having some methods in parent class, and whenever the same methods are needed in other class(es) too, then those class(es) will inherit the parent class and use it.
Like the original author did, you can still do multiple inheritance in java you just must use interfaces. Interfaces are like pure virtual classes in c++. But in interface concept if each derived class(es) has to define its own implementation then what is the use of inheriting it?
The reason you implement an interface in java is so that you guarantee that class has those methods. That way you can have a specific class implement a generic interface and then treat every specific class that implements that generic interface the same. Java Design is a bit different then c++ design but after doing several java program's you will become just as good at using multiple interfaces as you are at using multiple inheritance.
I was just about to press the answer button but you answered just a second before me, you had a little bit better explanation than me anyway. Good answer. – gsfd Jul 18 at 19:28 @Grammin being a beginner I cannot grasp your explanation about having the own definitions for the inherited methods.My doubt is why not we define that method in the derived class(es) itself.
Why to bring interface concepts here. Can you explain it again. Sorry for asking it again.
– EAGER_STUDENT Jul 18 at 19:43 @EAGER_STUDENT well one of the main reasons was to avoid naming conflicts that arise from having two inherited classes with the same function name. – Grammin Jul 18 at 19:49.
Each subclass has to define it's own implementation because each subclass may perform the operation slightly differently. Consider the following example: public interface animal { //All implementers must define this method void speak(); } This interface states that any Animal MUST have a way to speak. Basically, any type of animal is going to be able to make a noise.
We then have 2 subclass, or 2 different types of animals that we create. Public class Dog implements animal { //Define how a Dog speaks public void speak() { System.out. Println( "woof" ); } } We then define another animal, cat public class Cat implements animal { //Define how a Cat speaks public void speak() { System.out.
Println( "meow" ); } } In this example, both Cat and Dog are animals, and therefore must be able to speak due to our interface. However, everybody knows that cats and dogs make different sounds. By allowing each subclass to define how it 'speaks', we can give Dog and Cat their own respective sound when the speak() method is called, while ensuring they are both Animals.In answer to your question more specifically, inheritance forces it's subclasses to have a specific method.
In other words, an interface states that "all my subclasses will define each of these methods". What this allows us to do is to write code that deals with the methods in an interface without knowing the specific subclass. We can safely do that because we know that each subclass MUST have defined the method in the interface class.
If only the subclasses that use the method defined it, then we would have no way of knowing for sure whether it is safe to call the method on all subclasses. Just a note: If you do not want a subclass to define the method, you can simply define an empty method like this: public class MuteAnimal implements animal { //A MuteAnimal can't speak! Public void speak() { } }.
Public class Dog extends animal" here animal is a interface so "public class Dog implements animal" could come na. – EAGER_STUDENT Jul 18 at 19:34 Yeah I realized I accidentally used extends instead of implements. I've since fixed it in my answer because you must implement an interface – Sum Deos Jul 18 at 19:38 I am still not cleared with my 2nd question.
Could you explain it. – EAGER_STUDENT Jul 18 at 19:50 You could have each subclass define the methods themselves if you wanted too. The reason an interface is useful, however, is that it forces every single subclass to define the same methods.
Let's say you want to iterate over an array of Animals and call currAnimal.speak(). If Animal is an interface that forces every subclass to define a speak() method, you are guaranteeing that your call will be legal. Otherwise, someone implementing your Animal class(and not necessarily the author of Animal) may not define speak(), and so all of a sudden your iterator is throwing missingMethod exceptions.
– Sum Deos Jul 18 at 19:59 yeah! Thanks a lot. I got it now after reading your answer again.To be honest the mistake is with my side.
Your answer is absolutely clear. But I read with more concentration only this time. – EAGER_STUDENT Jul 18 at 20:17.
Inheritance is often useless without polymorphism. It is really not easy to explain it all just in few sentences. My advices would be to look at interfaces for defining behavior (something like can-do relationship), and concrete inheritence for is-a relationships.In the center of everything as you may learn is something called Single Responsibility Principle.
This means that one class has one responsibility, if you are having more of them, you separate the class. If you take your example, even the Pegasus isn't both horse and bird at the same time 100% percent.It would inherit the horse, but implement specific characteristics of the birds, which would be defined in interfaces, like Flyable for instance. You can say that birds have one way of flying common to them all, so inherit them from Bird.
Pegasus is a little different, so that custom logic can be defined after you implement the Flyable interface with method Fly. Also, the example with horseNoise and birdNoise is little unrealistic, you want one method speak() which will due to internal class alhorithm perform certain action. What if that pegasus could talk?
Would you have a method for each word? Back to Flyable example, say you now have a video-game. Now you can have polimorphism for this: Lets say that in game earthquake happens.
You want for each animal that can fly to go and fly. You have a collection of animals currently in game, so you write this: foreach(Flyable flyableAnimal in animals) flyableAnimal.Fly(); You just rely on polimorphism ... These were just some random thoughts, you can find far better examples online, hope this helps ...
Thanks for both answering and editing my question – EAGER_STUDENT Jul 18 at 19:38.
If class A inherits from class B, that effectively means two things: Class A can implicitly use all methods and properties of class B, and need only define that functionality which is in fact unique to class A. Code which expects an object of type B will accept an object of type A. Those two features of inheritance are in some sense orthogonal; one can imagine places where either could be useful without the other.
Although derived classes can only have one parent class from which they gain implicit access to methods and properties, they may define an arbitrary number of interfaces for which they are substitutable. Note that while some people insist that interfaces form a "has-a" rather than "is-a" relationship, I think it's better to think of them as saying something "is __able" or "is a __er", the point being that interfaces don't just define abilities, but define substitutability (i.e. "is a") in terms of ability.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.