Answer Posted / prema g.v
Inheritance is a object oriented concept where in the subclass acquires all the properties of superclass.
The main use of inheritance are,
1. Code reuse.
2. Code enhancement- built on what you have, without altering the present functionality.
For example: Consider the Class animal,
class animal {
String name;
int size;
public void sleep() {
System.out.println("Animal is sleeping");
}
public void eat() {
System.out.println("animal is eating");
}
}
In this example the class animal has a state name and size and behaviors sleep(), eat().
Now I will create a subclass dog,
class dog extends animal {
int legs;
public void bark() {
System.out.println("Dog barking");
}
}
class cat extends animal {
int legs;
public void make_sound() {
System.out.println("cat making sound miyo miyo");
}
}
Here extends key word indicates inheritance.
The subclasses dog and cat now acquires the properties of class animal i,e the class dog and animal has got state size, name and behaviors sleep(), eat() along with their
own state(instance variable) and behavior(method).
Now if we want to invoke sleep() method in the class dog
and animal, we can directly invoke by creating dog and cat
class objects. It means you need not write functionality of
sleep() method in dog and class. We are re-using the code
of animal class sleep() method by using inheritance concept.
Suppose if we did not have the concept of inheritance, we
need to write sleep() method functionality in cat and dog
class as well, which is something not good.
Suppose if we want dog to sleep differently than animal,
we can also provide our own implementation just by
overriding sleep() method in dog class i,e we can enhance the code of sleep() method in dog class. To override methods
in subclass we should maintain same signature as that of
super class.
i,e in dog class,
public void sleep() {
System.out.println("Dog sleeping with its eyes closed");
System.out.println("dog is sleeping");
}
Here just we are building on what we have, nothing on enhancement.
When ever we encounter IS-A relationship, then its shows inheritance. Example Cat is an animal. Lilly is a flower.
Super classes indicates generalized behavior. Subclasses indicates specialized behavior.
i.e the above example indicates all animal will sleep and
eat- indicates generalized behavior. But only dark can
bark, this behavior is only related to dog.
All generalized behavior we can put it in superclass and
can use it in subclasses -again reuse of code.
Is This Answer Correct ? | 5 Yes | 1 No |
Post New Answer View All Answers
What is the use of runnable interface?
How many types of operators are there?
How can we make sure main() is the last thread to finish in java program?
How many bits is a 64 bit byte?
Can we override the static method?
What is a void in java?
What is the final keyword in java?
Do you know how to reverse string in java?
Which package has light weight components in java programming?
4.1 Supply contracts (in the form of comments specifying pre- and post conditions) for the enqueue() method of the LinkedQueue class given in the Appendix. (2) 4.2 Let Thing be a class which is capable of cloning objects, and consider the code fragment: Thing thing1 = new Thing(); //(1) Thing thing2 = thing1; //(2) Thing thing3 = (Thing) thing1.clone(); //(3) Explain how the objects thing2 and thing3 differ from each other after execution of the statements. (
How destructors are defined in java?
Why inputstreamreader is used in java?
How a variable is stored in memory?
What is equals method in java?
What is the difference between a constructor and a method?