Object-Oriented Programming: Inheritance

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

If a base class changes, what is the impact on a derived class in Java?

  • The derived class will be deleted.
  • The derived class remains unaffected.
  • The derived class must have its name changed.
  • The derived class is also changed. (correct)

Which statement accurately describes the relationship between classes when using composition?

  • Classes are independent and do not directly interact.
  • One class 'has-a' relationship with another class, utilizing it as a component. (correct)
  • One class is an instance of another class.
  • One class inherits properties and methods from another class.

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

  • A maximum of two classes.
  • As many classes as there are methods in the parent class.
  • Only one class. (correct)
  • Any number of classes.

In the context of inheritance, what does the term 'overriding' refer to?

<p>Providing a specific implementation of a method in a subclass that is already defined in its superclass. (D)</p>
Signup and view all the answers

When overriding a method, what access modifier is required for the overriding method?

<p>It cannot be more restrictive than the method it overrides. (D)</p>
Signup and view all the answers

What is a key characteristic of final methods?

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

What is the primary purpose of using final classes?

<p>To prevent the class from being subclassed, ensuring its behavior remains constant. (D)</p>
Signup and view all the answers

When a new object of a derived class is created, what is the order of constructor execution?

<p>Base class constructor first, then derived class constructor. (B)</p>
Signup and view all the answers

If a base class constructor with arguments is defined, what must be done in the derived class constructor?

<p>The derived class must explicitly call the base class constructor using the <code>super</code> keyword. (B)</p>
Signup and view all the answers

What is the purpose of the finalize() method in Java?

<p>To perform cleanup operations before an object is garbage collected. (D)</p>
Signup and view all the answers

In what order are the finalize() methods of base and derived classes called during garbage collection?

<p>The <code>finalize()</code> method of the derived class is called first. (B)</p>
Signup and view all the answers

What is upcasting in the context of inheritance?

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

What is downcasting in the context of inheritance and why is it potentially unsafe?

<p>Casting a superclass reference to a subclass type; unsafe because the superclass object might not actually be an instance of the subclass. (B)</p>
Signup and view all the answers

What is the mechanism that allows a method call to be associated with a specific implementation at runtime, enabling polymorphic behavior?

<p>Late binding (dynamic binding) (D)</p>
Signup and view all the answers

What is the benefit of polymorphism in object-oriented programming?

<p>It increases code flexibility and extensibility by allowing objects of different classes to be treated as objects of a common type. (B)</p>
Signup and view all the answers

What happens when a subclass redefines a method that exists in its superclass?

<p>The subclass method overrides the superclass method. (A)</p>
Signup and view all the answers

How does method overloading relate to polymorphism?

<p>Method overloading is an example of compile-time polymorphism, where the correct method to call is determined at compile time based on the method signature. (D)</p>
Signup and view all the answers

Why is it important for the overriding method to have the same or a less restrictive access modifier compared to the overridden method?

<p>To ensure that the subclass method can be called wherever the superclass method can be called, maintaining substitutability. (D)</p>
Signup and view all the answers

What advantage does dynamic binding (late binding) provide in the context of polymorphism?

<p>It allows the specific implementation of a method to be determined at runtime based on the actual object type, promoting flexibility and extensibility. (D)</p>
Signup and view all the answers

How does the super keyword relate to inheritance in Java?

<p>It is used to access members (methods or fields) of the superclass from within the subclass. (A)</p>
Signup and view all the answers

Which scenario exemplifies the Liskov Substitution Principle?

<p>A subclass can be used in place of its superclass without altering the correctness of the program. (B)</p>
Signup and view all the answers

What is the role of the toString() method in Java, especially in the context of inheritance and polymorphism?

<p>It provides a string representation of an object, useful for debugging and logging, and can be overridden in subclasses to provide a more specific representation. (A)</p>
Signup and view all the answers

How do private methods behave with respect to inheritance?

<p><code>private</code> methods are not inherited by subclasses. (C)</p>
Signup and view all the answers

What is Run-Time Type Identification (RTTI)?

<p>A mechanism to identify the actual type of an object at runtime, enabling polymorphic behavior. (A)</p>
Signup and view all the answers

What is composition in object-oriented design?

<p>A class contains an instance of another class, creating a 'has-a' relationship (A)</p>
Signup and view all the answers

Given a class hierarchy where Animal is the superclass and Dog and Cat are subclasses, which of the following is true about an ArrayList<Animal>?

<p>It can store objects of type <code>Animal</code>, <code>Dog</code>, and <code>Cat</code> due to polymorphism (C)</p>
Signup and view all the answers

Which of the following describes final methods?

<p>Cannot be overridden in subclasses. (C)</p>
Signup and view all the answers

What is an abstract class in Java?

<p>A class that cannot be instantiated and may contain abstract methods. (A)</p>
Signup and view all the answers

How is method overriding related to polymorphism?

<p>It enables polymorphism. (D)</p>
Signup and view all the answers

Why are constructors not inherited in Java?

<p>Because they are class-specific and used to create instances of that specific class. (D)</p>
Signup and view all the answers

What does it mean for an object to be garbage collected in Java?

<p>The object is no longer referenced and the memory it occupies is reclaimed by the JVM. (C)</p>
Signup and view all the answers

What is the main difference between inheritance and composition in object-oriented programming?

<p>Inheritance allows code reuse through an “is-a” relationship, while composition achieves it through a “has-a” relationship. (B)</p>
Signup and view all the answers

If a subclass constructor does not explicitly call a superclass constructor using super(), what happens?

<p>The default constructor of the superclass is automatically called. (A)</p>
Signup and view all the answers

What happens if a class contains an abstract method and does not implement it?

<p>The class must be declared as abstract. (A)</p>
Signup and view all the answers

Flashcards

Inheritance

A mechanism where a new class inherits properties and behaviors from an existing class.

Class Composition

Utilizing a class to define another class, where one class contains an object of another Class.

Method Overriding

When a subclass provides a specific implementation for a method that is already defined in its superclass.

Final Class in Java

A final class prevents other classes from inheriting from it.

Signup and view all the flashcards

Polymorphism

The ability of a variable, function or object to take on multiple forms.

Signup and view all the flashcards

Upcasting

Upcasting is converting a subtype to a supertype.

Signup and view all the flashcards

Downcasting

Converting a supertype to a subtype

Signup and view all the flashcards

RTTI

Run Time Type Identification means to determine the type of object during runtime.

Signup and view all the flashcards

Study Notes

Object-Oriented Programming

  • Spring Semester 2024-25
  • Inheritance and Polymorphism

Reusing Classes

  • One class object construction.
  • Using a class to define another: composition or aggregation.
  • Example: creating an "engine" class, then a "car" class that "has" an "engine."

Inheritance

  • Copies a class's structure and extends it with characteristics and functions.
  • Changing the base class (parent or super) also changes the derived class (child).
  • In Java each class can only inherit from one class.

Inheritance Examples

  • Class Human includes name, surname, and age, with methods to set and get the name variable.
  • Class Address is defined with street, number, state, and postcode attributes.
  • An Employee class inherits from Human and includes position and Address fields.
  • Employee can set position and address variables.

Visibility and Inheritance

  • All members of Employee, including private, protected, and public, are visible within the class.
  • Only public and protected members of Manager, PieceWorker, and HourlyWorker are visible to their objects. Bonus and all methods are included.
  • e.g., In the Manager class, an attempt to directly assign a value to a private position field will result in an error, while using the setPosition method is correct.
  • Only public members are visible in another class.

Inheritance and Constructors

  • Constructors are not inherited.
  • Constructors are closely tied to their respective classes.
  • Suppose Employee had two constructors: one with name, wage, hours, and attitude, and another with name and wage.
  • If Manager defines a constructor with name, wage, hours, attitude, and an Employee reference, Manager will not automatically inherit the two constructors from Employee.

Method Overloading

  • getCompleteData method in Human class as an example of method overloading.
  • The ability to call due to inheritance for an Employee, but it won't have all the information for them.

Overriding the Method

  • Override a method by declaring it with the same name in the Employee class.
  • Use super to call the employee's existing method.

Overriding toString

  • In Java, every class inherits the Object class by default, which has a public toString method that returns a String.
  • The public method can override in the three classes, the getCompleteData and getCompleteAddress can be renamed.
  • Must be public because it is public in Object.

Final Methods

  • "Final" variables become constants, assigned only once.
  • "Final" methods cannot be overridden.
  • The parent class defines them once and does not redefine them in subclasses.
  • All private methods are implicitly final.
  • This ensures that the behavior is maintained and cannot be changed in subclasses.

Final Classes

  • "Final" classes cannot be inherited.
  • All of its methods become implicitly final.
  • This is used when one needs to be sure that no one will inherit them.
  • final methods and classes are not often used.

Constructors for Object Management

  • Class Human():
    • name and surname are set to Unknown.
    • Age is assigned the value of zero.
    • Prints “A new Human has been created”.
  • Class Address():
    • street and city are set to Unknown.
    • Number is assigned the value zero.
    • Prints “A blank Address has been created”.
  • Class Employee():
    • residence is set to new Address().
    • position is set to Unemployed.
    • Prints “A new Employee has been created”.
  • Base constructor is called automatically.

Object constructors

  • Address ad1 and Human h1 created.
  • Constructor order matters.
  • A new address, person and employee are created.
  • The basic constructor is called automatically.

Human (No Default Constructor)

  • If the Human class doesn't have a default constructor, you need to explicitly specify which Human constructor to call in the Employee constructor using super.
  • Here, "Unknown" for name and surname, and 0 for age are used.

Example Usage

  • Employee object is created.
  • Outputs constructor messages.

Finalize Method Example

  • In Human class, finalize() prints Human is being cleared.
  • In Employee, super.finalize() is called followed by Employee being cleared being printed.

Finalize order

  • Object created then labeled as useless when System.gc() is used
  • The address is useless after Employee is cleared.

Polymorphism and Inheritance

  • Upcasting: You can put Employee objects in a Human array.
Human[] group=new Human[];
group[0]=new Human();
group[1]=new Employee();
  • It is safe to call the Human class's methods and properties on them.
  • Downcasting: It is necessary to convert an Employee to employee before calling its members.
(Employee)group[1].getPosition();
(Employee)group[0].getPosition(); //Class Cast Exception

RTTI Solution

  • The problem resolves automatically for toString because it is present in both types.
  • Without the need for downcasting:
group[1].toString();
group[0].toString();
  • Based on type, the function is invoked.
  • Run Time Type Identification determines type.

Object structure

  • ArrayList and Vector store objects deriving from same base.
  • Base has methods derived types override.
  • Extracting, converts to base, exhibiting polymorphism.

Polymorphism Advantages

  • Focus on general behavior; specific behavior is determined at runtime.
  • Eases expansion through uniform messaging.

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