InheritanceAndPolymorphism

InheritanceAndPolymorphism - CIS3023: Programming...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 We are all gifted. That is our inheritance. --Ethel Waters Ganesh 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 ). 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 // new or changed methods } 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; } 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: 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 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 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 . 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 public class GraduateStudent extends Student{ private String ugDegree; private String ugInstitution;...
View Full Document

This note was uploaded on 05/27/2011 for the course CIS 3023 taught by Professor Dobbins during the Summer '08 term at University of Florida.

Page1 / 45

InheritanceAndPolymorphism - CIS3023: Programming...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online