Object-Oriented Programming:Session 7

HelpfulBugle avatar
HelpfulBugle
·
·
Download

Start Quiz

Study Flashcards

16 Questions

What is the 'is-a' relationship in inheritance?

A child class is a more specific version of a parent class.

Which of the following is NOT supported by Java?

Multi inheritance

What is the purpose of inheritance design at the level of Employee entity?

To model the relationships between different types of Employees

What is an indirect superclass?

A superclass that is inherited two or more levels down the hierarchy

What is the benefit of software reusability using inheritance?

Reducing code duplication

What is the relationship between an object of a subclass and an object of a superclass?

An object of a subclass is a subtype of an object of a superclass

What happens to an object when all references to it are dropped?

It becomes eligible for garbage collection

What is the primary purpose of a package in Java?

To provide a namespace for classes and interfaces

What is the default access modifier for a package in Java?

No modifier (default)

What is the significance of the enum keyword in Java?

It declares a special data type with predefined constants

What is the return type of the values() method in an enum type?

An array of enum constants

What is the purpose of the default constructor in Java?

To set default values for instance variables

What is the significance of the 'this' keyword in constructor overloading?

It is used to refer to the current object

What happens to an object's instance variables when it is garbage collected?

They are removed from memory

What is the role of the Object class in Java inheritance?

It is the parent class for all Java classes

What is the term for the characteristics of an object that exist throughout its lifetime?

Object state

Study Notes

Software Reusability

  • A new class (subclass) can be created from an existing class (superclass) to inherit its data fields and behaviors.
  • The subclass can enhance the inherited behaviors and attributes with new functionalities.
  • Behaviors from the superclass can be customized by the child class.
  • The relationship between the subclass and superclass is an "is-a" relationship, where the child class is a more specific version of the parent class.
  • An object of the subclass is also an object of the superclass.

Direct vs Indirect Inheritance

  • In a class hierarchy, a direct superclass is inherited explicitly, one level down.
  • An indirect superclass is inherited two or more levels down the hierarchy.
  • Java supports both direct and indirect inheritance.

Single vs Multi Inheritance

  • Single inheritance occurs when a subclass inherits from only one superclass.
  • Multi inheritance occurs when a subclass inherits from two or more superclasses.
  • Java does not support multi inheritance.
  • However, Java supports the implementation of multiple interfaces.

Inheritance Design Level

Employee Class Hierarchy

  • Employee class has attributes: First Name (FN), Second Name (SN), Tax Number.
  • CommissionEmployee is a subclass of Employee, with additional attributes: Gross Sale, Commission Rate.
  • HourlyEmployee is a subclass of Employee, with additional attributes: Wage, Hours.
  • SalariedEmployee is a subclass of Employee, with additional attributes: Monthly Salary.
  • BasedPlusCommissionEmployee is a subclass of Employee, with additional attributes: Base Salary.

Constructors

  • A default constructor is automatically provided by the Java compiler for any class that contains no constructors.
  • The default constructor has no arguments and sets fields to default values.
  • If some initialization is needed, a constructor must be written explicitly.
  • Constructor overloading allows multiple constructors with different signatures (argument lists).
  • The specific constructor called is based on the parameters specified when "new" is executed.
  • A constructor can call another constructor using the "this" keyword, which refers to the current object.
  • The parent class for all Java classes is the Object class, providing automatic inheritance of methods like toString().

Java Object Life Cycle

  • Objects are created using the "new" keyword and a constructor.
  • Instance variables and methods are accessed using dot notation.
  • An object has characteristics (state) that exist as long as the object exists.
  • Object state characteristics can change in value during the object's lifetime.
  • Unused objects are automatically cleaned up by the garbage collector if the program holds no more reference.
  • A reference can be explicitly dropped by setting the variable holding the reference to null.

Packages

  • A package is a collection of related classes and interfaces providing access protection and namespace management.
  • Packages can have no modifier (default) or the "package" keyword.
  • Advantages of packages include determining related types, ease of programming, reusability, and ease of finding.
  • A package creates a new namespace, avoiding naming conflicts, but restricts access within the package and outside.

Creating and Using Packages

  • A package should have a right name and a package statement at the top of each class belonging to the package.
  • The package statement maps to the file system.
  • Package names should be all lower case.
  • Package members can be referred to by their fully qualified name or imported using the "import" statement.
  • Importing the entire package can be done using "import" with a wildcard (*), but be careful not to import a package within a package.

Enumerated Data Type

  • An enum type is a special data type that enables a variable to be a set of predefined constants.
  • Enums are declared with the "enum" keyword, followed by a comma-separated list of enum constants.
  • Enums extend java.lang.Enum and are implicitly public, static, and final.
  • Enums can have fields, constructors, and methods, but constructors are only available within the enum itself.
  • The static "values()" method returns an array of enum constants in the order they were declared.
  • The static "valueOf(String str)" method returns the enumerated object whose name is str.
  • The instance "name()" method returns the enum constant as a string.

Test your understanding of software reusability concepts, including creating subclasses, inheritance, and class hierarchies in object-oriented programming.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Evolution Quiz
10 questions

Evolution Quiz

ProdigiousWonder899 avatar
ProdigiousWonder899
Chapitre 4
30 questions

Chapitre 4

InfluentialEuclid avatar
InfluentialEuclid
Use Quizgecko on...
Browser
Browser