MainMenu

Home Java Overview Maven Tutorials

Sunday, 15 January 2017

Inheritance in java with example



Inheritance in java with example

For Video : CLICK HERE



As like Overloading, Inheritance is also a OOPs feature, Inheritance provide a mechanism that allow a class to inherit property of another class.



When a class inherit another class using extends keywords then all non-private members including fields and Methods.
In java inheritance cab be described in parent, child relationship also known as Super Class(Parent) and Sub class(child) in java language.

Syntax:
Class Parent
{
..............
}
Class Child extends Parent
{
...............
}
Example : In below example parent class has a method witch will be inherited by child and then object of class child will access the methods
package chandan_test1;
class C_parent
{
int x;
public void sum(int a, int b)
{
x = a+b;
System.out.println("Your sum is :" +x);
}
}
public class C_inhritance extends C_parent
{
public void sub(int c, int d)
{
x = d-c;
System.out.println("Your Difference is :" +x);
}
public static void main(String args[])
{
int a = 10;
int b = 20;
C_inhritance alpha = new C_inhritance();
alpha.sum(a,b);
alpha.sub(a,b);
}
}
Output:
Your sum is : 30
Your Difference is : 10

Tags :What is inheritance in Java?,java inheritance with example



9 comments:

  1. The online selenium training course is becoming very popular and its popularity will continue to grow. The point that every sphere of work and business is now being digitalized, the demands of such training continue to soar. Online Advanced JAVA Training

    ReplyDelete
  2. It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. xenforo.asia

    ReplyDelete
  3. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. [REVEALED] THE BEST FREE PRODUCT RESEARCH SPY TOOL 2019 | FACEBOOK MARKETING & SHOPIFY RESEARCH

    ReplyDelete
  4. Computer tutorials that are created using screen capture software are often extremely beneficial to people because they show a working version of the system and users can relate what the video is showing, to what they are physically seeing. click here for how to guides

    ReplyDelete
  5. I don’t think many of websites provide this type of information. yabo

    ReplyDelete
  6. Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates. online

    ReplyDelete
  7. I really love your write-ups web design agency guys continue the good work.

    ReplyDelete