Object-Oriented Programming: Inheritance & Polymorphism

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the primary function of inheritance in object-oriented programming?

  • To create multiple instances of a class.
  • To reuse and extend the structure and behavior of existing classes. (correct)
  • To define a new data type.
  • To encapsulate data within a class.

Which of the following best describes composition (or aggregation) in the context of class design?

  • A mechanism to inherit properties from multiple classes.
  • Overriding methods in a subclass.
  • Creating an object of one class within another class. (correct)
  • Defining abstract methods in a class.

If a base class is modified in Java, what is the direct impact on its derived classes?

  • Derived classes remain unchanged.
  • Derived classes automatically reflect the changes from the base class. (correct)
  • Derived classes must be recompiled but otherwise remain the same.
  • The program will no longer compile due to version conflicts.

In Java, how many classes can a class directly inherit from?

<p>Only one (C)</p>
Signup and view all the answers

What accessibility does a derived class have to members of its base class?

<p>It has access to public and protected members. (C)</p>
Signup and view all the answers

Consider a class Vehicle with a method startEngine(). A class Car inherits from Vehicle and overrides the startEngine() method. If you have a Car object, which version of startEngine() is called?

<p>The <code>startEngine()</code> method in the <code>Car</code> class. (C)</p>
Signup and view all the answers

What is method overloading?

<p>Defining multiple methods with the same name but different parameters within a class. (D)</p>
Signup and view all the answers

What does the super keyword do in Java?

<p>It refers to the superclass of the current class. (D)</p>
Signup and view all the answers

In Java, what is the purpose of the toString() method?

<p>To convert an object to its string representation. (D)</p>
Signup and view all the answers

If no toString() method is explicitly defined in a class, what happens when you try to print an object of that class?

<p>The <code>toString()</code> method of the <code>Object</code> class is called. (D)</p>
Signup and view all the answers

What is the effect of declaring a method as final?

<p>It cannot be overridden in subclasses. (A)</p>
Signup and view all the answers

What is the consequence of declaring a class as final?

<p>It cannot be extended (inherited from). (A)</p>
Signup and view all the answers

Consider the following code:

class Base {}
final class Derived extends Base {}

Is this code valid?

<p>Yes, because a final class can extend from another class (A)</p>
Signup and view all the answers

In Java, what does garbage collection primarily manage?

<p>Memory allocation and deallocation (C)</p>
Signup and view all the answers

When is the finalize() method called on an object?

<p>Right before the object is garbage collected. (D)</p>
Signup and view all the answers

What is the correct order of constructor calls when an object of a subclass is created?

<p>Superclass constructor, then subclass constructor. (A)</p>
Signup and view all the answers

If a superclass does not have a default (no-argument) constructor, what must a subclass do?

<p>The subclass must explicitly call a superclass constructor using <code>super()</code>. (C)</p>
Signup and view all the answers

What does polymorphism enable in object-oriented programming?

<p>The ability of an object to take on many forms. (D)</p>
Signup and view all the answers

In Java, what is upcasting?

<p>Casting a subclass object to a superclass type. (A)</p>
Signup and view all the answers

What is downcasting?

<p>Casting a superclass object to a subclass type (D)</p>
Signup and view all the answers

When is explicit downcasting typically required?

<p>When assigning an object of a superclass to a subclass variable. (D)</p>
Signup and view all the answers

What is the potential risk of using downcasting?

<p><code>ClassCastException</code> at runtime. (D)</p>
Signup and view all the answers

What is Run-Time Type Identification (RTTI) used for?

<p>Determining the data type of an object at runtime. (D)</p>
Signup and view all the answers

What is the primary advantage of Polymorphism?

<p>Code flexibility and extensibility. (C)</p>
Signup and view all the answers

Consider the following scenario: You have a list of objects, all inheriting from a common base class. You want to call a method that is defined in the base class, but each subclass has its own implementation. How does polymorphism help achieve this?

<p>By allowing you to call the method on each object without knowing its specific type. (B)</p>
Signup and view all the answers

How does using the ArrayList or Vector classes relate to polymorphism?

<p>They allow storing objects of different types that inherit from a common base class, leveraging polymorphism. (B)</p>
Signup and view all the answers

Which keyword prevents a method from being overridden in a subclass?

<p><code>final</code> (C)</p>
Signup and view all the answers

Which keyword is used to call the superclass's constructor from a subclass?

<p><code>super</code> (D)</p>
Signup and view all the answers

Which statement about constructors and inheritance is true?

<p>Constructors are not inherited, but subclasses can call a superclass constructor using <code>super()</code>. (D)</p>
Signup and view all the answers

What is the implication of using an abstract method in Java?

<p>The method must not have a body. (C)</p>
Signup and view all the answers

If a class contains at least one abstract method, what must be true about that class?

<p>It must be declared as <code>abstract</code>. (B)</p>
Signup and view all the answers

When does method overriding occur?

<p>When a subclass defines a method with same name and parameters as in superclass. (C)</p>
Signup and view all the answers

Which of the following best describes the use of abstract methods in object-oriented programming?

<p>Forces subclasses to provide concrete implementations (C)</p>
Signup and view all the answers

Flashcards

Inheritance

A mechanism where a class inherits properties and methods from another class.

Creating an object of a class

Creating an object of one class, using another class

Using a class in another class definition

Using a class in the definition of another class - Composition or Aggregation

Basic Class

Base or super or parent class

Signup and view all the flashcards

Derived Class

Derived, inherited, sub, or child class

Signup and view all the flashcards

Class inheritances in Java

Each call in Java can inherit only one class

Signup and view all the flashcards

Final methods

Final methods cannot be overridden. Variables means constants

Signup and view all the flashcards

Final Classes

Final classes cannot be inherited.

Signup and view all the flashcards

Class Inheritance

A process in which one class acquires the attributes and methods of another.

Signup and view all the flashcards

Basic Constructor

A default constructor provided by Java if no constructor is explicitly defined.

Signup and view all the flashcards

Finalize process

Destroys and clear the memory (Human has been cleared)

Signup and view all the flashcards

Polymorphism

Ability of an object to take on many forms.

Signup and view all the flashcards

Structures of elements

ArrayList and Vectors, used to store objects with basic class

Signup and view all the flashcards

Converting class to basic

Objects are converted to basic class when calling them

Signup and view all the flashcards

Polymorphism benefits

Ability of code to exist in several forms

Signup and view all the flashcards

Method Overriding

Overriding a superclass method in a subclass to provide a specific implementation.

Signup and view all the flashcards

Method Overloading

A method with the same name and different paramaters

Signup and view all the flashcards

Study Notes

  • Object-Oriented Programming is the topic
  • Spring Semester 2024-25
  • This course covers inheritance and polymorphism.

References

  • Dr. Ioannis Violos is the reference
  • Subject is Object-Oriented Programming II (Java)
  • Department of Informatics and Telematics, Harokopio University

Topics Covered

  • Covers inheritance
  • Covers overloading
  • Covers object management
  • Covers polymorphism
  • Covers abstract classes

Reusing Classes

  • Creating an object of a class is one way to reuse classes
  • Using a class in the definition of another class is composition or aggregation
  • Composition example: A "Car" class includes a "Engine" class

Inheritance

  • Inheritance copies the structure of a class and extends it with new characteristics and functions
  • If the base class (super or parent class) changes, the derived class (inherited, sub, or child class) also changes
  • In Java, each class can inherit from only one class

Example Classes

  • The Human class has name, surname, and age. Includes methods to set and get the name
  • The Address class has street, number, state, and postcode

Employee Class

  • Employee class extends Human class through inheritance
  • Has position and address attributes, which is composition
  • Includes methods to set and get the position and address

Employee Class Diagram

  • Employee (abstract): Includes residence, position, and bonus attributes and several methods.
  • Manager: Inherits from Employee and includes weeklySalary attribute and Manager() and getSalary() methods.
  • PieceWorker: Inherits from Employee and includes wagePerPiece and quantity attributes and PieceWorker() and getSalary() methods.
  • HourlyWorker: Inherits from Employee and includes wage and hours attributes and HourlyWorker() and getSalary() methods.

Inheritance and Visibility

  • Members of the Employee class: All (private, protected, and public) are visible
  • Members of Manager, PieceWorker, HourlyWorker, and their objects
    • Only public and protected members are accessible
    • For example, bonus and the methods

Constructors and Inheritance

  • Constructors are not inherited
  • Constructors are linked to their class definitions
  • If Employee defines two constructors, for example: public Employee(String nm, int wage, int hours, double attitude) and public Employee(String nm, int wage)
  • If Manager defines one constructor: public Manager(String nm, int wage, int hours, double attitude, Employee under)
  • Manager cannot inherit constructors with 2 and 4 arguments

Method Overloading

  • It refers to defining a method more than once using same name
  • Methods are defined in the Human class
  • Inheritance allows to call methods for an Employee, but may not have all the required information

Overloading Solution

  • Define the method in Employee with the same name
  • Call the overloaded method from the employee the declaration super
  • This adds employee-specific information

Overloading toString

  • Every class in Java inherits the Object class, which has a public toString method that returns a String
  • It can be overloaded in classes by renaming getCompleteData and getCompleteAddress
  • Return method requires being public within the Object

Final Methods

  • "Final" variables become constants, cannot be changed once assigned a value
  • "Final" methods cannot be overridden
  • Parent classes define them once and not redefined in subclasses
  • Private methods are implicitly final
  • Behavior is maintained and cannot be changed in subclasses

Final Classes

  • "Final" classes cannot be inherited
  • All their methods become implicitly final
  • Used when be certain that no one will inherit them
  • Example: public final class String{ .....}
  • Final methods and classes are not used fequently

Constructors

  • Human constructor: Sets name and surname to "Unknown", and age to 0. Prints "A new Human has been created"
  • Address constructor: Sets street, number, and city to "Unknown". Prints "A blank Address has been created"
  • Employee constructor: Creates a new Address, sets position to "Unemployed", and prints "A new Employee has been created".

Creating Objects

  • Calling the Human constructor automatically creates the Human base constructor

Handling Missing Base Constructor

  • Define a constructor that takes name, surname, and age as parameters

Super Keyword

  • Use the super keyword to call the specific Human constructor: super("Unknown","Unknown",0);

Example Usage

  • Code creates an Employee object, Unknown is created
  • The blank Address and the new Employee is created also

Destructors - finalize

  • Destructors are useful to clear memory when objects are deleted
  • System calls it automatically, like Garbage collection

Order of calling destructors

  • Employee is created which creates a human, which has unknown attributes
  • A new address is created that is blank
  • A new is created, Human cleared, Employee cleared, and address cleared.
  • After the employee is destroyed the address is useless.

Polymorphism

  • Structures and Inheritance: An array of Human objects, can hold also Employee objects through upcasting.

Calling Methods on Objects

  • Can safely call the characteristics and methods of the Human class
  • Employe characteristics and methods can be called via downcasting
  • Run Time Type Identification – Type is determined at execution

Data Structures and polymorphism

  • ArrayList and Vector store objects of different classes that inherit from the same base class
  • Base class has methods that derived classes override
  • Converting to the base class allow recall of class messages
  • Behavior relies on object type.

Advantages of Polymorphism

  • Can focus on the general behavior of objects and allow their specific behavior to be defined during execution
  • Simplifies expansion due to message calls being identical to the base class
  • New classes needs only to define their method of handling messages

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser