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

do I need to use synchronized on setvalue(int)? : Java thread

785


Can we override constructor?

808


Give any two differences between C++ and java.

858


When should I use singleton pattern?

824


What is a variable in java?

764


How to create an immutable class?

799


Explain the difference between private, public, package and protected in java?

833


Is vector synchronized in java?

757


What is mutable object and immutable object?

918


What is use of static method?

791


What are static variables and functions?

865


Can we have multiple public classes in a java source file?

811


What is a vararg?

770


What is http client in java?

791


How can we use primitive data types as objects?

763