-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path9.packages_subpackages.java
73 lines (67 loc) · 1.83 KB
/
9.packages_subpackages.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*Question -
Create a class person with datamembers like name,
age, phno with methods getvalues and printvalues, store
the class in a package called pack1. Define a class
named student in subpackage SubPack1 and extend
the class defined as person. Use this class in a program
of different package Pack2, create an object and invoke
the methods defined in pack1 class file.*/
//Code-
//Package1: (Person class)
package pack1;
import java.util.Scanner;
public class person {
String name,age,ph;
public void getValue() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Person Name:");
name = sc.nextLine();
System.out.println("Enter Person Age: ");
age = sc.nextLine();
System.out.println("Enter Phone no of person: ");
ph = sc.nextLine();
}
public void printValue(){
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("Phone number: "+ph);
}
}
//Subpack1:(student class)
package subpack1;
import pack1.*;
import java.util.Scanner;
public class student extends person {
String n,a,roll,ph;
public void get(){
Scanner s=new Scanner(System.in);
System.out.println("Enter Name: ");
n=s.nextLine();
System.out.println("Enter Age: ");
a=s.nextLine();
System.out.println("Enter Roll no: ");
roll=s.nextLine();
System.out.println("Enter Phone no: ");
ph=s.nextLine();
}
public void print(){
System.out.println("Name : "+n);
System.out.println("Age : "+a);
System.out.println("Roll no: "+roll);
System.out.println("Phone no of Student: "+ph);
}
}
//Pack2: (Final class)
package pack2;
import pack1.*;
import subpack1.*;
public class Final {
public static void main(String[] args) {
person o1=new person();
o1.getValue();
o1.printValue();
student o2=new student();
o2.get();
o2.print();
}
}