Podcast
Questions and Answers
What is the primary function of inheritance in object-oriented programming?
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?
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?
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?
In Java, how many classes can a class directly inherit from?
What accessibility does a derived class have to members of its base class?
What accessibility does a derived class have to members of its base class?
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?
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?
What is method overloading?
What is method overloading?
What does the super
keyword do in Java?
What does the super
keyword do in Java?
In Java, what is the purpose of the toString()
method?
In Java, what is the purpose of the toString()
method?
If no toString()
method is explicitly defined in a class, what happens when you try to print an object of that class?
If no toString()
method is explicitly defined in a class, what happens when you try to print an object of that class?
What is the effect of declaring a method as final
?
What is the effect of declaring a method as final
?
What is the consequence of declaring a class as final
?
What is the consequence of declaring a class as final
?
Consider the following code:
class Base {}
final class Derived extends Base {}
Is this code valid?
Consider the following code:
class Base {}
final class Derived extends Base {}
Is this code valid?
In Java, what does garbage collection primarily manage?
In Java, what does garbage collection primarily manage?
When is the finalize()
method called on an object?
When is the finalize()
method called on an object?
What is the correct order of constructor calls when an object of a subclass is created?
What is the correct order of constructor calls when an object of a subclass is created?
If a superclass does not have a default (no-argument) constructor, what must a subclass do?
If a superclass does not have a default (no-argument) constructor, what must a subclass do?
What does polymorphism enable in object-oriented programming?
What does polymorphism enable in object-oriented programming?
In Java, what is upcasting?
In Java, what is upcasting?
What is downcasting?
What is downcasting?
When is explicit downcasting typically required?
When is explicit downcasting typically required?
What is the potential risk of using downcasting?
What is the potential risk of using downcasting?
What is Run-Time Type Identification (RTTI) used for?
What is Run-Time Type Identification (RTTI) used for?
What is the primary advantage of Polymorphism?
What is the primary advantage of Polymorphism?
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?
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?
How does using the ArrayList
or Vector
classes relate to polymorphism?
How does using the ArrayList
or Vector
classes relate to polymorphism?
Which keyword prevents a method from being overridden in a subclass?
Which keyword prevents a method from being overridden in a subclass?
Which keyword is used to call the superclass's constructor from a subclass?
Which keyword is used to call the superclass's constructor from a subclass?
Which statement about constructors and inheritance is true?
Which statement about constructors and inheritance is true?
What is the implication of using an abstract method in Java?
What is the implication of using an abstract method in Java?
If a class contains at least one abstract method, what must be true about that class?
If a class contains at least one abstract method, what must be true about that class?
When does method overriding occur?
When does method overriding occur?
Which of the following best describes the use of abstract methods in object-oriented programming?
Which of the following best describes the use of abstract methods in object-oriented programming?
Flashcards
Inheritance
Inheritance
A mechanism where a class inherits properties and methods from another class.
Creating an object of a 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 another class definition
Using a class in the definition of another class - Composition or Aggregation
Basic Class
Basic Class
Signup and view all the flashcards
Derived Class
Derived Class
Signup and view all the flashcards
Class inheritances in Java
Class inheritances in Java
Signup and view all the flashcards
Final methods
Final methods
Signup and view all the flashcards
Final Classes
Final Classes
Signup and view all the flashcards
Class Inheritance
Class Inheritance
Signup and view all the flashcards
Basic Constructor
Basic Constructor
Signup and view all the flashcards
Finalize process
Finalize process
Signup and view all the flashcards
Polymorphism
Polymorphism
Signup and view all the flashcards
Structures of elements
Structures of elements
Signup and view all the flashcards
Converting class to basic
Converting class to basic
Signup and view all the flashcards
Polymorphism benefits
Polymorphism benefits
Signup and view all the flashcards
Method Overriding
Method Overriding
Signup and view all the flashcards
Method Overloading
Method Overloading
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)
andpublic 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 declarationsuper
- This adds employee-specific information
Overloading toString
- Every class in Java inherits the
Object
class, which has a publictoString
method that returns a String - It can be overloaded in classes by renaming
getCompleteData
andgetCompleteAddress
- 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.