Describe inheritance as applied to java?

Answer Posted / shivaprasad

Inheritance is a form of software re usability in which new
classes are created from existing classes by absorbing their
attributes and behavior using the key word 'extends'.
The class which absorbs the properties is called 'sub class'
and the class which is used is called 'super class'.
Ex:
class Room
{
void roomAttributes()
{
int roomlength;
int roomheight;
int roombreadth;
String roomname;
}
void roomBehavior()
{
System.out.println("Two windows");
System.out.println("One 6.2*3.5 door");
}
}
public class BedRoom
{
public static void main(String[] args)
{
Room roomobj=new Room();
roomAttributes();
roomBehavior();
int area= room.length*room.breadth*room.height;
}
}
In the above example the room is a super class from which
bedroom(subclass) is inheriting the properties and
attributes. This is simple inheritence.
Similarly, multi level inheritence is also possible.

Ex: class A extends B
class B extends C
class C extends D

But, java does not support multiple inheritance.
Ex: class A extends B, c

For better reference : Deitei & Deitel or Balaguruswamy

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Print Vertical traversal of a Binary Tree.

804


Is node a data type in java?

735


How can we create objects if we make the constructor private ?

752


What is ternary operator? Give an example.

815


What is a variable and constant?

777


Can you start a thread twice in Java?

858


What is a finally block?

766


In a program, initializing an array of 100 KB is throwing an out of memory exception while there is 100 MB of memory available. Why?

783


What are the two main uses of volatile in Java?

814


Can It is possible to synchronize the constructor of a Java Class?

828


Give an example of use of pointers in java class.

769


How destructors are defined in java?

901


Can we declare an array without size in java?

727


How do I type unicode?

736


What is a class reference?

777