Object-Oriented Programming Paradigm

UnparalleledCircle avatar
UnparalleledCircle
·
·
Download

Start Quiz

Study Flashcards

22 Questions

Which programming paradigm involves encapsulating both data and methods into one software entity called an object?

Object-oriented programming

What are the three pillars of object-oriented programming?

Encapsulation, Inheritance, Polymorphism

The keyword 'this' is used to refer to the instance variable of the current object.

True

The method ____ gives the String representation of an object.

toString

What is the purpose of the 'toString' method in the Rectangle2 class?

The 'toString' method is used to provide a string representation of the object, containing information about the position, width, and height of the rectangle.

Can two methods in a class have the same name?

Yes, only if they have the same signature.

Static methods in Java can be invoked without creating an object.

True

What is inheritance in object-oriented programming?

Inheritance in object-oriented programming is the process of deriving a new class based on an existing class.

What is reclaimed by the garbage collector in Java when an object is no longer in use? The memory space occupied by the _______ is reclaimed.

object

Match the following terms related to variables with their descriptions:

Static variable = Only one copy exists, not unique to individual objects Instance variable = Each object gets a separate version of this variable

Why do we use inheritance in programming?

We use inheritance in programming to inherit the properties of a superclass in a subclass, avoid repetition of instance variables, utilize methods from the parent class, and enable code extension through class hierarchies.

How can you decide if two classes have an inheritance relationship?

If there is a 'is-a' relationship

Can a method in the subclass have the same name and signature as a method in the superclass?

Yes, a method in the subclass can have the same name and signature as a method in the superclass.

What is an abstract class in Java?

An abstract class in Java is a class from which objects cannot be instantiated, and it may contain abstract methods.

What is polymorphism in Java?

Polymorphism means the ability of one object to be treated, or used, like another. It works for objects in the inheritance hierarchy, allowing a reference of a superclass type to refer to an object of its subclass type.

Polymorphism allows you to declare an object of a subclass type and instantiate an object of a superclass type using the same reference. (True/False)

False

What does the instanceof operator do in Java?

Tests if an object is of a certain type or a subclass of that type

___ works downwards in a class hierarchy from superclass to subclass.

Polymorphism

What is one advantage of using Generics in Java?

Writing a class with one set of methods that can be applied to any kind of object

What is the purpose of the 'Grade' class in the Generics Program Example?

To work for either a String object or an Integer object.

In the 'GradeDemo' class, the Grade instance 'm2' is initialized with the value __.

86

Generics in Java allow you to restrict the type of objects that can be used in a class.

False

Study Notes

Object-Oriented Programming (OOP)

  • OOP is a programming paradigm that uses objects and classes to create programs.
  • It mirrors real-world applications, where each object has its own data and methods.

Advantages of OOP

  • Models real-world applications.
  • Each programming module (object) can be changed independently of the other modules, making it flexible and scalable.
  • Only allowed data (variables) and methods can be accessed by other modules, making it less error-prone.
  • Each object can be modeled by an interface.

The Three Pillars of OOP

  • Encapsulation: The concept of bundling data and methods that operate on that data within a single unit (a class).
  • Inheritance: The ability of one class to inherit properties and behavior from another class.
  • Polymorphism: The ability of an object to take on multiple forms.

Basics of OOP in Java

  • Define objects à encapsulation.
  • Create and use the objects.
  • Two basic steps: define objects and create and use the objects.

Class File Structure

  • Fields/attributes to hold data (variable declarations, typically private).
  • Procedures/operations on the data (methods, typically public).

Example of a Simple Object-Oriented Program

  • A Rectangle class with private instance variables (xpos, ypos, width, height) and public methods (setX, setY, setWidth, setHeight, getX, getY, getWidth, getHeight).

The this Parameter

  • Refers to the current object.
  • Used to distinguish instance variables from method parameters.

toString Method

  • Gives a string representation of an object.
  • Can be designed to print the object's attributes in a convenient form.

Method Overloading

  • Can have multiple methods with the same name in a class.
  • Provided their signatures are different (method name, type, and order of input parameters).
  • Useful when we need several different ways to perform the same operation.

Garbage Collection in Java

  • The memory space occupied by an object is reclaimed by the garbage collector when the object is no longer in use.
  • Runs in the background, identifies objects without references, and reclaims the space used by the object.

Static Methods and Variables

  • Static Methods: Can be used without an object.
  • Static Variables: Only one copy exists, and individual objects do not get separate versions.

Examples of Static Methods

  • main is a static method that can be included in any class.
  • The Math class has multiple static methods (e.g., Math.pow, Math.random).### Static Variables and Methods
  • A class can have both static and instance variables.
  • A non-static method can access both instance and static variables.
  • A non-static method can invoke both static and non-static methods.
  • A static method can only access static variables and methods directly.
  • Constants are shared by all objects of the class and should be declared final static.

Inheritance

  • Inheritance is the process of deriving a new class from an existing class.
  • The new class inherits the properties of the existing class.
  • The existing class is called the superclass or base class.
  • The new class is called the subclass or derived class.
  • Inheritance is used to create a class hierarchy.
  • The subclass inherits the attributes and methods of the superclass.

Class Hierarchy

  • A class hierarchy is a tree-like structure where classes are arranged in a hierarchical order.
  • The superclass is at the top of the hierarchy, and the subclasses are below.
  • Inheritance is used to create a class hierarchy.
  • The class hierarchy can be extended as required.

Programming Inheritance

  • Inheritance is programmed using the "extends" keyword in the class declaration.
  • The subclass inherits the properties of the superclass.
  • The subclass can also add its own attributes and methods.

Superclass, Subclass, and UML Notation

  • The superclass is the generalized class.
  • The subclass is the specialized class.
  • The UML notation is used to represent the inheritance relationship between classes.

Advantages of Inheritance

  • Code modularization
  • Code reusability
  • Accessibility of methods by other classes
  • Code extension by creating class hierarchies

Deciding on Inheritance

  • If there is an "is-a" relationship between classes, then it is inheritance.
  • Examples: Car is a Vehicle, Student is a Person, Monkey is an Animal.

Method Overriding

  • A method in the subclass can have the same name and signature as a method in the superclass.
  • The method in the subclass overrides the method in the superclass.

Abstract Class

  • An abstract class is a class from which objects cannot be instantiated.
  • An abstract class defines variables and methods.
  • Methods can also be abstract, with just a header and no body.
  • Subclasses must implement the abstract methods.

Interface

  • An interface is a collection of method declarations with no data and no bodies.
  • Any class that implements an interface guarantees to implement all the methods.
  • An interface enforces an API in software design.

The Object Class

  • In Java, every class inherits implicitly from the Object class.
  • The Object class provides utility methods such as equals, toString, and clone.
  • The Object class also provides methods for multi-threading.

Polymorphism

  • Polymorphism means the ability of one object to be treated, or used, like another.
  • Polymorphism works for objects in the inheritance hierarchy.
  • A reference of a superclass type can be used to refer to an object of its subclass type.### Polymorphism
  • Polymorphism allows you to declare an object of a superclass type and instantiate an object of a subclass using the same reference.
  • Example: A obj; obj = new A(); obj = new B(); obj = new C(); where A is the superclass, and B and C are subclasses.
  • This allows for flexibility in programming, as the same reference can be used to create objects of different types.

instanceof Operator

  • The instanceof operator is used to check if an object is of a certain type or a subclass of that type.
  • Example: obj1 instanceof Jedi returns true if obj1 is a Jedi object or a subclass of Jedi.
  • It can be used to check the type of an object at runtime.

Casting

  • Casting is used to convert an object of a superclass type to a subclass type.
  • Example: GameEntity g1 = new Jedi(); Jedi j2 = (Jedi) g1; where g1 is a GameEntity object and j2 is a Jedi object.
  • Casting can be used to work with objects of different types, but it can also lead to errors if the object is not of the correct type.

Generics

  • Generics are a way to create classes that can work with objects of different types.
  • Example: public class MyCollection { ... } where T is a type parameter.
  • Generics allow for flexibility in programming, as the same class can be used to work with objects of different types.

Generics Example

  • A Grade class is created that can work with either String objects or Integer objects.
  • The Grade class has a constructor, get and set methods, and a toString method.
  • The Grade class is used in a client program to create objects of different types.

Benefits of Generics

  • Generics allow for flexibility in programming, as the same class can be used to work with objects of different types.
  • Generics reduce code duplication, as the same class can be used to work with objects of different types.
  • Generics make the code more reusable and easier to maintain.

Test your understanding of object-oriented programming concepts, including encapsulation, the pillars of OOP, and method usage.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Master Object-Oriented Programming
10 questions
Object-Oriented Programming Paradigms Quiz
15 questions
TEMA 1: 3.Paradigmas de Programación
10 questions
Use Quizgecko on...
Browser
Browser