Podcast
Questions and Answers
If a base class changes, what is the impact on a derived class in Java?
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?
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?
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?
In the context of inheritance, what does the term 'overriding' refer to?
When overriding a method, what access modifier is required for the overriding method?
When overriding a method, what access modifier is required for the overriding method?
What is a key characteristic of final
methods?
What is a key characteristic of final
methods?
What is the primary purpose of using final
classes?
What is the primary purpose of using final
classes?
When a new object of a derived class is created, what is the order of constructor execution?
When a new object of a derived class is created, what is the order of constructor execution?
If a base class constructor with arguments is defined, what must be done in the derived class constructor?
If a base class constructor with arguments is defined, what must be done in the derived class constructor?
What is the purpose of the finalize()
method in Java?
What is the purpose of the finalize()
method in Java?
In what order are the finalize()
methods of base and derived classes called during garbage collection?
In what order are the finalize()
methods of base and derived classes called during garbage collection?
What is upcasting in the context of inheritance?
What is upcasting in the context of inheritance?
What is downcasting in the context of inheritance and why is it potentially unsafe?
What is downcasting in the context of inheritance and why is it potentially unsafe?
What is the mechanism that allows a method call to be associated with a specific implementation at runtime, enabling polymorphic behavior?
What is the mechanism that allows a method call to be associated with a specific implementation at runtime, enabling polymorphic behavior?
What is the benefit of polymorphism in object-oriented programming?
What is the benefit of polymorphism in object-oriented programming?
What happens when a subclass redefines a method that exists in its superclass?
What happens when a subclass redefines a method that exists in its superclass?
How does method overloading relate to polymorphism?
How does method overloading relate to polymorphism?
Why is it important for the overriding method to have the same or a less restrictive access modifier compared to the overridden method?
Why is it important for the overriding method to have the same or a less restrictive access modifier compared to the overridden method?
What advantage does dynamic binding (late binding) provide in the context of polymorphism?
What advantage does dynamic binding (late binding) provide in the context of polymorphism?
How does the super
keyword relate to inheritance in Java?
How does the super
keyword relate to inheritance in Java?
Which scenario exemplifies the Liskov Substitution Principle?
Which scenario exemplifies the Liskov Substitution Principle?
What is the role of the toString()
method in Java, especially in the context of inheritance and polymorphism?
What is the role of the toString()
method in Java, especially in the context of inheritance and polymorphism?
How do private
methods behave with respect to inheritance?
How do private
methods behave with respect to inheritance?
What is Run-Time Type Identification (RTTI)?
What is Run-Time Type Identification (RTTI)?
What is composition in object-oriented design?
What is composition in object-oriented design?
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>
?
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>
?
Which of the following describes final methods?
Which of the following describes final methods?
What is an abstract class in Java?
What is an abstract class in Java?
How is method overriding related to polymorphism?
How is method overriding related to polymorphism?
Why are constructors not inherited in Java?
Why are constructors not inherited in Java?
What does it mean for an object to be garbage collected in Java?
What does it mean for an object to be garbage collected in Java?
What is the main difference between inheritance and composition in object-oriented programming?
What is the main difference between inheritance and composition in object-oriented programming?
If a subclass constructor does not explicitly call a superclass constructor using super()
, what happens?
If a subclass constructor does not explicitly call a superclass constructor using super()
, what happens?
What happens if a class contains an abstract method and does not implement it?
What happens if a class contains an abstract method and does not implement it?
Flashcards
Inheritance
Inheritance
A mechanism where a new class inherits properties and behaviors from an existing class.
Class Composition
Class Composition
Utilizing a class to define another class, where one class contains an object of another Class.
Method Overriding
Method Overriding
When a subclass provides a specific implementation for a method that is already defined in its superclass.
Final Class in Java
Final Class in Java
Signup and view all the flashcards
Polymorphism
Polymorphism
Signup and view all the flashcards
Upcasting
Upcasting
Signup and view all the flashcards
Downcasting
Downcasting
Signup and view all the flashcards
RTTI
RTTI
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
includesname
,surname
, andage
, with methods to set and get thename
variable. - Class
Address
is defined withstreet
,number
,state
, andpostcode
attributes. - An
Employee
class inherits fromHuman
and includesposition
andAddress
fields. - Employee can set position and address variables.
Visibility and Inheritance
- All members of
Employee
, includingprivate
,protected
, andpublic
, are visible within the class. - Only
public
andprotected
members ofManager
,PieceWorker
, andHourlyWorker
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 aprivate
position
field will result in an error, while using thesetPosition
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 anEmployee
reference,Manager
will not automatically inherit the two constructors fromEmployee
.
Method Overloading
getCompleteData
method inHuman
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
andgetCompleteAddress
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 implicitlyfinal
. - 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
andsurname
are set to Unknown.- Age is assigned the value of zero.
- Prints “A new Human has been created”.
- Class
Address()
:street
andcity
are set to Unknown.- Number is assigned the value zero.
- Prints “A blank Address has been created”.
- Class
Employee()
:residence
is set to newAddress()
.position
is set to Unemployed.- Prints “A new Employee has been created”.
- Base constructor is called automatically.
Object constructors
- Address
ad1
andHuman
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.