+ 2

What is the use of inheritance injava but I dont understand how it is inheritance

class Teacher { String designation = "Teacher"; String collegeName = "Beginnersbook"; void does() { System.out.println("Teaching"); } } public class PhysicsTeacher extendsTeacher { String mainSubject = "Physics"; public static void main(String args[]) { PhysicsTeacher obj = new PhysicsTeacher(); System.out.println(obj.collegeName); System.out.println(obj.designation); System.out.println(obj.mainSubject); obj.does(); } }

15th Sep 2019, 2:07 PM
Kande Likhitha
Kande Likhitha - avatar
1 Odpowiedź
+ 1
The main point is that you want to write things only once (DRY = don't repeat yourself!). When your program deals with different teachers, you would have a lot of similar objects. It is smart to store their common traits in only one point of the program. This is what you do with using a class Teacher, which you use as a template when you use extends while you define different types of teachers. hope it helps.
15th Sep 2019, 7:15 PM
Nonea
Nonea - avatar