Docsity

Object-Oriented Programming: Understanding Inheritance, Cheat Sheet of Computer Science

Partial preview of the text

Download Object-Oriented Programming: Understanding Inheritance and more Cheat Sheet Computer Science in PDF only on Docsity!

Prepared By Dr Jamal Zraqo

Part 2 –

Inheritance

Part 2 Object Oriented Programming Course Prepared by Dr Jamal Zaraqo

Inheritance

  • (^) Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes
  • (^) Part 2 focuses on
    • (^) deriving new classes from existing classes
    • (^) the protected modifier
    • (^) creating class hierarchies
    • (^) abstract classes
    • (^) indirect visibility of inherited members
    • (^) designing for inheritance

Getters & Setters

class Point { private int x; private int y; public Point(int x,int y) { this.x =x; this.y =y; } public int getX() { return x; } public void setX(int xValue) { x = (xValue >0)? xValue:0; } public int getY() { return y; } public void setY(int yValue) { y = (yValue >0)? yValue:0; } }

Part 2.1 – Inheritance

  • (^) Inheritance allows a software developer to derive a new class from an existing one
  • (^) The existing class is called the parent class, or super class , or base class
  • (^) The derived class is called the child class or subclass
  • (^) As the name implies, the child inherits characteristics of the parent
  • (^) That is, the child class inherits the methods and data defined by the parent class

Part 2.1 – Inheritance

  • (^) A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones
  • (^) Software reuse is a fundamental benefit of inheritance
  • (^) By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software

Part 2.1 – Deriving Subclasses

  • (^) In Java, we use the reserved symbol extends to establish an inheritance relationship class Car extends Vehicle { // class contents }

Part 2.1 – 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

Part 2.1 – The protected Modifier

  • (^) The protected modifier allows a child class to reference a variable or method directly in the child class
  • (^) It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility
  • (^) A protected variable is visible to any class in the same package as the parent class
  • (^) Protected variables and methods can be shown with a # symbol preceding them in UML diagrams

The super Reference

  • (^) A child’s constructor is responsible for calling the parent’s constructor
  • (^) In the header of a child’s constructor the base is used to reference and call the parent’s constructor
  • (^) The base reference can also be used to reference other variables and methods defined in the parent’s class

1- 14 class Animal{ String color="white"; } class Dog extends Animal{ String color="black"; void printColor(){ System.out.println(color);// prints color of Dog class System.out.println( super .color);// prints color of Animal class } } class TestSuper1{ public static void main(String args[]){ Dog d= new Dog(); d.printColor();

The super Reference

Abstract Classes

  • (^) An abstract class is a placeholder in a class hierarchy that represents a generic concept
  • (^) An abstract class cannot be instantiated
  • (^) We use the modifier abstract on the class header to declare a class as abstract: public abstract class Product { // contents }

Abstract Classes

  • (^) An abstract class often contains abstract methods with no definitions (like an interface)
  • (^) Unlike an interface, the abstract modifier must be applied to each abstract method
  • (^) Also, an abstract class typically contains non-abstract methods with full definitions
  • (^) A class declared as abstract does not have to contain abstract methods – simply declaring it as abstract makes it so

An Abstract Class in UML

Abstract classes and methods are shown in italics font.

abstract class Bike{ abstract void run(); } class Honda4 extends Bike{ void run() {System.out.println("running safely");} public static void main(String args[]){ Bike obj = new Honda4(); obj.run(); } }

Abstract Classes