Presentation is loading. Please wait.

Presentation is loading. Please wait.

8.1 Classes & Inheritance. 8.1.1 Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.

Similar presentations


Presentation on theme: "8.1 Classes & Inheritance. 8.1.1 Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes."— Presentation transcript:

1 8.1 Classes & Inheritance

2 8.1.1 Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes in common We can create an object that models their common attributes

3 8.1.2 Abstraction A superclass can be used to model common attributes or behavior Sometimes, the superclass cannot be an object itself The superclass may define the behaviors that subclasses must implement

4 8.1.3 The problem of multiple inheritance Some languages allow one class to have multiple super-classes Inheritance provides an “is a” relationship Multiple inheritance creates the potential for conflicting attributes and behaviors

5 8.2.1 Java language keywords in inheritance extends: Declares that the current class is a subclass of another super: A keyword used by the subclass to access the attributes of the superclass protected: An access modifier that restricts access to subclasses abstract: Declares that a class is an interface implements: Allows a class to take on the properties of an interface

6 8.3.1 Role of access modifiers in inheritance The subclass inherits all the attributes and behaviors of the superclass except those declared as private

7 8.4.1 Method overriding Subclasses may need to customize the behavior of the superclass The subclass defines a method that already exists in the superclass When this method is called, it is the one defined in the subclass that is used The superclass method is still accessible from the subclass using the super keyword

8 8.4.3 Overloading versus overriding Overriding is when a method in the subclass is designed to replace a method of the same name in the superclass Overloading is having several methods with the same name in the same class Overriding allows you to customize behavior Overloading creates several ways to do the same thing!

9 8.6.1 Handling constructors in inheritance Constructors can be overloaded Whenever an subclass is ‘constructed’ the superclass(es) constructors are also called The subclass constructor can explicitly call the superclass constructor using the super keyword If there is no corresponding constructor in the superclass, the compilation will fail

10 8.7.1 Abstract classes Superclasses are supposed to model more generalized attributes and behavior High up the tree, the classes are more a guideline that a model Abstract classes define methods that have no body and must be overridden The abstract class does not know how to perform the behavior, but does force subclasses to create a behavior with this name

11 8.7.2 Final classes Use the final keyword to declare an attribute immutable Also use the final keyword to prevent a method from being overridden in a subclass

12 8.8.1 What and why of interfaces Java prevents multiple inheritance However, a class can inherit attributes from multiple sources using interfaces Interfaces are a blueprint for behavior The interface does not define the behavior, but forces the subclass to do it


Download ppt "8.1 Classes & Inheritance. 8.1.1 Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes."

Similar presentations


Ads by Google