Saturday, June 13, 2015

RELATIONSHIPS ,Inheritance in Java

12:02 AM


 TYPES of RELATIONSHIPS in java

Based on reusing the data members from one class to another class in JAVA we have three
types of relationships. They are is-a relationship, has-a relationship and uses-a relationship.

• Is-a relationship is one in which data members of one class is obtained into another class
through the concept of inheritance.
• Has-a relationship is one in which an object of one class is created as a data member in
another class.
• Uses-a relationship is one in which a method of one class is using an object of another class.
Inheritance is the technique which allows us to inherit the data members and methods from
base class to derived class.
• Base class is one which always gives its features to derived classes.
• Derived class is one which always takes features from base class.
A Derived class is one which contains some of features of its own plus some of the data
members from base class.

Syntax for INHERITING the features from base class to derived class:

class <clsname-2> extends <clsname-1>
{
Variable declaration;
Method definition;
};

Here, clsname-1 and clsname-2 represents derived class and base class respectively.
Extends is a keyword which is used for inheriting the data members and methods from base class to
the derived class and it also improves functionality of derived class.

NOTE:

• Final classes cannot be inherited.
• If the base class contains private data members then that type of data members will not be
inherited into derived class.
Whenever we develop any inheritance application, it is always recommended to create an object
of bottom most derived class. Since, bottom most derived class contains all the features from its
super classes.
• One class can extend only one class at a time. Since, JAVA does not support multiple
inheritance.
Whenever we inherit the base class members into derived class, when we creates an object of
derived class, JVM always creates the memory space for base class members first and later memory
space will be created for derived class members.

For example:

class c1;
{
int a;
void f1()
{
…………;
}
};
class c2 extends c1
{
int b;
void f2()
{
…………;
}
};
NOTE:
• Whatever the data members are coming from base class to the derived class, the base class
members are logically declared in derived class, the base class methods are logically defined
in derived class.
• Private data members and private methods of the base class will not be inherited at all.

Write a JAVA program computes sum of two numbers using inheritance?
Answer:
class Bc
{
int a;
};
class Dc extends Bc
{
int b;
void set (int x, int y)
{
a=x;
b=y;
}
void sum ()
{
System.out.println ("SUM = "+(a+b));
}
};
class InDemo
{
public static void main (String k [])
{
int n1=Integer.parseInt (k [0]);
int n2=Integer.parseInt (k [1]);
Dc do1=new Dc ();
do1.set (n1, n2);
do1.sum ();
}
};
For every class in JAVA we have a super class called object class. The purpose of object class
is that it provides garbage collector for collecting unreferenced memory locations from the derived
classes.

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Java Tutorials. All rights resevered. Designed by Templateism

Back To Top