in this java tutorial we are learn about inheritance using extend keyword.
in the inheritance an object can acquire the properties the property and behavior of another object.
in java extend keyword is use to indicate a new class is derived from the base class using inheritance.
so basically extend keyword is used to inheritance the class or extent the functionality of that class.
//Program to illustrate single inheritance using extend keyword
import java.util.Scanner;
class student
{
String nm;
int rno,m1,m2,m3;
void get()
{
Scanner s=new Scanner(System.in);
System.out.print("\nEnter name:");
nm=s.next();
System.out.print("\nEnter roll no.:");
rno=s.nextInt();
System.out.print("\nEnter Marks1:");
m1=s.nextInt();//created by kiran gowda
System.out.print("\nEnter Marks2:");
m2=s.nextInt();
System.out.print("\nEnter Marks3:");
m3=s.nextInt();
}
void display()
{
System.out.print("\nName:"+nm); System.out.print("\nRoll No.:"+rno); System.out.print("\nMarks1:"+m1); System.out.print("\nMarks2:"+m2); System.out.print("\nMarks3:"+m3);
}
SUB:-CORE JAVA ROLL NO:-1951012
SYBSC[COMPUTER SCIENCE]
}
class result extends student
{
int avg;
void claculate()
{
avg=(m1+m2+m3)/3;
System.out.print("\nAverage:"+avg);
}
}
class inherit
{
public static void main(String[] args) { result r=new result();
r.get();
r.display();
r.claculate();
}
}
Output :-
Import java.util.Scanner;
class emp
{
String empname;
int empid;
float basicsal;
void get()
{
Scanner s=new Scanner(System.in);
System.out.print("Enter employee name:");
empname=s.next();
System.out.print("Enter emp id:");
empid=s.nextInt();
System.out.print("Enter basic salary:");
basicsal=s.nextInt();
}
void display()
{
System.out.println("Employee name:"+empname);
System.out.println("Employee id:"+empid);
System.out.println("Basic salary:"+basicsal);
}
}
class income extends emp{
float ns,hra,da,ta;
void calculate()
{
hra=(basicsal)*(0.15f);
da=(basicsal)*(0.1f);
ta=(basicsal)*(0.05f);
ns=(hra+da+ta+basicsal);
System.out.println("ns="+ns);
}
SUB:-CORE JAVA ROLL NO:-1951012
SYBSC[COMPUTER SCIENCE]
}
void calculate()
{
hra=(basicsal)*(0.15f);
da=(basicsal)*(0.1f);
ta=(basicsal)*(0.05f);
ns=(hra+da+ta+basicsal);
System.out.println("ns="+ns);
}
Output :-
0 Comments