PREVIOUS
Inheritance
Inheritance is the process of deriving a new class
from a base class/old class. The derived class has a larger set of properties
than its base class.
* The major advantage of inheritance is reusability of code.
* Java supports single,
multilevel, hierachical inheritance only.
* Java supports multiple
inheritances through the interface.
* The extends
keyword is used to inherit a class.
1.
Single inheritance:
AàB
A - Base class / super class
B - Derived class / sub class
EXAMPLE 1:
import java.io.*;
import java.lang.String.*;
class A
{
static
String s,a,c;
int
j,k=0,m=1;
void
display()throws IOException
{
InputStreamReader o=new InputStreamReader(System.in);
BufferedReader b=new BufferedReader(o);
System.out.println();
System.out.println("Enter The First String:");
s=b.readLine();
System.out.println();
System.out.println("Enter The Single Character:");
a=b.readLine();
for(j=0;j<s.length();j++)
{
c=s.substring(j,m);
m++;
if(c.equals(a))
{
k++;
}
}
System.out.println();
System.out.println("No Of Occurence Of "+a+" is
"+k);
}
}
class B extends A
{
static
String s,p;
int i;
void
rep()throws IOException
{
InputStreamReader o=new InputStreamReader(System.in);
BufferedReader b=new BufferedReader(o);
display();
System.out.println();
System.out.println("The Replace String Is:
"+super.s.replace('a','i'));
System.out.println();
System.out.println("Enter The Second String:");
this.s=b.readLine();
if((this.s).equals(super.s))
{
System.out.println();
System.out.println("The
Strings Are Equal");
}
else
{
System.out.println();
System.out.println("The Strings Are Not Equal");
}
if(this.s.toUpperCase()=="true")
{
System.out.println("Conversion Of String:"+this.s.toLowerCase());
}
else
{
System.out.println("Conversion Of
String:"+this.s.toUpperCase());
}
}
}
class inherit
{
public
static void main(String args[])throws IOException
{
InputStreamReader o=new InputStreamReader(System.in);
BufferedReader b=new BufferedReader(o);
String l;
B obj=new
B();
obj.rep();
A obj1=new
A();
System.out.println();
System.out.println("The Length Of First
String:"+obj1.s.length());
System.out.println("The Length Of Second String:"+obj.s.length());
}
}
/* Single Inheritance */
class A
{
int a,b;
void dis()
{
System.out.println("A class"+(a+b));
}
}
class B extends A
{
int m;
void list()
{
dis();
System.out.println("use a and
b"+(a+b+m));
}
}
class in
{
public static void main(String ar[])
{
B obj=new B( );
obj.a=10;
obj.b=20;
obj.m=30;
obj.list();
}
}
2. Multilevel
Inheritance:
A new class inherits from another
derived class
AàBàC
A - Base class
B - Intermediate
class
C - Sub class
class x
{
int a=10;
void disp()
{
System.out.println("this is class x");
System.out.println(" a :"+a);
}
}
class y extends x
{
int b=20;
void disp1()
{
System.out.println("this is class y");
System.out.println(" a+b :"+(a+b));
}
}
class z extends y
{
int c=30;
void disp2()
{
System.out.println("this is class z");
System.out.println(" a+b+c :"+(a+b+c));
}
}
class multilevel
{
public static void main(String ar[])
{
z z1=new z();
z1.disp();
z1.disp1();
z1.disp2();
}
}
3.
hierarchical Inheritance:
A
__________
B, C, D
A – One
base class
More than one derived class and single base class.
import java.io.*;
class first
{
int a,b;
void input()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("enter a,b val");
a=Integer.parseInt(d.readLine());
b=Integer.parseInt(d.readLine());
}
}
class sec extends first
{
void dis()
{
System.out.println("the method for derived
class");
System.out.println(" a%2 :"+(a%2));
System.out.println("a*b :"+(a*b));
}
}
class thi extends first
{
void disp()
{
System.out.println("the value of a+b : " +(a+b));
}
}
class hiera
{
public static void main(String ar[])throws
IOException
{
sec s=new sec();
s.input();
s.dis();
thi t=new thi();
t.input();
t.disp();
}
}
No comments:
Post a Comment