4 '+("' )* "78/4'9"+ %BWH'"0':;"1'(<""='1'(9I2='&,B6+78/6D2"DH'6F9">'?'.'@' !5$ 1'(C%$"!:(<1(09"D,@/$H'6 W%B H'"0'&H@"94nH :;"1'(C%$"!:(<1(0>'?'.'@'!5$9I2 4#,11'(IA61(+@"1'( "7/8 4'9"+ "%8.:;"1'(<""='1'(C%$"!:(<1(0>'?'.'@'IA6 //:.K !5$<""='1'(9I20L/5 1'(9I2 X X 1'(C%$"!:(<1(0!5$9I23#,1? 5H"C/6!:(<1(0IA6//:.K/' AIH"1'(4H/43201'(&7+ /5<#1'(0%G524#'$(M:<++1'( 9I2#'&<++abstract<#1'(9I2/A"D/(EJ&:;"D2"
4 G0 0L/5:;"&0'IA1C/6#'&-7B/(+3 @AL%1'(4(7/-oDA1((0 %B//:.KC/6#'& &'0'(*1( ='G52 0L/5 ='4"2' %B9"1'(&H6CH'@&'((4@H'6//:.KRSB6:(%$+ $% +G521,+1'( 9I2 X4(7/ XV %B/$MH9"!:(<1(0IA61(+@"1'(D,@/$H'6!:(<1(09"+ %B WH'"0'.C%$" 31='&,6B /$MH>'$9"0L/5 main() 1'(C%$"!:(<1(0IA6//:.K %B5.% D2/6 0%1'(1='4"50L/5DH'6F9421,+#'& <#0%1'(&(2'6//:.KC/6#'& <#
[email protected].(%$19I2 0L/5 &%B (2'6CS8"D,@/$H'6IH" !:(<1(0 %B
QP :;"!:(<1(0 %B0%1'(C%$"='&,6B ,86405/$MH9" 0L/5 main() !:(<1(0"%8G0HG529I24#,11'(IA6//:.K<#G0H40' .%B "='G:9I29" '6 :ZA+,DA 07B/!:(<1(00%C"'594nHCS8" ('&'0'(*C%$"!:(<1(0 %B
QP 940H!5$9I24#,11'(IA6//:.K5,6!:(<1(0 %B
Q !5$1='4"59420%#'&&/6#'& #'&<(1I7B/ NumericalClass .0%0L/5 calculate() RSB6.0%='&,B6DH'6F %B/$M9H "+#K/1C/60L/5 main() C/6!:(<1(0 %B
QP &H@"/%1#'&I7B/MyMain:;"#'& %B1='4"5-7B/:;".35(AB0D2"C/6!:(<1(0!5$0% 0L/5 main() /$MH9"#'& RS6B 0L/5 main().&(2'6//:.KC/6#'& NumericalClass <#0%='&,B6(%$19I20L/5 calculate() 9"C5%$@1,"('1K&'0'(* .%B C%$"!:(<1(0 !5$9I24#,11'(IA6//:.K!5$1='4"50L/5 main()942/$MH>'$9"#'&5%$@1,"1,+#'& %B D2/61'(&(2'6//:.KG52!5$.0%(M:<++5,6!:(<1(0 %B
Qc
("' .! 1'(C%$"='&,B6 ,864059"0L/5main()
public class AllInMain { public static void main(String args[]) { double score = Math.random()*100; if (score >= 80) { System.out.println("Grade is A"); } else if (score >= 70) { System.out.println("Grade is B"); } else if (score >= 60){ System.out.println("Grade is C"); } else if (score >= 50){ System.out.println("Grade is D"); } else { System.out.println("Grade is F"); } } }
("' .!$1'(C%$"!:(<1(0!5$9I24#,11'(IA6//:.K class NumericalClass { public public void calculate() { double score = Math.random()*100; if (score >= 80) { System.out.println("Grade is A"); } else if (score >= 70) { System.out.println("Grade is B"); } else if (score >= 60){ System.out.println("Grade is C"); } else if (score >= 50){ System.out.println("Grade is D"); } else { System.out.println("Grade is F"); } } } public class MyMain { public static void main(String args[]) { NumericalClass obj = new NumericalClass(); obj.calculate(); } }
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .!%1'(C%$"!:(<1(0!5$942 310L/5/$MH>'$9"#'&5%$@1," class NumericalClassV1 { public void calculate() { public double score = Math.random()*100; if (score >= 80) { System.out.println("Grade is A"); } else if (score >= 70) { System.out.println("Grade is B"); } else if (score >= 60){ System.out.println("Grade is C"); } else if (score >= 50){ System.out.println("Grade is D"); } else { System.out.println("Grade is F"); } } public static void main(String args[]) { NumericalClassV1 obj = new NumericalClassV1(); obj.calculate(); } }
1'(C%$"!:(<1(0D'0D,@/$H'6C/6!:(<1(0 %B
Q 4(7/ !:(<1(0 %B
Qc :;"1'( <+H6!:(<1(0//1:;"!05M#$H/$F ='942&'0'(*:(,+:#%B$"<12GC!:(<1(0G526H'$CS8" <#:;" 1'(9I24#,11'(IA6//:.KRSB6&'0'(* %B.9I2#,1?5H"DH'6FG52IH"4#,11'(C/61'(4H/4320 1'(&7+ /5<#1'(0%G524#'$(M:<++
4 '+'D) G0 0L/5 %B1='4"5CS8"9"#'&95F&'0'(*(%$19I26'"G52&/6(M:<++7/ PQ 1'((%$19I26'".'1#'& %BDH'61," Q 1'((%$19I26'">'$9"#'&5%$@1," 1'((%$19I20L/5.'1#'& %BD'H 61," .D2/60%1'(&(2'6//:.KC/6#'& %B0% 0L/5 %B.*M1(%$19I26'"1H/" .S6.&'0'(*(%$19I20L/5G52 5,6D,@/$H'69"!:(<1(0 %B
Q #'& MyMain.D2/6&(2'6//:.KC/6#'& NumericalClass1H/".S6.(%$19I26'"0L/5 calculate() G52!5$9I2='&,B6 NumericalClass obj = new NemericalClass();
obj.calculate();
86, "%80L/5 %B.*M1(%$19I26'".'1#'& %BDH'61," .D2/6G0H0% V :;" private 1'((%$19I20L/5>'$9"#'&5%$@1,"&'0'(* ='G52!5$G0H.=':;"D2/6&(2'6//:.K C/6#'&CS8"0'1H/" <#&'0'(*(%$10L/5G52 310L/5 $1@2"0L/5 0%B % V :;" static .G0H&'0'(*(%$19I20L/5 %BG0H0% V :;" static (%$1@H' 0L/5<++ qXG52RSB6.1#H'@*S6>'$4#,6 !:(<1(0 %B
Qu <&56D,@/$H'61'((%$19I20L/5>'$9"#'&5%$@1," !:(<1(0"%8 1='4"50L/59"#'&NumericalClassCS8"0'940H %BI7B/callMethod()<#2@(%$19I2 0L/5calculate()>'$9"0L/5 %B1='4"5CS8"940H ("' .!,1'((%$19I20L/5>'$9"#'&5%$@1," public class NumericalClassV2 { public void calculate() { double score = Math.random()*100; if (score >= 80) { System.out.println("Grade is A"); } else if (score >= 70) { System.out.println("Grade is B"); } else if (score >= 60){ System.out.println("Grade is C"); } else if (score >= 50){ System.out.println("Grade is D"); } else { System.out.println("Grade is F"); } } public void callMethod() { calculate(); } }
4 '1g1>BeDLJIC 0L/5 %B1='4"5CS8"9"#'&/'.0% %B(,+H'-7B/"='G:9I29"0L/5/' A IH" 0L/5 setGPA()9"#'& StudentC/6!:(<1(0 %B
Qt .0% &='4(,+(,+H'
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
C2/0M# 0%B %I"A5C2/0M#:;" double9"1(%"%81'((%$19I20L/5.D2/6&H6 %B0I% "A5 C2/0M# &%B /5#2/61,"G:-(2/01," 5,6",8"1'((%$19I20L/5 setGPA() .D2/6&H6 %B0I% "A5C2/0M#:;" doubleG:-(2/01,+I7B/0L/55,6<&569"#'& MyMain29"!:(<1(0 %B
Qt ='&,B6s1.setGPA(3.0); ("' .!-D,@/$H'61'(&H6WH'" class Student { public String id; String name; double gpa; public void setGPA(double GPA) { gpa = GPA; } public double getGPA() { return gpa; } } ------------------------------------------------------public class MyMain2 { public static void main(String args[]) { Student s1 = new Student(); s1.setGPA(3.0); } } C/60L/5.0%I"A5C2/0M#:;"&/6<++D'0I"A5C2/0M#C/6D,@<:(5,6"%8 PQ %B0I% "A5C2/0M#<++-78"l'" Q %B0I% "A5C2/0M#<++/2'6/A6 9"1(%C/6 %B0I% "A5C2/0M#<++-78"l'" ('&'0'(* %B.&H6H'6 C%B 2/0M# D,@<:( 4(7/"A-."E9421,+ G52/' AIH" 1'((%$19I20L/5 setGPA() ",8" %B.&H6WH'"/'.0%(M:<++:;" x H'6 %BC2/0M# IH" s1.setGPA(3.0);
x D,@<:(IH" double
x = 3.0; s1.setGPA(x);
x "A-."E IH" s1.setGPA(3.0+0.05); &H@"1(% %B0I% "A5C2/0M#<++/2'6/A6('.D2/6&H6//:.K 0%B %I"A5C2/0M# %B &/5#2/6G: H'",8" $1@2"1(% %B ",8"0%I"A5C2/0M#:;" String RSB69"1(%"%8. &'0'(*&H6C2/0M#H'6 %BG52 !:(<1(0 %B
Q
:;"D,@/$H'6C/6#'& StudentV1 %B0% 3#,1? dobRSB60%I"A5C2/0M#:;"#'& Date %B1='4"5G@29"NO-7B/1K+@," 57/":r1A5C/6//:.KI"A5 StudentV1 #'& StudentV1 0%0L/5 setDOB()-7B/ 9I29"1'(1='4"5H'9421,+3#,1?dob ("' .!.D,@/$H'61'(&H6WH'" %B:;"//:.K import java.util.*; public class StudentV1 { String id; String name; Date dob; public void setDOB(Date d) { dob = d; } public Date getDOB() { return dob; } }
!:(<1(0 %B
Qv <&561'((%$19I20L/55,61#H'@!5$D2/6&H6WH'"//:.KC/6#'& Date9421,+0L/55,6"%8 Date d1 = new Date(16, 12, 1980); s1.setDOB(d1);
&='4(,+1(%C/6!:(<1(0 %B
Qv "7B/6.'1//:.K d1 %B&(2'6CS8"0' .G0H0%1'( /2'6/A6>'$9"!:(<1(0"%8/%1 5,6",8"('&'0'(* .%B &(2'6//:.K<#&H6WH'"G:$,6 9"='&,6B 5%$@1,"G525,6"%8 s1.setDOB(new Date (16, 12, 1980));
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .!3D,@/$H'61'((%$19I20L/5 %B0% :;"//:.K import java.util.*; public class TestStudentV1 { public static void main(String args[]) { StudentV1 s1 = new StudentV1(); Date d1 = new Date(16,12,1980); s1.setDOB(d1); } }
I"A5C2/0M#C/6 %B.&H6WH'"G:$,60L/5G0H.=':;" .%B D2/6:;"I"A5C2/0M# 5%$@1,"
.:;"='&,6B 9"1'((%$19I20L/5 %BWA5
("' .!4D,@/$H'61'(&H6WH'" .='"@"&/6D,@ class NumericalSample { public public void calMax(int i, double d) { if (i > d) { System.out.println("Max = "+i); } else { System.out.println("Max = "+d); } } public static void main(String args[]) { NumericalSample obj = new NumericalSample(); obj.calMax(3,4.0); } } 9"1(% %B 0%I"A5C2/0M#:;"<++-78"l'"<#0%1'(&H6 !5$9I2 D,@<:(9421,+0L/5 %B*M1(%$19I26'" !:(<1(0>'?'.'@'.*7/@H':;"1'(&H6H'G:942 H'",8" 4'10%1'(:#%B$"<:#6H'C/6 >'$9"0L/5 %B*M1(%$19I26'" .G0H0%W# ='942H' C/6 %B&H6G::#%B$"H'G:52@$ !:(<1(0 %B
Qs <&56D,@/$H'61'((%$19I20L/5 methodP()9"#'& ArgumentPassingRSB6(,+ %B:;"D,@<:( xC2'0'RS6B 07B/ x9"0L/5:#%B$"H':;"c.G0H0%W# ='942H'C/6 x9"0L/5 main():#%$B "G: 52@$.S6G52W##,-LE//10':;"uIH"5A0/"SB6D,@<:( x %B/$MH9"0L/5 main()<#0L/5 method1():;"D,@<:("#D,@1," <#0%D='<4"H6/2'6/A6&='4(,+1K+C2/0M#9"4"H@$@'0.=' %BDH'61,"-%$6
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .!5D,@/$H'61'(:#%B$"<:#6H'C/6 java.util.*; import
public class ArgumentPassing { public void method1(int x) { x = 3; } public void method2(Date d) { d.setDate(1); d.setMonth(1); d.setYear(2002); } public int method3() { return 0; } public static void main(String args[]) { int x = 4; ArgumentPassing obj = new ArgumentPassing(); Date d1 = new Date(16,12,1980); obj.method1(x); System.out.println("x = "+x); obj.method2(d1); System.out.println("Date = "+d1); obj.method3(); } }
4" ' 1 1'# 0L/595FC/6#'&&'0'(* .%B 0%H' &%B H61#,+0'G52 ,86"%81'(:(1')0L/5.D2/6 (+3I"A5C2/0M#C/6H' %B.&H61#,+9" RSB6I"A5C2/0M#C/6H' .%B &H61#,+/'.:;" I"A5C2/0M#<++-78"l'"4(7/:;"I"A5C2/0M#<++/2'6/A69"1(% %B0L/5G0H0C% 2/0M#.&H61#,+0' .D2/6(+3942 :;" void/' A IH"0L/5 setGPA()9"#'& student C/6!:(<1(0 %B
Qt "/1.'1"%8 0L/5 0%B %H' .%B &H61#,+0'.D2/60%='&,B6 return RSB6.(+3 H' %B&H61#,+!5$0%(M:<++5,6"%8
return value;
!5$ %B value CS8"/$MH1,+I"A5C2/0M# %B.&H61#,+ *2':;"I"A5C2/0M#<++-78"l'"/'. :;"H'6 %BC2/0M#D,@<:(4(7/"A-."E1KG52
H'",8"$1@2"I"A5C2/0M#String %B/'.:;"C2/0M#H'6 %BG52 ='&,B6 return .0%W#942!:(<1(0&A8"&351'( ='6'"9"+#K/1C/60L/5",8"<#2@ 1#,+G:$,6='&,B65A0 %B (%$19I20L/5",8" !5$ @,B G:1'((%$19I20L/5 0%B 1% '(&H6H'1#,+0'",8" .D2/6(%$19I20L/5",8"9" "A-."E/' AIH"1'((%$19I20L/5getGPA()C/6#'&Student9"!:(<1(0 %B
Qt/'. ='G525,6"%8 double d = s1.getGPA(); 4(7/ System.out.println("GPA: "+ s1.getGPA()); ,86&/6='&,6B :;"1'((%$19I20L/59""A-."E
4& LNWAPAJB G0 0L/5C/6#'&.0% V %B:(1')G@24"2' C/60L/55,6 %B(+3 G@29"4,@C2/ %BuQQcRSB6V C/60L/5<+H6//1:;" x XXV -7B/(+3*S6(5,+1'(C2'*S6 x
static-7B/(+3@H':;"0L/5<++static
x
abstract-7B/(+3@H'0L/5$,6G0H&0+M(E
x
synchronized
x
final-7B/(+3@H'0L/5"%8G0H&'0'(* VG52
-7B/(+3@H'0L/5.0%1'(#K/1//:.K RSB69I29"1(%C/6 !:(<1(0 %B :;"<++L(5 V
9WWZ^^uXYVcVZb V :(> "%89I2-7B/(+3(5,+1'(C2'*S6!5$0%%$E @A(E5DH'6F5,6"%8 x x
public protected
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x private x V G0H(+3$% E @A(E595F 0L/54(7/3#,1? %B0% V :;"<++ public . ='9420L/54(7/ 3#,1?",8"&'0'(* %B.*M1(%$19I2.'10L/5C/6#'&/7B"G52 &H@"0L/54(7/3#,1? %B0% V :;"<++ private.*M1:/61,"G0H9420L/5.'1#'&/7B"(%$19I2G52 !5$. &'0'(**M1(%$19I2G52.'10L/5>'$9"#'&5%$@1," H'",8" !:(<1(0 %B
QPm <&56D,@/$H'6 1'(:(1')0L/5<#3#,1? %B0%V :;"<++public<#privateD,@/$H'6 "%8<&569424K"@H'0L/5<#3#,1? %B0%V :;"<++privateG0H&'0'(* .%B 942 #'&/7B"(%$19I26'"G52 1(% %B0L/54(7/3#,1?G0H(+3%$E@A(E595F&='4(,+ XX V >'?'.'@'.*7/@H':;" V RSB6. ='9420L/54(7/3#,1?",8"&'0'(* %B. *M1(%$19I2.'10L/5C/6#'&/7B" %B/$MH9"<-1K.5%$@1,"G52 &='4(,+@'040'$C/6 V %B:;"<++protected .1#H'@*S6DH/G:
("' .! 6D,@/$H'61'(:(1')0L/5<#3#,1? public class PrivateStudent { private String id; private String name; private double gpa; public void setDetails(String ID,String n,double GPA) { id = ID; name = n; gpa = GPA; } public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); } } ---------------------------------------------------------public class TestPrivateStudent { public static void main(String args[]) { PrivateStudent ps = new PrivateStudent(); /* ps.id = "12345"; illegal ps.name = "Thana"; illegal ps.gpa = 3.25; illegal */ ps.setDetails("12345","Thana",3.25); ps.showDetails(); } }
4 '+("' (0+D)'''&1&/ C2/5%C/61'(4H/4320:(1'(4"SB67/1'(RH/"(2"C2/0M# !:(<1(0IA6//:.K %B5.% //1<++942//:.K0%0L/5 %B:(1')942//:.K/7B"F(%$19I26'"G52
'?'.'@' ='G52!5$1'((+3V C/60L/54(7/3#,1?5,6"%8 x 3#,1?C/6//:.K.0% ;=J/?/80 :;" private -7B/RH/"G0H942//:.K /7B"F(%$19I2G52 x 0L/5C/6//:.K D%B 2/61'(942//:.K/7B"F(%$19I2.0%;=J/?/80:;"public
("' .! D,@/$H'6#'& %BG0HG529I24#,11'(C/61'(4H/4320
public class Student { String ID; String name; public double gpa; } public class NoEncapDemo { public static void main(String args[]) { Student s1 = new Student(); double temp = Double.parseDouble(args[0]); if ((temp<0) || (temp>4.00)) { System.out.println("Incorrect Format!"); } else { s1.gpa = temp; System.out.println("GPA: "+s1.gpa); } } }
.! D,@/$H'6W##,-LE %BG52.'11'((,"!:(<1(0 %B
QPP
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
!:(<1(0 %B
QPP <&56D,@/$H'6#'& %BG0HG529I24#,11'(C/61'(4H/4320 !5$1'( 1='4"59423#,1?gpaRSB60%V :;"public ='942//:.K/7B"F&'0'(*(%$19I2 gpaG52!5$D(6#'& NoEncapDemoG52&(2'6//:.KC/6#'& Student %BI7B/@H' s1 <#G521='4"5942W9M2 I2&'0'(*:/"H' gpaWH'"C2'0' '6XVRSB6WM29I2/'.:/"H' gpa %BG0H*M1D2/6IH" :;"H'D,@#C.='"@"#+ 4(7/0%H'0'11@H' uQmm 5,6",8".S6.D2/60%1'( D(@.&/+H' %B:/"C2'0'1H/" .%B 0%1'(1='4"5H'9421,+3#,1? gpa 31(,86!5$9I2='&,B6 if..else 1'(C%$"!:(<1(0<++"%8 ='942!:(<1(0R,+R2/"CS8"<#$'1DH/1'(<12GC RSB6 D,@/$H'6C/6W##,-LE %BG52.'11'((,"!:(<1(0 %B
QPP :;"5,6(M: %B
QP !:(<1(0 %B
QP G52:#%B$"3#,1?C/6 gpa 9420% V :;" private <#1='4"50L/5940H %BI7B/ setGPA() %B0% V :;" public -7B/942//:.K/7B"F %B D2/61'(1='4"5H' gpa (%$19I2G52 RSB61'(C%$"!:(<1(0<++"%8:;"1'(9I24#,11'(C/61'( 4H/43205,6",8"1'(1='4"5H'9421,+3#,1? gpaC/6//:.K s1 9"#'& EncapDemo . ='G52!5$1'((%$19I20L/5setGPA() H'",8"RSB69"0L/55,61#H'@.0%1'(D(@.&/+H' C/6 gpa /$MH>'$9" ='9421'(C%$"!:(<1(0 ='G526H'$CS8"-('G0H.=':;"D2/6D(@.&/+H' 31(,86 <#6H'$DH/1'(<12GC!:(<1(09"1(% %BC2/1='4"5C/63#,1? gpa :#%$B "<:#6 G: "7B/6.'1:;"='&,B6|-'9"0L/5 H'",8"
4 G0"bGGJXXNB 1'(C2'*S63#,1?C/6//:.K 0%B % V :;" private",8".D2/69I20L/5 <++XX 9"1'(1='4"5H'C/63#,1?4(7/5S6H'C/63#,1?//10'9I26'" 0L/5<++XX <+H6//1G52:;"&/6:(> 7/ 0L/5<++K81180.9I29"1'(1='4"5H'C/63#,1?RSB60L/5<++"%8.G0H0% 1'(&H6H'1#,+ 5,6",8".S6(+3I"A5C2/0M#C/6H' D%B 2/61'(&H61#,+:;" void !5$ @,B G:I7B/C/60L/5<++"%8.CS8"D2"52@$='@H' set <#2@D'052@$I7B/C/6 3#,1?RSB60%(M:<++5,6"%8
public void setAttributeName(dataType arg) { attributeName = arg; }
!5$ %B x
dataType7/I"A5C2/0M#C/63#,1?
%BD2/61'(1='4"5H'
x
arg7/D,@<:(
%B.(,+H'C/63#,1? %BD2/61'(1='4"5
x attributeName7/I7B/C/63#,1? D,@/$H'6IH"#'& Student 0%0L/5 setGPA() -7B/1='4"5H'C/6 3#,1?gpaRSB60%='&,B65,6"%8
public void setGPA(double GPA) { gpa = GPA; }
("' .! $D,@/$H'6#'& 9% I24#,11'(C/61'(4H/4320
public class Student { String ID; String name; private double gpa; public void setGPA(double GPA) { if ((GPA<0) || (GPA>4.00)) { System.out.println("Incorrect Format!"); } else { gpa = GPA; } } public double getGPA() { return gpa; } } public class EncapDemo { public static void main(String args[]) { Student s1 = new Student(); double temp = Double.parseDouble(args[0]); s1.setGPA(temp); System.out.println("GPA: "+s1.getGPA()); } }
0L/5<++ ^81180 .9I29"1'((%$1H'C/63#,1? RSB60L/5<++"%.8 0%1'(&H6 H'1#,+ 5,6",8".S6D2/6(+3I"A5C2/0M#C/6H' %B&H61#,+0'52@$&0/ !5$ ,B@G:I7B/ C/60L/5<++"%8.CS8"D2"52@$='@H' get <#2@D'052@$I7B/C/63#,1? RSB60% (M:<++5,6"%8
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
public dataType getAttributeName() { return attributeName; }
D,@/$H'6IH"#'&Student 0%0L/5getGPA()-7B/(%$1H'C/6gpa %B0% ='&,B65,6"%8 public double getGPA() { return gpa; }
I7B/C/60L/5<++ &='4(,+3#,1? %B0%I"A5C2/0M#:;" boolean !5$ @,B G:.1='4"5942CS8"D2"52@$='@H' is < " .%B :;"='@H' get /' AIH" 0L/5 isGraduate() :;"0L/5-7B/(%$1H'C/63#,1? graduate C/6#'&Student %B0I% "A5C2/0M#:;"boolean ("' .! %D,@/$H'6#'& 9% I24#,11'(C/61'(4H/4320
public class EncapStudent { private String id; private String name; private double gpa; public void setID(String ID) { id = ID; } public void setName(String n) { name = n; } public void setGPA(double GPA) { if ((GPA<0) || (GPA>4.00)) { System.out.println("Incorrect Format!"); } else { gpa = GPA; } } public String getID() { return id; } public String getName() { return name; } public double getGPA() { return gpa; } }
!:(<1(0 %B
QPc <&56D,@/$H'6C/6!:(<1(0>'?'.'@' 9%B I24#,11'(C/61'(4H/4320 !5$1='4"59423#,1?DH'6FC/6//:.K0% V :;"<++ private<#1='4"59420% 0L/5<++ XX ,86 %B :;"<++ <# -7B/ ='942&'0'(*C2'*S63#,1?
public class Account { private double balance; public double getBalance() { return balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } }
4 +0this %$@A(E5 this 40'$*S6//:.KC/6D,@/6 ('&'0'(* .%B (%$19I20L/54(7/ 3#,1?>'$9"#'&G52!5$9I2$% E @A(E5thisRSB60%(M:<++5,6"%8
this.methodName(); this.attirbuteName;
!5$ @,B G:('.G0H9I2$% E @A(E5this9"='&,6B $1@2"9"1(% .%B =':;"
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
D,@/$H'6!:(<1(0 %B
QPt :;"1'((%$19I23#,1?C/6//:.KD!5$9I2%$E @A(E5 this 1(%"%8*2'G0H(+3@H' this.id, this.name <# this.gpa /0G-#/(E. C2'9.@H'40'$*S6D,@<:( %B :;" 0^2;871 C/60L/5 G0H9IHD@, <:( %B :;"3#,1?C/6 //:.K ("' .! -D,@/$H'6<&561'(9I2%$E @A(E5this
public class ThisStudent { private String id; private String name; private double gpa; public void setDetails(String id,String name, double gpa) { this.id = id; this.name = name; this.gpa = gpa; } public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); } }
4" '+("' (0+D)'''0 C2/5%C/61'(&7+ /57/1'("='#'& %B0%/$MH<#2@0'9I2940H!5$1'(-AB0DA03#,1? 4(7/0L/59"#'&940H -7B/942C2'9.4#,11'(C/61'(&7+ /5942-A.'('D,@/$H'6C/6#'& Student %B1='4"5G@29"!:(<1(0 %B
QP
RSB6*2'4'1.=':;" %B.D2/6-,f"'#'&CS8"940H %B I7B/ GradStudent-7B/9I29"1'(&(2'6//:.K %B :;"",1)S1?'(5,++,AD)S1?'",8"(' &'0'(* .%B #7/1@AL%1'(G52&/6<++7/ &(2'6#'&CS8"0'940H!5$G0H/2'6/A61,+#'&5A0 %BI7B/ Student 5,6!:(<1(0 %B
QPv &(2'6#'& &%B 7+ /50'.'1#'&5A0 I%B 7B/Student5,6!:(<1(0 %B
QPz 1'(&(2'6#'&CS8"940HD'0!:(<1(0 %B
QPv ",8".4K"G52@H'0%@'0R8='R2/"9"1'( 1='4"53#,1?<#0L/5C/6#'& "/1.'1"%$8 ,6. ='9421A5@'0$3H6$'19"1'(<12GC
!:(<1(0/%152@$/' AIH" 4'1D2/60%1'(-AB03#,1? !:(<1(0C/6 ,86&/6#'&
address
. ='942D2/6<12GC
("' .! .D,@/$H'6#'& I% 7B/Student
public class Student { private String id; private String name; private double gpa; public void setID(String ID) { id = ID; } public void setName(String n) { name = n; } public void setGPA(double GPA) { gpa = GPA; } public void showDetails() { System.out.println(“ID: “+id); System.out.println(“Name: “+name); System.out.println(“GPA: “+gpa); } }
>'?'.'@'G521='4"5%$E @A(E5 I%B 7B/ extends -7B/(+31'(&7+ /5C/6#'& !5$0% (M:<++5,6"%8
public class SubClass extends SuperClass { ... }
!5$ %B SuperClass 7/I7B/C/6#'& D%B 2/61'(.9420%1'(&7+ /53#,1?<# 0L/5DH'6FG:$,6SubClass !:(<1(0 %B
QPv&'0'(* %B.C%$"G52940H!5$9I24#,11'(C/61'(&7+ /55,6<&569" !:(<1(0 %B
QPz RSB6.4K"G52@H'1(%"%8.#5@'0R8='R2/"C/63#,1?<#0L/5 <#. ='9421'(:(,+:(36<#<12GC!:(<1(0:;"G:G526H'$$AB6CS8" G5/<1(0#'&C/6#'& 6,8 &/6 &'0'(*<&565,6(M: %B
Q#'& GradStudent.&7+ /53#,1?<#0L/5C/6#'&
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
Student0'5,6",8"//:.KC/6#'&GradStudent.0%3#,1?
<#
%B:;"id,
name
gpaIH"5%$@1,+//:.KC/6#'&Student
("' .! 3D,@/$H'6#'& I% 7B/GradStudent %BG0HG529I24#,11'(C/61'(&7+ /5
public class GradStudent { private String id; private String name; private double gpa; private String thesisTitle; private String supervisor; public void setID(String ID) { id = ID; } public void setName(String n) { name = n; } public setGPA(double GPA) { gpa =void GPA; } public void setThesisTitle(String t) { thesisTitle = t; } public void setSupervisor(String s) { supervisor = s; } public void showThesis() { System.out.println("ThesisTitle: "+thesisTitle); System.out.println("Supervisor: "+supervisor); } }
("' .! 4D,@/$H'6#'& I% 7B/GradStudent %B9I24#,11'(C/61'(&7+ /5
public class GradStudent extends Student { private String thesisTitle; private String supervisor; public void setThesisTitle(String t) { thesisTitle = t; } public void setSupervisor(String s) { supervisor = s; } public void showThesis() { System.out.println("ThesisTitle: "+thesisTitle); System.out.println("Supervisor: "+supervisor); } }
.!$1'(9I24#,11'(C/61'(&7+ /5
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
4" '0 L ' ('&'0'(* %B.1='4"5942#'&95F&7+ /5#'&/7B"G52
char size; float price;
} public class Skirt { char size; float price; boolean long; }
",1-,f"'!:(<1(0/'.4K"@H'#'& ,86&/60%3#,1? %BR8='R2/"1,"4#'$I7B/ .S6 /'.D,5&A"9.1='4"5942#'&Skirt&7+ /50'.'1#'&Shirt!5$0%!:(<1(05,6"%8 publicclass Shirt {
char size; float price;
} public class Skirt extends Shirt { boolean long; }
}
,86"%8"7B/6.'1 ,86&78/ <#1(!:(6w DH'61K:;"&78/W2'X
4" +0
protected
*S6<02@H'#'& %B :;" dX .&7+ /53#,1?<#0L/5C/6#'& %B:;" X 0' '?'.'@'1='4"59420% XX V %BI7B/ protected RSB6. '= 942#'& %B:;" dX &'0'(* %B.(%$19I20L/5 4(7/3#,1?C/6 XG52 V <++ protected.
# + 1D
#0+' " '* 0+'
# H XD]GE>XX
#D0C
public
protected
default
Private
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .! 5 D,@/$H'61'(9I2%$E @A(E5protected public class Student { protected String id; protected String name; protected double gpa; public void setDetails(String ID,String n,double GPA) { id = ID; name = n; gpa = GPA; } public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); } }
4"" # ) Object #'& 31#'&9">'?'.'@'*2'G0HG52&+7 /5.'1#'&95#$ .*7/@H'#'&",8"&7+ /5.'1#'& %BI7B/ Object /' A IH" #'& Student .&7+ /50'.'1#'& I%B 7B/ Object<#='&,6B :(1')#'&.*7/&07/"@H'0%(M:<++5,6"%8 public class Student extends Object { ... }
#'& %BI7B/Object.0%0L/5 %B&=',n7/ x public String toString()<# x public boolean equals(Object o)
-7B/9I29"1'(<:#6//:.KC/6#'&942:;"//:.KI"A5 :(%$+ %$+//:.KC/6#'&",8"1,+//:.K/7B"F
String
<#9I29"1'(
4"& +0super super :;"%$E@A(E5 9%B I29"1'(/2'6/A6*S6 X -7B/ %B.(%$19I20L/54(7/ X X C/6 X!5$0%(M:<++='&,B65,6"%8
super.methodName([arguments])
D,@/$H'6IH" #'& GradStudent /'.1='4"59420% 0L/5 %BI7B/ showDetails() !5$0%='&,6B %B (%$19I20L/5 showDetails() C/6#'& Student 5,6<&569"!:(<1(0 %B
Qm ("' .!$6D,@/$H'61'(9I2%$E @A(E5super public class Student { private String id; private String name; private double gpa; public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); } } class GradStudent extends Student { publicprivate String thesisTitle; private String supervisor; public void showDetails() { super.showDetails(); System.out.println("Thesis Title: "+ thesisTitle); System.out.println("Supervisor: "+supervisor); } }
4& ' ?0+ " 1'(0%G524#'$(M:<++40'$*S63&0+,DAC/6//:.KC/6#'& %BDH'61," &%B '0'(*. 1='4"5H'G524#'$(M:<++ 4(7/1'(D/+&"/6DH/0L/55%$@1,"52@$@AL%1'( D%B H'61,"G52 4#,11'(C/61'(0%G524#'$(M:<++:;"4#,11'( &%B 7+"7B/60'.'14#,11'(C/61'(&7+ /5
4& RqI>LAG_AIWAIe XeV:;"4#,11'(C/61'(1='4"5//:.KC/6#'& %B0%1'(&7+ /51," G524#'$(M:<++>'?'.'@'1='4"5942//:.KC/6#'&95F&'0'(*&(2'60'.'1#'&",8"4(7/ #'&/7B"F %B :;" dX G52 1#H'@7/*2':(1')#'& SubClass RSB6&7+ /50'.'1
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
#'&
SuperClass
<#2@ <#0%='&,B6:(1')//:.K obj 942:;"//:.KC/6#'& SuperClass!5$9I2='&,B6 SuperClass obj; ('&'0'(*9I24#,11'(C/61'(0%G524#'$(M:<++9"1'(&(2'6//:.K objG52 4#'$ (M:<++5,6"%8 obj = new SuperClass(); 4(7/ obj = new SubClass(); 9"1(%C/6D,@/$H'6C/6#'&Student %B0% dX:;"#'&GradStudent !5$ %B#'& GradStudent 0% dX /%1#'&4"SB6 I%B 7B/ PhDStudent ",8" (' &'0'(* .%B 1='4"5//:.KC/6#'&4#H'"%8G524#'$(M:<++ 5,6<&56@'0&,0-,"LE9"(M: %B
Qc 1#H'@7/ ('&'0'(* %B:(1')//:.KC/6#'& Student <#2@&(2'6//:.KG524#'$ (M:<++5,6"%8 Student s1 = new Student(); 4(7/ Student s1 = new GradStudent(); 4(7/ Student s1 = new PhDStudent(); IH"5%$@1,+1(%C/6//:.KC/6#'& GradStudent &'0'(* .%B &(2'6 //:.KG524#'$(M:<++5,6"%8 GradStudent s2 = new GradStudent(); GradStudent s2 = new PhDStudent();
.!%//:.KC/6#'& %B0G% 524#'$(M:<++
4& ''A&0 G0D& G' 1' 4#,11'(C/61'(0%G524#'$(M:<++$,6(@0G:*S61'( %B 0L/5I7B/5%$@1,"'?'.'@'/"3n'D942#'&95F 0%0L/5 %BI7B/5%$@1,"0'11@H'4"SB60L/5G52
("' .!$ D,@/$H'6C/60L/5<++=-803=,J8J public }
class StudentV2 { private String id; private String name; private double gpa; public void setDetails(String ID,String n) { id = ID; name = n; } public void setDetails(String ID,String n,double GPA) { id = ID; name = n; gpa = GPA; }
D,@/$H'6DH/G:"%8 :;"1'(C%$"0L/5<++ VV %B*M1D2/6 public void setDetails(String ID,String
n) { } public void setDetails(String ID,double GPA) { } public double setDetails(double GPA,String ID){
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
}
D,@/$H'6DH/G:"%8 :;"1'(C%$"0L/5<++ VV %BG0H*M1D2/6 public void setDetails(String ID,double
GPA) {
} public void setDetails(String n,double GPA) { }
1(%D,@/$H'6 %BG0H*M1D2/6"%*8 S6<02@H'.0%I7B/C/6 %BDH'61,"1KD'0
("' .!$$ 0L/5<++ VV %BI7B/showDetails()
public class protected protected protected
Student { String id; String name; double gpa;
public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); } } ---------------------------------------------------------public class GradStudent extends Student { private String thesisTitle; private String supervisor;
public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); "+gpa); System.out.println("GPA: System.out.println("ThesisTitle: "+thesisTitle); System.out.println("Supervisor: "+supervisor); } }
wVbdP9`lZdxXY=U:XW9dVXU 1'(1='4"5//:.KD'04#,11'(C/6 X eV ='942!:(<1(0>'?' .'@'-A.'('(%$19I20L/5.'1I"A5C/6//:.K &%B (2'6CS8"D,@/$H'6IH"='&,6B Student s1 = new GradStudent(); s1.showDetails();
:;"='&,B6 %B (%$19I20L/5 showDetails() C/6#'& GradStudent G0H9IH 0L/5C/6#'& Student ,86"%8 "7B/6.'1//:.K s1 :;"//:.KC/6#'& GradStudent '?'.'@'.G0H/"3n'D942(%$19I20L/595F1KD'0 %B G0H0%1'(:(1')/$M9H "0L/5C/6 X %B1='4"5G@21#H'@7/='&,6B Student s1 = new GradStudent(); s1.getSupervisor();
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
:;"='&,B6 G%B 0H*M1D2/6<#.G0H&'0'(*/0G-#EWH'"G52 "7B/6.'1/0G-#/(E.G0H (M2.,10L/5 getSupervisor() %B/$MH9"#'& Student *S6<02@H'//:.K s1 .:;"//:.KC/6#'&GradStudent %B0% 0L/55,61#H'@/$MH1KD'0
4&" ##*') "tJCJBNeJIJNDX /##K1I,B"<++ U 7/1'(1K+C2/0M# 0%B I% "A5C2/0M#DH'61,"9"1#3H0 5%$@1," RSB6. /##K1I,B" %B1='4"59" X NO RSB6.1#H'@*S6DH/G:9"+ %B z .,5:;"/##K1I,B" <++ U ,86"%8 "7/B 6.'1('&'0'(* %B.1K+//:.KC/6#'&95F9"/##K1I,B" G52 D'04#,11'(C/61'(0%G524#'$(M:<++"7B/6.'1#'&:(> /##K1I,B"5,61#H'@.9I29" 1'(1K+//:.KC/6#'& Object RSB6#'&5,61#H'@.:;"#'& %B :;"#'&('1 XC/6 31F#'& %B.D2/6&7+ /51,"0' ('&'0'(*.1='4"5/($EC/6C2/0M#I"A5#'& 942&'0'(*1K+C2/0M#<++ U G52 !5$1'(&(2'6&0'IA1C/6/($E
= = = = =
new new new new new
Student(); GradStudent(); PhDStudent(); FullTimeStudent(); PartTimeStudent();
4&& '1g1>BeDLJIC ?0+ 9"1(% %B0L/50% :;"C2/0M#I"A5#'& ('&'0'(* %B.9I24#,11'(C/6 1'(0%G524#'$(M:<++9"1'(&H6 %B :;"//:.K9421,+0L/55,61#H'@G52 1#H'@7/(' &'0'(* .%B &H6//:.KC/6#'& %B :;" dXC/6#'&",8"< "G52D,@/$H'6IH" ='&,B6 Student s1 = new Student(); Student s2 = new GradStudent();
<#0L/5 public void printInfo(Student s) { ... }
:;"0L/5 0%B % :;"C2/0M#I"A5#'&Student ('&'0'(* .%B (%$19I2 0L/5"%8!5$1'(&H6//:.K &%B 7+ /50'.'1#'&Student G52/' AIH" printInfo(s1) printInfo(s2)
4(7/
printInfo(new FullTimeStudent())
4&- 0A'instanceof %$E@A(E5 instanceof :;"D,@5='"A"1'( 9%B I21,+//:.K<##'& -7B/D(@.&/+ @H'//:.K95F:;"//:.KC/6#'& %B(+34(7/G0H %$E@A(E5 instanceof.942W##,-LE:;" I"A5C2/0M#<++ boolean RSB6*2'G52W##,-LE:;" true .40'$*S6@H'//:.K5,61#H'@:;" //:.KC/6#'& %B(+34(7/:;"//:.KC/6#'& %B :;" XC/6#'&",8" D,@/$H'6IH" GradStudent s1 = new GradStudent();
:;"='&,B69"1'(:(1')<#&(2'6//:.K s1 C/6#'& GradStudent 5,6",8" "A-."E (s1 instanceof GradStudent) .942W##,-LE:;"trueIH"5%$@1,+"A-."E (s1 instanceof Student) 4(7/ (s1 instanceof Object) %B.942W##,-LE:;" true ,86"%8"7B/6.'1#'& GradStudent &7+ /50'.'1 #'&Student <##'&Student1K&7+ /50'.'1#'& %BI7B/Object /%1DH/4"SB6
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
&H@""A-."E (s1 instanceof String)
.942W##,-LE:;" false ,86"%8"7B/6.'1//:.KC/6#'& GradStudent . G0H9IH//:.KC/6#'& &%B 7+ /50'.'1#'&String "/1.'1"%8%$E @A(E5instanceof.942W##,-LE:;"false9"1(% %B.D(@.&/+ @H'//:.K5,61#H'@:;"//:.KC/6#'& %B:;" dX4(7/G0HD,@/$H'6IH"='&,6B Student s1 = new Student();
.G52"A-."E (s1 instanceof GradStudent)
%B942W##,-LE:;"false %$E@A(E5instanceof&'0'(*9I29"1'(D(@.&/+@H' %B(,+C2/0M#0'9" 0L/5:;"//:.KC/6#'&95D,@/$H'6IH"='&,B6DH/G:"%8 :;"1'(9I2$% E@A(E5instanceof -7B/D(@.&/+@H' C/6//:.Ks@H':;"//:.KC/6#'&95
public void printInfo(Student s) { if (s instanceof PhDStudent) { System.out.println("PhD Student"); } else if (s instanceof GradStudent) { System.out.println("Graduate Student"); } else if (s instanceof FullTimeStudent) { System.out.println("Full-Time Student"); } else if (s instanceof PartTimeStudent) { System.out.println("Part-Time Student"); } else if (s instanceof Student) { System.out.println("Student"); } }
4&4 'd>XCAIe*
4#,11'(C/61'(0%G524#'$(M:<++ ='942('&'0'(*.1='4"5//:.KC/6#'& %B :;" dX9421,+D,@<:( %B1'= 4"5:;"#'& %B :;" XG52D,@/$H'6IH" GradStudent s1 = new GradStudent(); Student s2 = s1;
Student s1 = new GradStudent(); GradStudent s2 = s1;
.:;"='&,B6 %BG0H*M1D2/6<#!:(<1(0"%.8 G0H&'0'(*/0G-#EWH'"G521'(1='4"5H' 9"#,1?"%8.D2/6 ='1'(X//:.K-7B/(+3I"A5C/6//:.K!5$0%(M:<++5,6"%8
(ClassName) objectName
!5$ %B
x
ClassName7/I"A5C/6#'&
x
objectName7/I7B/C/6D,@<:(C/6//:.K
%BD2/61'((+3
D,@/$H'6 %BWH'"0'&'0'(*C%$"940HG52:;" Student s1 = new GradStudent(); GradStudent s2 = (GradStudent) s1;
.S6. ='942!:(<1(0&'0'(*/0G-#EWH'"G52 IH"5%$@1,+='&,B69"4,@C2/
QuQ %B:;" D,@/$H'6C/61'((%$19I20L/51K.&'0'(*C%$"940HG52:;" Student s1 = new GradStudent(); ((GradStudent) s1).getSupervisor();
.S6. ='942!:(<1(0&'0'(*/0G-#EWH'"G52 .'1D,@/$H'6C2'6D2"//:.K s1 .*M1 1='4"5942:;"#'&I"A5GradStudent %B0% 0L/5getSupervisor() >'?'.'@'.D(@.&/+I"A5C/6//:.K9"IH@6C/61'((,"!:(<1(0 5,6",8"*S6<02@H' !:(<1(0./0G-#EWH'"G52
.:;"='&,B6 %B ='9421A5C2/WA5-#'59"IH@6C/61'((,"!:(<1(0 "7B/6.'1D,@<:( s1 *M11='4"5H'942:;"//:.KC/6 X RSB6G0H&'0'(* ='G52 IH"5%$@1,+1'((%$19I2 0L/5C/6//:.K %B ='1'( X >'?'.'@'.D(@.&/+I"A5C/6//:.K9"IH@6C/61'( (,"!:(<1(0 @H'//:.K5,61#H'@:;"//:.KC/6#'&95 <##'&",8"0%0L/5 %B (%$19I2 4(7/G0H4'1G0H-+1K.1A5C2/WA5-#'59"IH@6C/61'((,"!:(<1(0D,@/$H'6IH" Student s1 = new Student(); ((GradStudent) s1).getSupervisor();
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
.:;"='&,6B &%B '0'(*/0G-#EWH'"G52
public class Student { protected String id; protected String name; protected double gpa; public String getID() { return id; } public String getName() { return name; } public double getGPA() { return gpa; } } public class GradStudent extends Student { private String thesisTitle; private String supervisor; public String getThesisTitle() { return thesisTitle; } public String getSupervisor() { return supervisor; } } public class TestCallingMethods {
public static void main(String args[]) { Student s1 = new GradStudent(); s1.getSupervisor(); // compile error Student s2 = new Student(); ((GradStudent) s2).getSupervisor(); // runtime error
}
}
4- pXU^dbPWdXb X X :;"0L/5 0%B %I7B/5%$@1,+I7B/#'&RSB6.*M1(%$19I26'"07B/0%1'(&(2'6 //:.K!5$9I2='&,6B new!5$X X 0%(M:<++5,6"%8
[modifier] ClassName([arguments]) { [statements] }
X X :;"0L/5 %BG0H0%H' .%B &H61#,+
public ClassName() { }
X X <++ V .:;" X X %BG0H0%='&,B695F/$MH>'$9" /0G-#/(EC/6>'?'.'@'.9&H X X <++V 9421,+!:(<1(0!5$/,D!"0,DA /' A IH"#'& Student9"!:(<1(0 %B
QP
.0% X X <++V %B/0G-#/(E9&H 9421,+!:(<1(05,6"%8 public class Student { ... public Student() { } ... }
4- '+dNIXCBDGCNB ('&'0'(* %B.C%$"X X 9"#'&95FCS8"0'1KG52/' A IH"#'& Student /'.1='4"59420%X X :;" public class Student { private String id;
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
private String name; private double gpa; public Student(String ID,String n,double GPA) { id = ID; name = n; gpa = GPA; } ... }
1'(1='4"5 X X CS8"0'940H",8". ='942 X X <++ V 4'$G: 5,6",8"1'(&(2'6//:.KC/6#'& 0%B % X X 940H"%8.D2/69I2='&,B6 new %B&H6WH'".='"@" <#I"A5C2/0M#C/6 %B&/5#2/61,+ %B1='4"5G@29"X X D,@940H.'1D,@/$H'6 C2'6D2"1'(&(2'6//:.KC/6#'&Student/'..9I2='&,B6 Student s1 = new Student(“1122”,“Thana”,3.00);
4- 'A A new ='&,B6new %B9I29"1'(&(2'6//:.KC/6#'&.0%#='5,+C,8"D/"1'( ='6'"5,6"%8 PQ 1='4"5"78/ %B9"4"H@$@'0.='9421,+//:.K Q 1='4"5H'(AB0D2"9421,+3#,1?C/6//:.K cQ 1='4"5H'C/63#,1?C/6//:.KD'0='&,B61='4"5H' %B:(1')G@2 uQ (%$19I2X X D,@/$H'6IH"*2'#'&MyDate0%!:(<1(05,6"%8 public class MyDate {
private int day = 1; private int month = 1; private int year = 2000; public MyDate(int d,int m,int y) { day = d; month = m; year = y; }
}
='&,B6 MyDate d1 = new MyDate(16,8,1972); .0%C,8"D/"9"1'( ='6'"5,6(M: %B
Qu !5$C,8"D/" %B4"SB6.:;"1'(1='4"5"78/ %B9" 4"H@$@'0.=' (M: %B
Qu1 C,8"D/" &%B /6:;"1'(1='4"5H'(AB0D2"!5$3#,1?C/6 //:.K.0%H'V :;"m(M: %B
QuCC,8"D/" %B&'0:;"1'(1='4"5H'C/63#,1? D'0='&,B6 %B1='4"5H'G@2(AB0D2" !5$ %B3#,1? day .0%'H :;" P 3#,1? month .0%H':;"P<#3#,1? year.0%H':;"mmm(M: %B
Qu<#C,8"D/"&35 2'$. :;"1'((%$19I2 X X ='942H'C/63#,1?:;"G:D'0 %B1='4"59"='&,B6C/6 X X (M: %B
Qu6
E'FC,8"D/" %B4"SB6
EFC,8"D/" %B&/6
FC,8"D/" %B&'0
EFC,8"D/" %B&%B
.!,C,8"D/"1'( ='6'"C/6='&,B6 new
4-" dNIXCBDGCNB"i?JBEN>WJW >'?'.'@'&'0'(*1='4"5 X X <++ VV IH"5%$@1,+1'(1='4"5 0L/5<++ VV !5$ X X <++ VV .0%.='"@"4(7/I"A5C2/0M#C/6 %BDH'61,"RSB6='&,B6 new9"1'(&(2'6//:.KC/6#'&.(%$19I2 X X !5$ 1'(-A.'('.'1 %B&6H WH'"0'@H'&/5#2/61,+X X %B1='4"5D,@95C2/5%C/6
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
1'(&(2'6X X <++ VV7/ ='942('&'0'(* .%B &(2'6//:.K(A0B D2"G524#'$ (M:<++ !:(<1(0 %B
Qu <&56D,@/$H'6C/6#'& Student %B0% X X <++ VVRSB6='&,B61'(&(2'6//:.K Student s1 = new Student(“1234”,“Somchai”,3.75);
.0%W# ='942/0G-#/(E(%$1 X X C/6#'& &'0D,@
Student
I35 %B&/6RS6B 0%
("' .!$,D,@/$H'61'(C%$"5=7K10251=0<++=-803=,J8J public class Student {
private String id; private String name; private double gpa; public Student(String ID,String n) { id = ID; name = n; } public Student(String ID,String n,double GPA) { id = ID; name = n; gpa = GPA; }
}
4-& G0
this()
0L/5 I%B 7B/this() :;"1'((%$19I2X X C/6#'&D,@/6!5$.D2/6:;" ='&,B6<(1&35 %B/$MH9"X X <++ VV D,@/$H'6IH"!:(<1(0 %B
Qu&'0'(* C%$" X X C/6#'& Student I35 %B&/6940HG52!5$9I20L/5 %BI7B/ this() 5,6 <&569"!:(<1(0 %B
Qt
("' .!$- D,@/$H'61'(9I20L/5 %BI7B/this()
public class Student { private String id; private String name; private double gpa; public Student(String ID,String n) { id = ID; name = n; } public Student(String ID,String n,double GPA) { this(ID,n); gpa = GPA; } }
4-- G0super() !5$ @,B G:0L/5<#3#,1?C/6 X.&7+ /50'$,6 dX
public class Parent { ... } public class Child extends Parent { public Child (String s) { ...
9"1(%"%8#'& Parent 0% X X <++ V &H@"#'& Child %B&7+ /50'.'1#'& Parent0% X X %B0% :;" String-%$6(M:<++5%$@ "7B/6.'1 X X <++ V .G0HG52&+7 /50'$,6#'& Child 5,6",8"1'(&(2'6 //:.K!5$9I2X X <++V /' AIH" Child c1 = new Child();
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
.S6:;"='&,B6 G%B 0H*M1D2/6"7B/6.'1X X <++V C/6#'& ChildG52 *M1< " %B5@2 $X X <++940HG:<#2@ ('&'0'(* %B.(%$19I2 X X C/6 X G52!5$9I20L/5 %BI7B/ super() !5$&H6WH'" %B&/5#2/61," 6,8 "%80L/5 super() .D2/6:;"='&,B6 <(1C/6 X X IH"5%$@1,+0L/5 %BI7B/ this() !5$ @,B G:1'( ='6'"C/6 X X C/6#'&95F.0%W# ='9420%1'((%$19I2 X X C/6 X",8"RSB6*2' G0H0%'= &,6B super() /$MH9"='&,B6<(1C/6 X X C/6#'&",8" >'?'.'@'.(%$19I2 X X <++V C/6 X",8"!5$/,D!"0,DA 9"1(% %B XG0H0%X X <++V <#G0H0% ='&,B6super() /$MH 9" X X C/6 dX . ='942!:(<1(0G0H&'0'(*/0G-#EWH'"G5256, D,@/$H'69" !:(<1(0 %B
Q
RSB69"1(%"%8.D2/69I2='&,B6 super() 9" X X C/6#'& GradStudentV1 -7B/ %B. ='942!:(<1(0&'0'(*/0G-#EWH'"G52 !5$.D2/61='4"5 X X C/6#'&GradStudentV1940H5,6"%8 public GradStudentV1(String n){ super(n); }
("' .!$. D,@/$H'61'((%$19I25=7K10251=0C/6K2B8053,KK
public class StudentV1 { protected String name; public StudentV1(String n) { name = n; } } public class GradStudentV1 extends StudentV1 { public GradStudentV1(String n) { /* compile error name = n; should be super(n) */ } }
'ApXU^dbPWdXb
//:.KC/6#'&95F.*M1&(2'6CS8"07B/0%1'((%$19I2='&,B6 new D'0C,8"D/"1'( ='6'" %BG521#H'@WH'"0'<#2@ !5$C,8"D/"&35 2'$:;"1'((%$19I2 X X RSB6.0%C,8"D/" 1'( ='6'" %BD2/6-A.'('-AB0DA05,6"%8
PQ *2'0%='&,B6 this()9"X X 1K.(%$19I2 X X <++ VV %B&/5#2/61,+='&,B6this()<#2@C2'0G:C,8"D/" %Bu Q (%$19I2 X X C/6 X *2'G0H0%='&,B6 super() .(%$19I2 X X <++ V $1@2"#'& %BI7B/ Object .G0H0%1'((%$19I2 X X C/6 X "7B/6.'1#'& %BI7B/ Object .G0H0% X cQ (%$19I2='&,B61='4"5H'(A0B D2"C/63#,1?C/6//:.K uQ (%$19I2='&,B6>'$9"X X C/6#'& %B9I2",8" !:(<1(0 %B
Qv <&56D,@/$H'6#'& 0%B % X X 9"(M:<++DH'6F 1(% 0%B %1'( &(2'6//:.KC/6#'&GradStudent !5$9I2='&,B6 GradStudent s1 = new GradStudent(); .0%W# ='942X X C/6#'&GradStudent ='6'"5,6"%8 (%$19I2 X X C/6#'& GradStudent %B0%(M:<++='&,B6:;" GradStudent()
(%$19I2='&,B6this(“ ”) (%$19I2 X X C/6#'& GradStudent %B0%(:M <++:;" GradStudent(String n) !5$ %B.&H6C2/@'0 “ ” 9421,+ %BI7B/n "7B/6.'1G0H0%='&,B6this().S6C2'0C,8"D/""%8G: (%$19I2X X C/6#'&Student.'1='&,B6super(n) QcQP(%$19I2X X C/6#'&Student %B0%(M:<++='&,B6:;" Student(String n) "7B/6.'1G0H0%='&,B6this().S6C2'0C,8"D/""%8G: (%$19I2 X X C/6#'& I%B 7B/ Object %B:;" X X <++V (%$19I2X X <++V C/6#'& I%B 7B/Object "7B/6.'1G0H0%='&,B6this() .S6C2'0C,8"D/""%8G: "7B/6.'1#'& %BI7B/ ObjectG0H0% X .S6C2'0 C,8"D/"C/61'((%$1X X C/6 X
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
G0H0% ='&,6B 1='4"5H'(A0B D2"C/63#,1?C/6#'& %BI7B/ Object
G0H0%'= &,6B >'$9"X X C/6#'& I%B 7B/Object (%$19I2='&,B61='4"5H'(A0B D2"C/63#,1?C/6//:.K9"#'& Student (%$19I2='&,B6name = n9"X X C/6#'&Student (%$19I2='&,6B 1='4"5H'(A0B D2"C/63#,1?C/6//:.K9"#'& GradStudent
G0H0%='&,6B /7B"F>'$9"X X C/6#'&GradStudent ("' .!$3 D,@/$H'6#'& 0%B %5=7K10251=09"(M:<++DH'6F public class Student { protected String name; public Student(String n) { name = n; } } public class GradStudent extends Student { public GradStudent(String n) { super(n); } public GradStudent() { this(" "); } }
44 G0 # ) Object #'& %BI7B/ Object.:;"#'& %B 31F#'&.&7+ /50'#'& I%B 7B/ Object 0% 0L/5 &%B =',n4#'$0L/5/' AIH" toString()<# equals()9"1'( %B.&'0'(*9I2 6'"0L/54#H'"%8G52.D2/60%1'(C%$"='&,B6-7B/1='4"5942:;"0L/5<++ VV 9" #'& %BD2/61'(
44 G0toString() 0L/5 toString() :;"0L/5
9%B I29"1'(<:#6H'C/6//:.K942:;"C2/0M# I"A5 String #'& %B/$M9H " NO IH"#'& Date G521='4"5='&,B6&='4(,+0L/5 toString() G@2<#2@ 5,6",8"1'((%$19I20L/5 System.out.println() !5$ %B :;"//:.KC/6#'& Date. ='9420%1'((%$19I20L/5 toString() C/6 #'&Date!5$/,D!"0,DAD,@/$H'6IH"='&,B6 Date d = new Date(); System.out.println(d);
.G52W##,-LE:;"C2/@'0 %B(+3@,"57/":rC/6//:.Kd !:(<1(0 %B
Qz<&56D,@/$H'6C/6#'& Student %B0% 0L/5<++ VV %B I7B/ toString() -7B/1='4"5C2/@'0 %B&6H 1#,+0'07B/0%1'((%$19I20L/55,61#H'@ ='&,6B System.out.println(s1) .(%$19I20L/5 toString() -7B/&H6C2/@'0 %B :;" 3#,1?name1#,+0'<#2@<&56W##,-LE//1 '6./>'-5,6<&569"(M: %B
Qt
("' .!$4D,@/$H'6C/6#'& %B0% 0L/5<++=-800/JJ87 %BI7B/toString() public class Student { private String name; public Student(String n) { name = n; } public String toString() { return name; } } ------------------------------------------------------public class TestToString { public static void main(String args[]) { Student s1 = new Student("Thana"); System.out.println(s1); } }
.!-W##,-LE %BG52.'11'((,"!:(<1(0 %B
Qz
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
44 G0equals() 0L/5equals():;"0L/59"#'& %BI7B/Object -7B/:(%$+ %$+@H'//:.K"%8 0%H' H'1,+//:.KC/6 %B&H6WH'"0' '6 4(7/G0H#'& %B.&'0'(*(%$19I20L/5"%8 G52.D2/61='4"50L/5<++ VV&='4(,+0L/5"%8/$MH9"#'&",8"52@$!:(<1(0 %B
Qs <&56D,@/$H'6C/6#'& Student %B0% 0L/5<++ VV %BI7B/ equals() -7B/ 1='4"5@AL%1'(:(%$+ $% +//:.K !5$0L/5"%8.942H':;" true *2'//:.K &%B H6WH'"0' '6 0%H'C/63#,1? name H'1,+H'C/63#,1? name C/6//:.K"%8 !:(<1(0"%8.942W##,-LE5,6<&569"(M: %B
Q
("' .!$5 D,@/$H'6C/6#'& %B0% 0L/5<++=-800/JJ87 %BI7B/equals() public class Student { private String name; public Student(String n) { name = n; } public boolean equals(Object obj) { if (obj.equals(name)) { return true; } else { return false; } } } ------------------------------------------------------public class TestEquals { public static void main(String args[]) { Student s1 = new Student("Thana"); Student s2 = new Student("Thana"); System.out.println(s1.equals(s2)); } }
.!. W##,-LE %BG52.'11'((,"!:(<1(0 %B
Qs
4% #qb9aaZb #'&:(> ^ :;"#'& %B9I29"1'(&(2'6//:.K 0%B % 3#,1?&/5#2/6 1,+I"A5C2/0M#<++-78"l'" ,86<:5I"A5 !5$>'?'.'@'G521='4"5#'& %B/$MH9"<-1K. java.lang %B:;"#'&:(> ^ G@2<:5#'& 5,6<&569"D'('6 %B
Q #'& :(> ^ .IH@$9"1'(&(2'6//:.K %B 1K+I"A5C2/0M#<++-78"l'"G@29"/##K1I,B" <++U !:(<1(0 %B
Qcm <&56D,@/$H'61'(9I2#'&:(> ^ RSB69"D,@/$H'6"%8G52 &(2'6//:.KC/6#'&:(> ^ -7B/1K+I"A5C2/0M#<++-78"l'"&'0H'7/ true, ‘A’<#1.234<#G52-A0-EH'C/6//:.K ,86&'0//10'!5$.G52W##,-LE5,6<&569"(M: %B
Qv 4 #'&:(> k0,BB80 )0 #
) #
boolean
Boolean
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .!%6 D,@/$H'61'(9I2#'&:(> k0,BB80 public class ShowWrapper { public static void main(String args[]) { Boolean b = new Boolean(true); Character c = new Character('A'); Double d = new Double(1.234); System.out.println(b); System.out.println(c); System.out.println(d); } }
.!3 W##,-LE %BG52.'11'((,"!:(<1(0 %B
Qcm
48 +0 C A >'?'.'@'G521='4"5%$E @A(E5 %B 1%B$@C2/61,+#'& 0L/5<#3#,1? %B&=',nG@2 &/6D,@7/ static <# final %$E@A(E5 static :;"%$E@A(E5-7B/(+3@H'D,@<:(4(7/ H'6 %B95:;"3#,1?C/6#'&4(7/0L/595:;"0L/5C/6#'& &H@"%$E @A(E5 final .9I29"#'& <#0L/5-7B/1='4"5G0H9420%1'(&7+ /5 4(7/9I29"3#,1?-7B/1='4"5942 :;"H'6 %B
48 /@#'@"static 3#,1?C/6#'&.:;"3#,1? %B9I2(@H 01,"9" 31//:.K <#&'0'(* (%$19I26'"G52!5$G0H.=':;"D2/6&(2'6//:.KC/6#'&CS8"0'1H/" D,@<:(4(7/H'6 %B %B :;" 3#,1?C/6#'&7/D,@<:(4(7/H'6 %BRSB60% V :;" static 3#,1?C/6 #'&"%8.:;"D,@<:(4(7/H'6 %BRSB69I2(H@01,"9" 31//:.K !5$.
H'6 %B %B :;"3#,1?C/6//:.KRSB6/'.0%H'
.!4 W##,-LE %BG52.'11'((,"!:(<1(0 %B
QcP
48 G0"static 0L/5<++ static 7/0L/5 %B0% V :;" static !5$ @,B G:('. :(1')0L/595F942:;"0L/5<++ static 9"1(% %B 0L/5",8":;"0L/5 %B9I26'" ,B@G:
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
Y kV ('&'0'(* %B.(%$19I20L/5<++ static G52!5$(%$1WH'"I7B/#'& IH"5%$@1,+3#,1?C/6#'&RSB60%(M:<++1'(9I26'"5,6"%8
ClassName.methodName()
!5$ %B x
className7/I7B/C/6#'&
x methodName7/I7B/C/60L/5<++static D,@/$H'6C/60L/5<++ static 7/0L/5 310L/59"#'& Math %B/$MH9" NO ,86"%8"7B/6.'10L/54#H'"%.8 ,5@H':;"0L/5&='4(,+9I26'" ,B@G:.S61='4"5942:;" 0L/5<++static ='942('&'0'(* .%B (%$19I20L/54#H'"%8G52!5$9I2I7B/#'&IH"='&,6B Math.sqrt(4); :;"='&,B6='"@4'H'('1 %B&/6C/6u:;"D2" 0L/5 I%B 7B/ main():;"/%1D,@/$H'64"SB6C/60L/5<++ static ,86"%8 "7B/6.'1 >'?'.'@'G0HD2/61'(942 pk D2/6&(2'6//:.KC/6#'& 0%B %0L/5 main() CS8"0'1H/" -7B/(,"0L/5main().S6G521='4"5942main():;"0L/5<++static >'?'.'@'G521='4"567B/"GCC/60L/5<++static %B&=',nG@25,6"%8 x 0L/5<++ static .G0H&'0'(*(%$13#,1?4(7/0L/5 %BG0HG52:;" <++staticb7=7K1,1/5eG52 x 0L/5<++static.G0H&'0'(*0%0L/5<++=-800/JJ87G52
48" OC>CAGYIACA>EAuJB \X O 7/+#K/19"#'&95F %B/$MH"/10L/5 <#0%%$E @A(E5 static -7B/"A$'0942:;"+#K/1<++static!5$0%(M:<++5,6"%8 static { ... }
!:(<1(0>'?'.'@'.(,"='&,B6 %B/$MH9"+#K/1<++static-%$6(,865%$@07B/pk !4#5#'& %B0%+#K/15,61#H'@CS8"0'\XO .9I29"1'(5%+,1Vd !:(<1(04(7/ 9I29"1(% %BD2/61'(&(2'6//:.KC/6#'&CS8"!5$/,D!"0,DA ('&'0'(* .%B 1='4"5+#K/1<++
staticG520'11@H'4"SB6+#K/19"#'&
'= 6'"C/6!:(<1(0.(%$1='&,B6 9"+#K/1<++ static .'1+"#6#H'6 !:(<1(0 %B
Qc <&56D,@/$H'6C/6#'& 0%B % \X O RSB6!:(<1(0"%8.942W##,-LE5,6<&569"(M: %B
Qs ("' .!%$ D,@/$H'6C/6#'& %B0% >1,1/5Q7/1/,3/r80 public class TestStaticBlock { static int x=5; static { x += 1; } public static void main(String args[]) { System.out.println("x = "+x); } static { x /= 2; } }
.!5W##,-LE G%B 52.'11'((,"!:(<1(0 %B
Qc
48& +0final %$E@A(E5 final &'0'(* %B.9I2:;" V &='4(,+#'& D,@<:( <#0L/5 !5$ %B x #'& %B0% ;=J/?/80 :;" final .:;"#'& %BG0H&'0'(*942#'&/7B"&7+ /5G52 D,@/$H'6IH" #'& String %B1='4"59" +,-, PHQ :;"#'&<++ final RSB60% (M:<++='&,B6:(1')#'&5,6"%8 public final class String { ... }
5,6",8"('.S6G0H&'0'(* %B.1='4"5942#'&95F&7+ /5#'&StringG52
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x 0L/5 0%B % ;=J/?/80 :;" final 7/0L/5 .%B G0H&'0'(*0%0L/5<++ =-800/JJ87 G52 D,@/$H'6IH"*2'#'& Student 1='4"59420L/5 showDetails()0%;=J/?/80:;"final5,6"%8 public class Student{ ... public final void showDetails(){ ... } }
5,6",8"*2'#'& GradStudent &7+ /50'.'1#'& Student .G0H&'0'(*0% 0L/5<++ VV %BI7B/showDetails()G52 x D,@<:( %B0% ;=J/?/80 :;" final 7/H'6 %B H'6 .%B &'0'(*1='4"5H'G52-%$6 (,865%$@ !5$.D2/60%1'(1='4"5H'1H/" %B.0%1'((%$19I26'" RSB607B/1='4"5H' <#2@.G0H&'0'(*:#%$B "<:#6H'G52 *2'H'6 %B",8":;"3#,1?C/6//:.K (' .D2/61='4"5H'9"='&,6B :(1')D,@<:( 4(7/9I2='&,B61='4"5H'>'$9" 5=7K10251=0 C/6#'& '$9"0L/5 ('.D2/61='4"5H'9"='&,B6 :(1')D,@<:( 4(7/9I2='&,B61='4"5H'>'$9"0L/51H/" %B.0%1'((%$19I26'" H'6 %B",8"
4! #"abstract"#2 >'?'.'@'1='4"5942('&'0'(* %B."A$'00L/5DH'6F>'$9"#'& CS8"0'1H/"G52 !5$$,6G0HD2/61='4"5+#K/1='&,6B DH'6F>'$9"0L/54#H'",8" '$9"#'&/7B"F .%B &7+ /5 @AL%1'("%8.IH@$ ='942('&'0'(*.1='4"50L/5 %BG0H &'0'(*"A$'0='&,6B G@2#H@64"2'G52 !5$>'?'.'@'G521='4"5#',1?"%8G@2&/6<++7/ #'&<++abstract <#/A"D/(EJ&
4! #"abstract #'& %B0% V :;" abstract .40'$@'0@H'#'&",8"$,6:;"#'& %BG0H &0+M(E !5$0%0L/5<++ abstractRSB6:;"0L/5 $%B ,6G0H&0+M(E/$H'6"2/$4"SB60L/5/$MH 9"#'&0L/5<++abstract.0%(M:<++1'(:(1')5,6"%8 [modifier] abstract return_type methodName([arguments]);
86, "%80L/5<++ abstract :;"0L/5 %B$6, G0H&0+M(E "7B/6.'1G0H0+% #K/1='&,6B /$MH>'$9"0L/5 !:(<1(0 %B
Qcc<&56D,@/$H'6C/6#'& Student %B:;"#'&<++ abstract #'&"%8.0%0L/5showDetails()RSB6:;"0L/5<++abstract/$MH>'$9"#'& ("' .!%%D,@/$H'6#'&<++abstract
public abstract class Student { protected String id; protected String name; protected double gpa; public void setID(String ID) { id = ID; } void setName(String n) { public name = n; } public void setGPA(double GPA) { gpa = GPA; } public abstract void showDetails(); }
#'&<++ abstract1='4"5CS8"0'-7B/942#'&/7B"&7+ /5!5$#'& %B0'&7+ /5 .D2/61='4"5+#K/1='&,6B 9"0L/5 %B$,6G0H&0+M(E ,86"%8 ('.G0H&'0'(*.&(2'6//:.KC/6 #'&<++abstractG52!:(<1(0 %B
Qcu<&56D,@/$H'6C/6#'&FullTimeStudent %B&7+ /50'.'1#'& Student #'&"%8.D2/60%1'(1='4"5+#K/1='&,B69"0L/5 showDetails() -7B/ ='942:;"0L/5 & %B 0+M(ERSB6. ='942#'&FullTimeStudent :;"#'& &%B '0'(*9I29"1'(&(2'6//:.KG52 /"SB6*S6<02@H'('.G0H&'0'(*&(2'6//:.KC/6 #'& Student G52
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .!%,D,@/$H'6#'& &%B 7+ /50'.'1#'&<++abstract
public class FullTimeStudent extends Student { private int credit; private final int MAX_YEAR = 4; public FullTimeStudent(int c) { credit = c; } public void showDetails() { System.out.println("ID: "+id); System.out.println("Name: "+name); System.out.println("GPA: "+gpa); System.out.println("Credit: "+credit); } }
4! 2 /A"D/(EJ& X 0%#,1?#2'$1,+#'&<++ :(1/+52@$0L/5 $%B ,6G0H&0+M(E H'",8"!5$0%(:M <++5,6"%8
abstract
[modifier] interface InterfaceName { [methods();] }
!5$ %B x
InterfaceName7/I7B/C/6/A"D/(EJ&
x
methods() :;"0L/5
%B$6, G0H&0+M(E IH"5%$@1,+0L/5<++ abstract
("' .!%-D,@/$H'6/A"D/(EJ&
public interface Student { public void setID(String ID); public void setName(String n); public void setGPA(double GPA); public void showDetails(); }
/A"D/(EJ&1='4"5CS8"0'-7B/942#'&/7B""='G:9I26'"!5$9I2%$E @A(E5 implements !5$0%(:M <++5,6"%8 [modifier] class ClassName implements InterfaceName { [methods();] }
!:(<1(0 %B
Qc
G52<&56D,@/$H'6C/6#'& PartTimeStudent %B implements /A"D/(EJ& Student #'& %B implements /A"D/(EJ&.D2/6C%$" +#K/1='&,B69"0L/5 310L/5 %B1='4"5G@29"/A"D/(EJ& -7B/ ='9420L/54#H'"%&8 0+M(E <#&'0'(*&(2'6//:.KC/6#'&",8"G52 /A"D/(EJ&.407/"1,+#'&<++ abstract D(6 %B ('.G0H&'0'(*&(2'6 //:.KC/6/A"D/(EJ&G52 :(!$I"EC/6/A"D/(EJ&1K7/1'(1='4"5(M:<++C/60L/5 DH'6F %B#'&/7B"F.D2/6 implements G@2#H@64"2' RSB6&'0'(*/'),$4#,11'(C/61'(0%G52 4#'$(M:<++0'(%$19I20L/54#H'",8"G52.'1#'& %B implements /A"D/(EJ& /' AIH" ='&,B6 Student s1 = new PartTimeStudent(6); s1.setID(“1111”); s1.setName(“Thana”); s1.setGPA(3.00); s1.showDetails();
/A"D/(EJ&$,6IH@$9"1'(&7+ /5<++4#'$#'&G52 k O X "7B/6.'1>'?'.'@'1='4"5942#'&95F&'0'(*&7+ /5#'&/7"B G52-%$6#'&5%$@ H'",8"
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
... }
:;"!:(<1(0.'@'
public class PartTimeStudent implements Student { private String id; private String name; private double gpa; private int credit; private final int MAX_YEAR = 8; public PartTimeStudent(int c) { credit = c; } public void setID(String ID) { id = ID; } public void setName(String n) { name = n; } public void setGPA(double GPA) { gpa = GPA; } public void showDetails() { System.out.println(“ID: “+id); System.out.println(“Name: “+name); System.out.println(“GPA: “+gpa); System.out.println(“Credit: “+credit); } }
4< #+D #'&>'$9" O X 7/#'& %B:(1')/$MH>'$9"#'&/7B"F RS6B +'6(,68 (%$1@H'#'&<++R2/" V X #'&>'$9"/"3n'D942:(1')3#,1?4(7/ 0L/5>'$9"#'&/7B"G52 #'&>'$9"0%:(!$I"E9"1(% %BD2/61'(..,51#30H C/6#'& D%B 2/6
'= 6'"(H@01," !5$D2/61'(@+30G0H9420%1'(C2'*S6!5$D(6.'1#'&/7B"F <#D2/61'( (%$19I23#,1?4(7/0L/5C/6#'& %B/$M>H '$"/1G52!5$D(6 >'?'.'@'<+H6#'&>'$9"//1:;"&/6<++7/ PQ #'&>'$9" %B/$M>H '$9"#'&!5$D(6 Q #'&>'$9" %B/$M>H '$9"0L/5
4< #+D + 1+D # 1(%"%8:;"1'(:(1')#'&>'$9"#'&/7B" %B (%$1@H'#'&>'$"/1[ X !:(<1(0 %B
Qcv :;"D,@/$H'6C/61'(:(1')#'&>'$9" I%B 7B/ Inner RSB6*M1:(1')/$MH >'$9"#'& I%B 7B/ Outer #'&>'$9".'$9"0% XX V :;" public, private, V 4(7/ protected 1KG52 C %B#'& ,B@FG:.1='4"5942:;"G52|-' public 4(7/ V H'",8""/1.'1"%81'(&(2'6//:.KC/6#'&>'$9"$,60%C2/'$9""/1#'&>'$"/1 .D2/6 ='1'(&(2'6 //:.KC/6#'&>'$"/11H/" <#[email protected]&(2'6//:.KC/6#'&>'$9"G52 D,@/$H'6IH"='&,B6 Outer.Inner in2 = new Outer().new Inner(); 9"!:(<1(0 %B
Qcv :;"D,@/$H'61'(&(2'6//:.KC/6#'&>'$9" %BI7B/ in2 1(%"%8.G0H&'0'(*&(2'6//:.KC/6#'&>'$9"G52 *2'#'&>'$9"0% V :;"private) Q 1'(&(2'6//:.K>'$9"0L/5 %B/$MH9"#'&>'$"/1&'0'(* '= G52!5$D(6 IH" 5%$@1,+1'(&(2'6//:.K ,B@FG: D,@/$H'6IH"*2'0L/5 methodX() :;" 0L/59"#'&>'$"/1 <#D2/61'(.&(2'6//:.KC/6#'&>'$9" (' &'0'(* ='G525,6"%8 public void methodX() { ... Inner in1 = new Inner(); }
#'& %B/$MH>'$9"&'0'(* .%B (%$19I23#,1? <#0L/5C/6#'&>'$"/1G52 !:(<1(0 %B
Qcv <&56D,@/$H'6C/61'((%$19I2<#:(1')D,@<:(C/6#'&>'$9"<# >'$"/1D,@/$H'6"%.8 0%D,@<:(I7B/5%$@1,"RSB60%C/+CD %BDH'61,"
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
("' .!%3 D,@/$H'6<&56#'&>'$9" %B/$MH>'$9"#'& public class Outer { public void method1() { Inner in1 = new Inner(); in1.method2(); } public class Inner { public void method2() { System.out.println("Inner Demo"); } } } ------------------------------------------------------public class InnerDemo { public static void main(String args[]) { Outer.Inner in2 = new Outer().new Inner(); in2.method2(); } }
4< #+D + 1+D G0 1(%"%8:;"1'(:(1')#'&>'$9" %B/$MH>'$9"0L/5C/6#'&>'$"/15,6D,@/$H'6 %B<&569"!:(<1(0 %B
Qcz#'&>'$9":(> "%8.0%XXV :;"V ('.G0H &'0'(* .%B &(2'6//:.KC/6#'&>'$9":(> "%8"/10L/5 %B:(1')#'&G52 <#. &'0'(*(%$19I2D@, <:(>'$9"C/60L/5G529"1(% %BD,@<:(",8":(1'):;" final H'",8" &H@"D,@<:( %B :;"3#,1?C/6#'&4(7/3#,1?C/6//:.K &'0'(* %B.(%$19I2G52 IH"5%$@1,+#'&>'$9" %B/$MH>'$9"#'& #'&>'$9" ,86&/6:(> $,60%3&0+,DA/7B"F %B&=',n5,6"%8 x #'&>'$9"/'.:;"#'&<++abstract4(7//A"D/(EJ&G52 x #'&>'$9" %B/$M>H '$9"#'&>'$"/1!5$D(6*2'0% ;=J/?/80:;" static. 1#'$:;"#'&:1DA <#&'0'(*&(2'6//:.K!5$1'((%$1I7B/C/6#'& >'$"/1G525,6"%8
Outer.Inner in3 = Outer.new Inner();
x #'&>'$9"G0H&'0'(* :%B (1')9420%3#,1?4(7/0L/5:;"<++ G52@2"'$9".:;"#'&<++static
static
x #'&>'$9"&'0'(* %B.9I2D,@<:( %B:;"3#,1?C/6#'&4(7/3#,1? C/6//:.KC/6#'&>'$"/1G52 ("' .!%4 #'&>'$9" %B/$MH>'$9"0L/5 public class MOuter { private int a = 1; public void method(final int final int c = 2; d = 3; int class Inner { private void iMethod()
b) {
{ System.out.println("a System.out.println("b System.out.println("c System.out.println("d
= = = =
"+a); "+b); "+c); "+d);
//illegal
} } } }
4 " '* <-1K..:;" %B(@+(@0#'&<#/A"D/(EJ&DH'6F 0%B %4"2' 1%B '( ='6'"#2'$1,"G@2 9" %B 5%$@1,"!5$ ,B@G:!:(<1(0>'?'.'@'.:(1/+G:52@$#'&4#'$#'& %B:(1')/$MH 9"!:(<1(0GJ#EDH'6F GJ#E!:(<1(0>'?'.'@'
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
<-1K.$H/$ c <-1K.7/
!5$!:(<1(0 Student.class, Teacher.class <# Subject.class ./$M9H "<-1K.$H/$ domain&H@"!:(<1(0Report.class ./$M9H "<-1K.$H/$reports domain,
GUI
<#
reports
.! 6 D,@/$H'6<-1K.
4 ''f"#D)" '* !(6&(2'6!:(<1(0GJ#EC/6>'?'.'@'.:(1/+52@$='&,B65,6"%8
[<package_declaration>] [] []
!5$ %B
x package_declaration:;"1'((+3@H'!:(<1(0GJ#E/$MH9"<-1K.95
x import_declaration:;"='&,B69"1'((%$19I2<-1K./7B"F
x class_declaration:;"='&,B69"1'(:(1')#'&
='&,B69"1'((+3<-1K..0%(M:<++5,6"%8
package <package_name>[<sub_package_name>]
D,@/$H'6IH" package faculty.domain; 4(7/ package faculty; !:(<1(0GJ#E4"SB6GJ#E.0%='&,6B packageG52-%$6='&,6B 5%$@!5$.D2/6:;" ='&,B6<(1C/6!:(<1(09"1(% %BG0H0%='&,B6package!:(<1(0GJ#E",8".*M11='4"5G@29" <-1K.V 1'((%$19I2<-1K./7B"F. ='G52!5$1'(9I2='&,B6import RSB60%(M:<++C/6='&,6B 5,6"%8
import <package_name>[.<sub_package_name>].
4(7/ import <package_name>[.<sub_package_name>].*;
D,@/$H'6IH" import faculty.reports.Report;
4(7/ import java.awt.*; ='&,B6 import .D2/61='4"5G@21H/"='&,B61'(:(1')#'& RSB6!:(<1(0GJ#E >'?'.'@'
/& x #'&.:(1/+G:52@$3#,1?<#0L/5 x 1'(C%$"!:(<1(0IA6//:.K:;"1'(<+H6!:(<1(0//1:;"!05M#$H/$F ='942 &'0'(*:(,+:#%B$"<12GC!:(<1(0G526H'$CS8" x 0L/5 %B1='4"5CS8"9"#'&95F&'0'(*(%$19I26'"G52&/6(M:<++7/ 1'((%$1 9I26'".'1#'& %BDH'61,"<#1'((%$19I26'">'$9"#'&5%$@1," x 0L/5 %B1='4"5CS8"9"#'&/'.0% %B(,+H'-7B/"='G:9I29"0L/5
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x 0L/595FC/6#'&&'0'(* .%B 0%H' &%B H61#,+0'G52 .D2/6(+3I"A5C2/0M#C/6H' .%B &H61#,+
,86"%81'(:(1')0L/5
x 0L/5&'0'(*<+H6:;"0L/5C/6//:.K 4(7/(%$1@H'0L/5<++ qX <#0L/5C/6#'&4(7/(%$1@H'0L/5<++static x 0L/5C/6#'&95 %B0% ;=J/?/80 :;"<++ static . ='942&'0'(**M1(%$19I2 6'"!5$9I2I7B/C/6#'&",8"G52#$ x 0L/5<++ VV7/0%0L/5 0%B %I7B/5%$@1,"'?'.'@'.9I2%$E @A(E5extends x %$E@A(E5 protected . ='942#'& %B :;" K2f53,KK &'0'(* %B./2'6/A6*S6 3#,1?4(7/0L/5C/6K2B8053,KKG52 x #'& 31#'&9">'?'.'@'*2'G0HG52&+7 /5.'1#'&95#$ .*7/@H'#'&",8" &7+ /5.'1#'& %BI7B/Object x
super:;"%$E@A(E5
9%B I29"1'(/2'6/A6*S6K2B8053,KK-7B/ %B.(%$19I20L/54(7/ 5=7K10251=0C/6K2B8053,KK
x 1'(0%G524#'$(M:<++40'$*S6 1'( %B('&'0'(* .%B &(2'64(7//2'6*S6//:.KC/6#'& &%B 7+ /51,"G52 4#'$(M:<++ 1'( %B('&'0'(* %B./2'6*S60L/5I7B/5%$@1,"G524#'$(M:<++
x 0L/5<++ =-803=,J8J 40'$*S60L/5 %B0I% 7B/5%$@1,"0'11@H'4"SB60L/5 !5$ 0%.='"@"4(7/I"A5C/6,0^2;871 %B
9I29"1'(<:#6H'C/6//:.K942:;"C2/0M#I"A5 String &H@"0L/5equals()9I29"1'(:(%$+ %$+H'C/6//:.K toString()
x #'&:(> k0,BB80.IH@$9"1'(&(2'6//:.K %B 1K+I"A5C2/0M#<++-78"l'" G@29"/##K1I,B"<++q8180=^878=2K
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6
x !5$:1DA('.:(1')0L/595F942:;"0L/5<++ static 9"1(% %B0L/5 ",8":;"0L/5 9%B I26'" ,B@G: <#0L/5<++ static &'0'(**M1(%$19I2.'1 I7B/C/6#'&G52!5$G0H.=':;"D2/6&(2'6//:.K x #'& %B0% V :;" final . ='942#'&/7B"G0H&'0'(*&7+ /5#'&"%8G52 <#0L/5 0%B % V :;" final 7/0L/5 %B.G0H&'0'(*0%0L/5<++ VV G52 &H@"D,@<:( 0%B % V :;" final 7/H'6 %B RSB6. ='942 &'0'(*1='4"5H'G52-%$6(,865%$@ H'",8" x #'& %B0% V :;" abstract 40'$@'0@H'#'&","8 $,6:;"#'& %BG0H &0+M(E !5$0%0L/5<++ abstract RSB6:;"0L/5 %B$,6G0H&0+M(E/$H'6"2/$ 4"SB60L/5/$MH9"#'& x /A"D/(EJ&0%#,1?#2'$1,+#'&<++ abstract'$9"40'$*S6#'& %B/$M>H '$9"#'&4(7/#'& %B/$MH>'$9"0L/5 x ='&,B6package:;"1'((+3@'H #'&/$MH9"<-1K.95 x ='&,B6
import:;"1'((%$19I2#'&9"<-1K.DH'6F
!"!#$%&'(&" )&*'+," !"!#$%-(./01#2'.2'3 4'(#'51(+,6