0% found this document useful (0 votes)
38 views6 pages

Swapnanil Dutta 12 OOP Lab

Abstract.java:5: error: Abstract is not abstract and does not override abstract method method() in Base public class Abstract extends Base { ^ 1 error So in summary, if a subclass extends an abstract class, it must either: 1. Be declared abstract itself if it does not implement all abstract methods in the parent abstract class OR 2. Implement all abstract methods declared in the parent abstract class. It is not allowed to only implement some but not all abstract methods.

Uploaded by

Diana Hartan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Swapnanil Dutta 12 OOP Lab

Abstract.java:5: error: Abstract is not abstract and does not override abstract method method() in Base public class Abstract extends Base { ^ 1 error So in summary, if a subclass extends an abstract class, it must either: 1. Be declared abstract itself if it does not implement all abstract methods in the parent abstract class OR 2. Implement all abstract methods declared in the parent abstract class. It is not allowed to only implement some but not all abstract methods.

Uploaded by

Diana Hartan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Swapnanil Dutta Roll:12

Question:​ ​Create a class Employee is having instance variables name and id. Create its
subclass named Scientist which has an instance variables no_of_publication and experience.
Now create its subclass, say DScientist which has instance variable award. Put a method like:
public String toString(){ }
in every class where you describe the class and from the main() method create an object of
each class and print each object.

Code:
class​ ​Employee​ {
​int​ id;
String name;
​Employee​(​int​ ​id​, String ​name​) {
​this​.id ​=​ id;
​this​.name ​=​ name;
}
​public​ String ​toString​() {
​return​ ​"Id = "​ ​+​ ​this​.id ​+​ ​"​\n​Name = "​ ​+​ ​this​.name ​+​ ​"​\n​"​;
}
}

class​ ​Scientist​ ​extends​ ​Employee​ {


​ nt​ no_of_publication;
i
String experience;
​ cientist​ (​int​ ​id​, String ​name​, ​int​ ​no_of_publication​, String
S
experience​) {
​super​(id, name);
​this​.no_of_publication ​=​ no_of_publication;
​this​.experience ​=​ experience;
}
​public​ String ​toString​() {
​return​ ​super​.​toString​() ​+​ ​"Publications = "​ ​+
this​.no_of_publication ​+​ ​"​\n​Experience = "​ ​+​ ​this​.experience ​+​ ​"​\n​"​;
}
}

public​ ​class​ ​DScientist​ ​extends​ ​Scientist​ {


String award;
​ Scientist​(​int​ i
D ​ d​, String ​name​, ​int​ ​no_of_publication​, String
experience​, String a ​ ward​) {
​super​(id, name, no_of_publication, experience);
​this​.award ​=​ award;
}
​public​ String ​toString​() {
​return​ ​super​.​toString​() ​+​ ​"Awards = "​ ​+​ ​this​.award;
}
​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
Employee em ​=​ ​new​ ​Employee​(​712541​, ​"Swagato Patra"​);
Scientist st ​=​ ​new​ ​Scientist​(​712454​, ​"Abhishek Pal"​, ​2​, ​"Junior
Scientist"​);
DScientist ds ​=​ ​new​ ​DScientist​(​712136​, ​"Swapnanil Dutta"​, ​5​,
"Senior Scientist"​, ​"Employee of the month"​);

System.out.​println​(​"----------------------Details-------------------------
-"​);
System.out.​println​(em​+​"​\n​"​);
System.out.​println​(st​+​"​\n​"​);
System.out.​println​(ds​+​"​\n​"​);
}
}

Output:
Question:​ ​Create a class with a method ​void show()​and make three subclasses of it and
all subclasses have this show() method overridden and call those methods using their
corresponding object references.
Code:
class​ ​Grandparent​ {
​void​ ​show​() {
System.out.​println​(​"Class GrandParent"​);
}
}

class​ ​Parent​ ​extends​ ​Grandparent​ {


@​Override
​void​ ​show​() {
System.out.​println​(​"Class Parent"​);
}
}

class​ ​Child​ ​extends​ ​Parent​ {


@​Override
​void​ ​show​() {
System.out.​println​(​"Class Child"​);
}
}

public​ ​class​ ​Qstn4​{


​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
Grandparent a ​=​ ​new​ ​Grandparent​();
a.​show​();
Parent b ​=​ ​new​ ​Parent​();
b.​show​();
Child c ​=​ ​new​ ​Child​();
c.​show​();
}
}
Output:
Question:​ ​Do the problem 4 using dynamic method dispatching.

Code:
class​ ​Grandparent​ {
​void​ ​show​() {
System.out.​println​(​"Class GrandParent"​);
}
}

class​ ​Parent​ ​extends​ ​Grandparent​ {


@​Override
​void​ ​show​() {
System.out.​println​(​"Class Parent"​);
}
}

class​ ​Child​ ​extends​ ​Parent​ {


@​Override
​void​ ​show​() {
System.out.​println​(​"Class Child"​);
}
}

public​ ​class​ ​DynamicMethodDispatching​ {


​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
Grandparent obj;
obj ​=​ ​new​ ​Grandparent​();
obj.​show​();
obj ​=​ ​new​ ​Parent​();
obj.​show​();
obj ​=​ ​new​ ​Child​();
obj.​show​();
}
}

Output:
Question:​ ​Check without having any abstract method/s whether a class can be abstract; if so,
then use that concrete method(s) from another class having the main method.

Code:
abstract​ ​class​ ​Base​ {
​public​ ​static​ ​void​ ​show​() {
System.out.​println​(​"Base abstract class"​);
}
}

public​ ​class​ ​ConcreteAbstract​ ​extends​ ​Base​ {


​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
​show​();
}
}

Output:
Question:​ ​Create an abstract class with three abstract methods check whether you can we
override only a few methods (not all methods) in subclass or not.

Code:
abstract​ ​class​ ​Base​ {
​abstract​ String ​method​();
}

public​ ​class​ ​Abstract​ ​extends​ ​Base​ {


​public​ ​static​ ​void​ ​main​(​String​[] ​args​) {
System.out.​println​(​"Hello World"​);
}
}

Output:

You might also like