Skip to content

Commit 1e1ee72

Browse files
I have written a code to show the implementation of abstract class in java
1 parent 75fc7e3 commit 1e1ee72

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Abstract_Class_Example.java

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//Subscribed by Mritunjay Kumar
2+
//https://www.facebook.com/Mritunjay70/posts/1445812775628125
3+
abstract class Person {
4+
String name;
5+
int age;
6+
7+
Person() {
8+
9+
}
10+
Person(String n, int r) {
11+
name = n;
12+
age = r;
13+
}
14+
15+
void setData(String name1, int age1)
16+
{
17+
name = name1;
18+
age = age1;
19+
}
20+
21+
void printDetails()
22+
{
23+
System.out.print("Name: "+name+" Age: "+age);
24+
}
25+
26+
abstract void exceptional();
27+
28+
final void isAdult() {
29+
if (age > 18) System.out.println("\t Yes");
30+
}
31+
} // class Person ends
32+
33+
class Student extends Person
34+
{
35+
double cpi;
36+
37+
Student() {
38+
39+
}
40+
Student(String n, int r, double c) {
41+
42+
super(n, r);
43+
cpi = c;
44+
}
45+
46+
void setData(String name1, int age1, double cpi1)
47+
{
48+
super.setData(name1, age1);
49+
cpi = cpi1;
50+
}
51+
52+
void printDetails()
53+
{
54+
System.out.println();
55+
super.printDetails();
56+
System.out.print(" CPI: "+cpi);
57+
}
58+
59+
void f() {
60+
61+
}
62+
63+
void exceptional()
64+
{
65+
if (cpi > 9.5) System.out.print(" Exceptional ");
66+
}
67+
68+
void isAdult(Person p) {
69+
70+
}
71+
} // class Student ends here
72+
73+
abstract class test extends Student {
74+
abstract void f();
75+
void f2(){
76+
77+
}
78+
}
79+
80+
class Faculty extends Person
81+
{
82+
float noOfPub;
83+
84+
Faculty() {
85+
86+
}
87+
Faculty(String n, int r, float nop) {
88+
super(n, r);
89+
noOfPub = nop;
90+
}
91+
92+
void setData(String name1, int age1, int nop)
93+
{
94+
super.setData(name1, age1);
95+
noOfPub = nop;
96+
}
97+
98+
void printDetails()
99+
{
100+
System.out.println();
101+
super.printDetails();
102+
System.out.print(" No of Pub: "+noOfPub);
103+
}
104+
105+
void exceptional()
106+
{
107+
if (noOfPub > 100) System.out.print(" Exceptional ");
108+
}
109+
}
110+
111+
public class AbstractDemo {
112+
113+
public static void main(String[] args)
114+
{
115+
Student s1 = new Student(), s2 = new Student();
116+
Faculty f1 = new Faculty(), f2 = new Faculty();
117+
118+
s1.setData("A", 10, 9.7); s2.setData("B", 10, 7.0);
119+
f1.setData("C", 70, 300); f2.setData("D", 75, 50);
120+
121+
Person [] p= new Person[4];
122+
123+
p[0] = s1;
124+
p[1] = s2;
125+
p[2] = f1;
126+
p[3] = f2;
127+
128+
for (int index=0; index<p.length; index++)
129+
{
130+
p[index].printDetails();
131+
p[index].exceptional();
132+
p[index].isAdult();
133+
}
134+
135+
}
136+
137+
}

0 commit comments

Comments
 (0)