Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Java Inheritance and packages

Java Program to display Student Marks information using Single Inheritance.
import java.io.*;
class Student 
{
int rollno;
String nm;
}
class Marks extends Student
{
int p, c, m, tot;
Marks()
{
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter Roll No:");
rollno=Integer.parseInt(in.readLine());

System.out.print("Enter Name:");
nm=in.readLine();

System.out.print("Enter Phy Marks:");
p=Integer.parseInt(in.readLine());

System.out.print("Enter Che Marks:");
c=Integer.parseInt(in.readLine());

System.out.print("Enter Maths Marks:");
m=Integer.parseInt(in.readLine());
tot=p+c+m;
}
catch(IOException e) {System.out.println("Error in input"); System.exit(1);}
}
void show()
{
System.out.println("RollNo : " + rollno);
System.out.println("Name : " + nm);
System.out.println("Physics : " + p);
System.out.println("Chemistry : " + c);
System.out.println("Maths : " + m);
System.out.println("Total : " + tot);
}
}
class Inherit
{
public static void main(String args[])
{
Marks sm=new Marks();
sm.show();
}
}


Java program to display Employee Salary information using Inheritance.
class Emp_det
{
int eno=100;
String nm="Ravi";
}

class Salary extends  Emp_det
{
int bs=34567, pf=500;
int net=bs-pf;

void show()
{
System.out.println("Employee Number:" +eno);
System.out.println("Employee Name:" +nm);
System.out.println("Employee Basic Pay:" +bs);
System.out.println("Employee PF:" +pf);
System.out.println("Employee Net Pay:" +net);
}
}
public class Inherit_emp
{
public static void main(String args[])
{
Salary s=new Salary();
s.show();
}
}

Java Program to show Multiple Inheritance using Java Interface
import java.lang.*;
import java.io.*;

interface Exam
{
void percent_cal();
}
class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}

void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+roll_no);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}

public void percent_cal()
{
int total=(mark1+mark2);
float percent=total/2;
System.out.println ("Percentage: "+percent+"%");
}
    
void display()
{
super.display();
}
}
class Mi
{
public static void main(String args[])
{
Result R = new Result("John",01,90,84);
R.display();
R.percent_cal();
}
}

Java Program to show Multi Level Inheritance.
class A 
{ 
void DisplayA() 
{ 
System.out.println("Class A"); 
} 
} 

class B extends A 
{ 
void DisplayB() 
{ 
System.out.println("Class B"); 
} 
} 

class C extends B 
{ 
void DisplayC() 
{ 
System.out.println("Class C"); 
} 
} 

class MLevel 
{ 
public static void main(String args[]) 
{ 
C c = new C(); 
c.DisplayA(); 
c.DisplayB(); 
c.DisplayC(); 
} 
} 

Java Packages 

Java Packages enable grouping of functionally related classes
Package names are dot separated, e.g., java.lang.
Package names have a correspondence with the directory structure.
Packages Avoid name space collision. 
There can not be two classes with same name in  a same Package But two packages can have a class with same name.


To Create user defined Packages in Java, follow the steps.
1. Create a folder named eg: mypack in your current working directory.
2. Create a file called "Welcome.java" in mypack folder only with following code in it.

package mypack;
public class Welcome
{
public void show()
{
System.out.println("Welcome to user defined packages!");
}
}

 3. Compile the above program and let it create the .class file (something like Welcome.class) but don't run this program! I repeat only compile it.

4. Now, please come out from the directory or folder or package folder "mypack" and stay in your normal java or javaprograms directory, now create an another java file, namely, eg:pack.java

5. Enter the following code into "pack.java"
import mypack.Welcome;
class pack
{
public static void main(String args[])
{
Welcome w=new Welcome();
w.show();
}
}

6. Now, compile and run the above program, you can see the following line of output message which was actually written  in the package called "Welcome.java".
Welcome to user defined packages!


<< == Java Exceptions, Method Over loading, Method Overriding examples





No comments:

Post a Comment