Lucy Rai wrote:Please tell me answer. I am java fresher.
Say we are making a computer game that depicts the earth and all the living and non-living things in it.
When two (or more) things are closely related you can combine all their common functions and properties inside an Abstract class and then derive from it. Example. Bird and Mammal are living beings, they can move(), eat(), breathe(), reproduce(), exist as dimorphs(male/female). We know all this from our knowledge of biology. So, we create an Abstract class Animal with abstract methods like move(), eat() etc and variables like sex = male or female (or transgender???). Then Bird and Mammal define methods in their unique way - bird eats with beak, mammal with mouth etc.
Tomorrow, if we discover a new animal in some jungle, its quite likely that it will be similar to bird and mammal, at least in some ways. To define that new animal, we will simply extend from Abstract animal class and add the add the extra features or modify animal methods accordingly.
Interfaces are used when two things can perform similar functions, but are not very closely related. Example bird and bat can fly. Even airplanes can fly. So, is a bat a bird or vice versa ? No,
bats don't lay eggs and they breastfeed. So we make an interface canFly or flyable or something like that. Bat and
bird implement that interface. When a third person reads the documentation for bat and bird, he sees "implements canFly" and extends Animal. So, he knows immediately that they are animals, but not the same kind. They can fly (and have two feet etc) but a bat is not a bird.
(Hey, potato has skin. I have skin. So am i a potato ?...Monkey has hair, monkey has skin. It can walk, eat and reproduce like me. It looks like and shares a huge percentage of its DNA with me. So, am i a monkey..no i am like a monkey... )
I feel that these may not be perfect examples. But, i thought they could guide you and maybe you can think of a better example.