Skip to content

Instantly share code, notes, and snippets.

@ucguy4u
Last active December 8, 2020 05:04
Show Gist options
  • Save ucguy4u/2f025f7f4c6663b5033b11a93f296654 to your computer and use it in GitHub Desktop.
Save ucguy4u/2f025f7f4c6663b5033b11a93f296654 to your computer and use it in GitHub Desktop.
eg: Inheritance in Java
class Teacher {
String designation = "Teacher";
String collegeName = "CMU";
void does() {
System.out.println("teaching");
}
}
public class MathsTeacher extends Teacher {
String mainSubject = "Maths";
public static void main(String[] arg) {
MathsTeacher mt = new MathsTeacher();
System.out.println(mt.collegeName);
System.out.println(mt.designation);
System.out.println(mt.mainSubject);
mt.does();
}
}
/**
* OUTPUT:
* CMU
* Teacher
* Maths
* teaching
* <p>
* Based on the example, the MathsTeacher class is a child class of Teacher.
* The child class MathsTeacher forms a IS-A relationship with the parent class Teacher.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment