What is meant by inheritance ? Explain with an example

Showing Answers 1 - 10 of 10 Answers

swapna

  • Sep 28th, 2005
 

inheritance is an OOPS concept by which a class can acquire the characteristics of another class.

The class from which its inheriting is called base class  and the class which is inheriting is called sub class.

class demo1

{

}

class demo2 extends demo1

{

}

Amit

  • Aug 18th, 2011
 

Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to
it. In the terminology of Java, a class that is inherited is called a superclass.

The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a
superclass. It inherits all of the instance variables and methods defined by the superclass and adds its own, unique elements.

Code
  1. /* Single Inhetitance To Find Area Of Rectangle */

  2. class Dimensions

  3. {

  4. int length;

  5. int breadth;

  6. }

  7.  

  8. class Rectangle extends Dimensions

  9. {

  10. int a;

  11. void area()

  12. {

  13. a = length * breadth;

  14. }

  15.  

  16. }

  17.  

  18. class Area

  19. {

  20. public static void main(String args[])

  21. {

  22. Rectangle Rect = new Rectangle();

  23. Rect.length = 7;

  24. Rect.breadth = 16;

  25. Rect.area();

  26. System.out.println("The Area of rectangle of length "

  27. +Rect.length+" and breadth "+Rect.breadth+" is "+Rect.a);

  28. }

  29. }

  Was this answer useful?  Yes

Inheritance is acquiring properties of one class from another class, the class which is inherited is called base class and the class which acquir the properties is called derived class.
eg:

Code
  1.  

  2. class rectangle //base class

  3. {

  4. }

  5. class rectangle  extends triangle //derived class

  6. {

  7. }

  Was this answer useful?  Yes

Shahnoor Alam

  • Aug 28th, 2017
 

Inheritance is an OOPS concept by which a class can acquire the characteristics of another class. The class that does the inheriting is called a subclass. Therefore, a subclass is a specialized version of a superclass.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions