L4-Wraper-Inheritance-Polymorphism-Interfaces

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

Info iconThis preview shows pages 1–7. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE205 Object-Oriented Programming and Data Structures 1-2 Outline • Wrapper class • Inheritance • Polymorphism • Interfaces 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”); 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 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 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 1-7...
View Full Document

This note was uploaded on 04/18/2011 for the course CSE 205 taught by Professor Matasu during the Spring '08 term at ASU.

Page1 / 30

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

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

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