InheritanceAndPolymorphism

InheritanceAndPolymorphism - CIS3023 Programming...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2nd June 2010 “ We are all gifted. That is our inheritance. ” --Ethel Waters Ganesh
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Inheritance Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. A class that is derived from another class is called a subclass (also a derived class , extended class , or child class ). The class from which the subclass is derived is called a superclass (also a base class or a parent class ).
Image of page 2
Inheritance It allows us to define a general class and then specialize it simply by adding more attributes and adding/changing methods Syntax: class <SpecialClass> extends <GeneralClass> { // new attributes
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
public class Student { private String name = “N/A”; private String ssn = “N/A”; private float gpa = 0.0f; // constructors, get/set methods not shown here public void print() { System.out.println( name + “ " + ssn + " " + gpa); } } public class GraduateStudent extends Student { private String ugDegree; private String ugInst; }
Image of page 4
Inheritance and Methods public class Main { public static void main(String[] args){ GraduateStudent s1 = new GraduateStudent(); s1.print(); } } GraduateStudent class inherits all the attributes and the print( ) method > java Main N/A N/A 0.0 Output:
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
public class Student { public String name = “”; public String major = “”; public float gpa = 0.0f; public void print() { System.out.println( name + “ ” + major + “ ” + gpa); } } public class GraduateStudent extends Student { private String ugDegree; private String ugInstitution; public void print() { System.out.println(name + “ ” + major + “ ” + gpa); System.out.println(ugDegree + “ ” + ugInst); } } Method Overriding
Image of page 6
public class GraduateStudent extends Student { private String ugDegree; private String ugInstitution; public void print() { super .print(); System.out.println(ugDegree + “ ” + ugInst); } } public class Student { public String name = “”; public String major = “”; public float gpa = 0.0f; public void print() { System.out.println( name + “ ” + major + “ ” + gpa); } } Method Overriding
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Method Overriding Can change the implementation of the method but cannot change the method signature. If you change the signature of a method -- this results in method overloading .
Image of page 8
public class Student { private String name = “”; private String major = “”; private float gpa = 0.0f; public void print() { System.out.println( name + “ ” + major + “ ” + gpa); } } public class GraduateStudent extends Student{ private String ugDegree; private String ugInstitution; public void print(String filename) { // code to print the graduate student information // to the file called ‘filename’ } } Method Overloading
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
public class GraduateStudent extends Student{ private String ugDegree; private String ugInstitution; public void print(String filename) { int sName = name; // code to print the graduate student information // to the file called ‘filename’ } } public class Student { private String name = “”; private String major = “”; private float gpa = 0.0f; public void print() { System.out.println( name + “ ” + major + “ ” + gpa); } }
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern