{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L4-Wraper-Inheritance-Polymorphism-Interfaces

L4-Wraper-Inheritance-Polymorphism-Interfaces - CSE205...

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

View Full Document Right Arrow Icon
CSE205 Object-Oriented Programming and Data Structures
Image of page 1

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

View Full Document Right Arrow Icon
1-2 Outline Wrapper class Inheritance Polymorphism Interfaces
Image of page 2
Wrapper Class A wrapper class represents a particular primitive data type. (primitive data types – int, double, char, boolean, and so on) They are defined in the java.lang package. Instead of declaring an integer as primitive data as: int number1 = 45; We can declare it as an object of type Integer (wrapper) class. Integer number1 = new Integer(45); For instance, the method doubleValue() returns the value of this integer as double. number1.doubleValue() The class Integer also has static methods such as parseInt(String str) that returns the int corresponding to the value stored in the parameter string. Since it is static, it can be called with the class name (without instantiating an object) as: int num = Integer.parseInt(“11”); Then “num” will contain the integer 11. Also see other wrapper classes such as Double. e.g., double num2 = Double.parseDouble(“3.21”);
Image of page 3

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

View Full Document Right Arrow Icon
New classes can be created vis inheritance faster, easier, and cheaper than by witting them from scratch. Inheritance is one way to support the idea of Software reuse . For instance, if we are trying to define Cat class, Dog class, and Raccoon class and know all of them will need instance data tail and legs, we can define them in another class called “Animal” and let Cat, Dog, and Raccoon classes inherit from it. Animal int tail; int legs; Cat Dog Raccoon Here the Animal class is called “ parent class ”, “ super class ”, or “ base class The classes Cat, Dog, and Raccoon are called “ child class ” or “ sub class ”. The child classes will inherit the methods and data defined for the parent class. However, child classes do not inherit private variables and methods. They can inherit public variables and methods. Making instance data (variables) public violates Encapsulation. There is another visibility modifier called protected” . Protected variables and methods can be inherited by child classes, but not as accessible as public. If you plan to have a class inherited by other classes, its data should be declared as “protected”. Inheritance
Image of page 4
public -- inherited, can be accessed from anywhere (we use + for UML class diagrams) private – not inherited, can be accessed only within its class (we use – for UML class diagrams) protected – inherited, can be accessed by any class in the same package (we use # for UML class diagrams) Note: Constructors are NOT inherited. Visibility Modifiers
Image of page 5

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

View Full Document Right Arrow Icon
1-6 The protected Modifier Visibility modifiers affect the way that class members can be used in a child class Variables and methods declared with private visibility cannot be referenced by name in a child class They can be referenced in the child class if they are declared with public visibility – but public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected
Image of page 6
1-7 The protected Modifier The
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
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