InheritanceAndPolymorphism

InheritanceAndPolymorphism - CIS3023: Programming...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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; }
Background 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:
Background image of page 5

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

View Full DocumentRight 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
Background 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
Background image of page 7

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

View Full DocumentRight 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 .
Background 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
Background image of page 9

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

View Full DocumentRight 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); } }
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the 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