40 Questions
What is an advantage of using polymorphism in programming?
It enables you to write programs that process objects that share the same superclass.
What is a characteristic of a private access modifier?
It can be accessed only within the class.
What is the purpose of using a scanner in Java?
To get input from the user.
What is the symbol used to represent protected access modifier in UML?
What is the main difference between inheritance and abstraction?
Inheritance is about creating a new class, whereas abstraction is about hiding implementation details.
What is the maximum number of superclasses a class can extend in Java?
1
What is the purpose of access modifiers in object-oriented programming?
To define the hierarchy of classes.
What is the UML symbol for public access modifier?
What is the 'is-a' relationship referring to in object-oriented programming?
Inheritance
What is the purpose of inheritance in object-oriented programming?
To create an 'is-a' relationship between classes
What is polymorphism in object-oriented programming?
Existing in multiple forms
What is the term for the class from which a subclass inherits?
Base class
What is encapsulation in object-oriented programming?
Hiding data from other classes
What is the purpose of abstraction in object-oriented programming?
To show only necessary information
What is the purpose of access modifiers in object-oriented programming?
To hide or show data to other classes
What is the purpose of UML in object-oriented programming?
To design and visualize classes
What is the purpose of using 'super' in a subclass constructor?
To explicitly invoke a superclass constructor
What happens when an instance of a class is constructed?
The constructors of all the superclasses along the inheritance chain are invoked
What is polymorphism?
Existing in many forms
What is the benefit of polymorphism?
It allows a subclass object to be used wherever its superclass object is used
What is the meaning of the term 'polymorphism'?
Many forms in Greek
Why should inheritance be used thoughtfully?
Because it may not apply for some is-a relationships
What is the purpose of method overriding in polymorphism?
To provide a different implementation of a method already defined in the superclass
What is the concept known as 'programming in the general rather than programming in the specific'?
Abstraction
What is the purpose of the 'extends' keyword in the subclass declaration?
To inherit from a superclass
What is the default value of the 'color' field in the GeometricShape class?
Colourless
How can the subclass access the superclass fields?
Using the superclass setters and getters
What is the purpose of the 'super' keyword in the subclass constructor?
To invoke the no-arg constructor of its superclass
What is the relationship between the Circle class and the GeometricShape class?
Inheritance
What is the benefit of using inheritance in object-oriented programming?
To promote code reuse and reduce duplication
What is the purpose of the 'filled' field in the GeometricShape class?
To store whether the shape is filled or not
What is the effect of the 'public class Circle extends GeometricShape' declaration?
The Circle class becomes a subclass of GeometricShape
What is the primary purpose of inheritance in object-oriented programming?
To reuse robust well-tested classes to build new software
What is the relationship between Circle and GeometricShape in the UML diagram?
Circle is a subclass of GeometricShape
What is the term for the process of creating a new class based on an existing class?
Inheritance
What is the benefit of using inheritance in programming?
It boosts programmer productivity and reduces redundancy
What do Circle, Rectangle, and Triangle have in common in the UML diagram?
They are all subclasses of GeometricShape
What is unique to each of the shapes in the UML diagram?
Their specific attributes and methods
What is the term for the concept of showing only essential features of an object?
Abstraction
What is the benefit of using UML diagrams in software development?
They provide a visual representation of the software design
Study Notes
Polymorphism
- Provides a way to write programs that process objects that share the same superclass directly or indirectly as though they are all objects of the superclass
- Enables you to hold a reference to a Chicken object or a Duck object in a variable of type Animal
Using Scanner
- A scanner is used to get input from the user
- Import Scanner package
- A scanner should be closed
- Offers many methods: next(), nextInt(), nextLine(), nextFloat(), nextLine(), nextDouble()
Abstraction
- A class can implement multiple interfaces, but it can only extend one superclass
- Enables you to write programs that process objects that share the same superclass directly or indirectly as though they are all objects of the superclass
Modifiers
- Access modifiers: public, protected, package, private
- Public: accessible within class, within package, and in subclass
- Protected: accessible within class, within package, and in subclass
- Package: accessible within class and within package
- Private: accessible within class
- Modifiers are necessary to define proper hierarchy
Inheritance
- Refers to is-a relationship between a super class and subclass
- Super class (base class, parent class) - existing top class whose members are inherited
- Subclass inherits members of the superclass
- Super class can be used to create a subclass
- Use inheritance thoughtfully as it may not apply for some is-a relationships
Inheritance Example
- In Class Demo / Practice
Constructor Chaining
- For an instance of a class to be constructed, the compiler invokes constructors of all the superclasses along the inheritance chain
- Super() or super(arguments) must be the first statement of the subclass's constructor
Polymorphism
- Existing in many forms
- Idea: Program in the general rather than program in the specific
- Writing programs to objects that share the same superclass directly or indirectly as though they are all objects of the superclass
- A variable of a supertype can refer to a subtype object
- Method displayObject() takes a parameter of the GeometricObject type
- You can invoke displayObject() by passing any instance of GeometricObject
UML - Inheritance
- Superclass => GeometricShape
- Subclass => Circle
- Public class Circle extends GeometricShape
- Superclass fields can only be accessed by calling superclass setters and getters or via constructor
UML - Inheritance Example
- Subclass extends superclass
- Public class Rectangle extends GeometricShape {}
- The keyword extends makes Rectangle class to inherit from GeometricShape class
- Methods getColor, setColor, isFilled, setFilled, and toString can be inherited from superclass
Learn about polymorphism, its uses, and how to process objects sharing the same superclass. Also, understand how to use the Scanner class in Java to get input from users.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free