OOP JAVA

Ace your homework & exams now with Quizwiz!

What is the definition about the Aggregation?

Aggregation is a special form of association where one class (whole) contains other classes (parts) as attributes, but the parts can exist independently of the whole. It's also a "has-a" relationship, but it's a weak relationship because the contained objects can outlive the container

What is an instance method?

An instance method belongs to an object of the class. To invoke it, you need to create an instance of the class.

Key points of a static method?

-> Can be called using the class name. -> Cannot access instance variables/methods directly since it belongs to the class. -> Typically used for utility or helper functions.

What is the difference between Abstraction and Encapsulation?

-> Encapsulation is about bundling the data and methods that operate on the data into a single unit (a class) and restricting access to it. -> Abstraction is about hiding the complexity and only exposing the essential details to the user. It focuses on what an object does rather than how it does it.

Key Concepts of Shallow copy?

-> For primitive fields (like int, boolean, char), the values are copied as-is. -> For reference fields (like other objects, arrays, collections), the memory address (reference) of the object is copied, not the object itself.

key point of instance method?

-> Requires an object to call the method. -> Can access instance variables and other instance methods of the object. -> Typically used to perform operations that are specific to a particular object.

What are the types of Association?

-> Unidirectional: One class knows about another (e.g., a teacher knows their students). -> Bidirectional: Both classes know about each other (e.g., a student knows their teacher, and the teacher knows their student).

What is the difference between == and .equals() in Java?

== compares references (memory addresses) of objects, checking if two variables point to the same object in memory. .equals() compares the content of objects for equality, depending on how it's implemented.

Example of Composition?

A House and Rooms. A room cannot exist without a house, and if the house is destroyed, the rooms are also destroyed. class Room { private String type; Room(String type) { this.type = type; } public String getType() { return type; } } class House { private List<Room> rooms; // Composition (House has Rooms) House() { rooms = new ArrayList<>(); rooms.add(new Room("Living Room")); rooms.add(new Room("Bedroom")); } public void showRooms() { System.out.println("House has the following rooms:"); for (Room room : rooms) { System.out.println(room.getType()); } } } public class Main { public static void main(String[] args) { House house = new House(); house.showRooms(); // Output: House has the following rooms: Living Room, Bedroom } }

What is deep copy?

A deep copy copies both the object and all the objects it references, effectively duplicating everything. Changes to the copied object do not affect the original.

What is a SHALLOW copy?

A shallow copy of an object copies the top-level structure, but not the objects contained within. Changes to objects referenced by the original will reflect in the copied object.

What is a static method?

A static method belongs to the class rather than an instance of the class. It can be called without creating an instance of the class and is generally used for utility or helper methods.

What is the difference between Abstract Class and Interface?

Abstract Class: Can have both abstract methods (without implementation) and concrete methods (with implementation). Can have fields (variables). Supports multiple inheritance in terms of methods, but not class inheritance (Java allows only single inheritance for classes). Interface: Can only declare methods (until Java 8, when default and static methods were introduced). Can't have fields (before Java 8). Supports multiple inheritance (a class can implement multiple interfaces).

What are the four main principles of Object-Oriented Programming?

Abstraction, Encapsulation, Inheritance, Polymorphism

What is the definition of an Association?

Association represents a relationship between two separate classes that work together but can exist independently. It's a "owns-a/works-with" relationship.

What is Encapsulation?

Bundling data (variables) and methods (functions) into a single unit, usually a class, and restricting access to some components.

What is the definition of a Composition?

Composition is a stronger form of aggregation where the contained objects cannot exist independently of the containing class. The life cycle of the parts is tied to the whole. This is a strong relationship.

What is Constructor Overloading in Java?

Constructor overloading occurs when a class has multiple constructors with different parameter lists. This allows objects to be initialized in different ways. class Car { String model; int year; // Overloaded constructors Car(String model) { this.model = model; } Car(String model, int year) { this.model = model; this.year = year; } }

Example for method overloading (compile-time polymorphism)

два метода с едно и също име и различни параметри и/или различен тип на връщане class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }

Example of an Aggregation

Example: A Library and Books. A library has books, but books can exist independently outside of the library class Book { private String title; Book(String title) { this.title = title; } public String getTitle() { return title; } } class Library { private String name; private List<Book> books; // Aggregation (Library has Books) Library(String name) { this.name = name; books = new ArrayList<>(); } public void addBook(Book book) { books.add(book); } public void showBooks() { System.out.println(name + " has the following books:"); for (Book book : books) { System.out.println(book.getTitle()); } } } public class Main { public static void main(String[] args) { Book book1 = new Book("1984"); Book book2 = new Book("To Kill a Mockingbird"); Library library = new Library("City Library"); library.addBook(book1); library.addBook(book2); library.showBooks(); // Output: City Library has the following books: 1984, To Kill a Mockingbird } }.

What is an example of an Association?

Example: A Teacher and a Student have an association — teachers teach students, and students learn from teachers, but both can exist independently. class Teacher { private String name; Teacher(String name) { this.name = name; } public String getName() { return name; } } class Student { private String name; private Teacher teacher; // Association with Teacher class Student(String name, Teacher teacher) { this.name = name; this.teacher = teacher; } public void getTeacherInfo() { System.out.println(name + "'s teacher is " + teacher.getName()); } } public class Main { public static void main(String[] args) { Teacher teacher = new Teacher("Mr. Smith"); Student student = new Student("John", teacher); student.getTeacherInfo(); // Output: John's teacher is Mr. Smith } }

What is Abstraction?

Hiding complex implementation details and exposing only essential features. It allows you to interact with objects at a higher level.

Example of Inheritance?

Superclass -> there is a method; in the sub/child class it can be "customised": class Animal { public void makeSound() { System.out.println("Some generic animal sound"); } } class Dog extends Animal { // Overrides the method in the parent class @Override public void makeSound() { System.out.println("Bark"); } }

What is Polymorphism?

The ability of different objects to respond to the same method call in their own way. It comes in two forms: -> Compile-time polymorphism (method overloading) -> Run-time polymorphism (method overriding)

What is Inheritance?

The mechanism by which one class can inherit properties and behaviors (methods) from another class. This promotes code reuse.

What is the this keyword in Java?

The this keyword refers to the current instance of the class. It is commonly used to: -> Refer to instance variables. -> Invoke current class methods or constructors. -> Pass the current instance as a parameter.

What concepts are Association, Aggregation, and Composition?

These three concepts are about relationships between objects and classes, and they are commonly used in designing software systems.

Example of shallow copy?

Using clone() or copy constructors in Java, a shallow copy copies only the references of mutable fields, not the objects they point to.

Example of deep copy?

class Address { String city; Address(String city) { this.city = city; } // Deep copy of Address public Address copy() { return new Address(this.city); // Create a new Address object with the same city } } class Person implements Cloneable { String name; Address address; // Reference to another object Person(String name, Address address) { this.name = name; this.address = address; } @Override protected Object clone() throws CloneNotSupportedException { // Create a deep copy of Person by cloning the Address object separately Person clonedPerson = (Person) super.clone(); clonedPerson.address = this.address.copy(); // Deep copy of Address return clonedPerson; } } public class Main { public static void main(String[] args) throws CloneNotSupportedException { Address address = new Address("New York"); Person person1 = new Person("John", address); Person person2 = (Person) person1.clone(); // Deep copy // Modifying the referenced object in person2 person2.address.city = "Los Angeles"; // Check if the change in person2 affected person1 System.out.println(person1.address.city); // Output: New York (person1's address remains unchanged) } }

Example for method overriding (run-time polymorphism)

примерът с кучето, където child class наследява класа от патента и си го правим къстъм: class Animal { public void sound() { System.out.println("Animal makes a sound"); } } class Cat extends Animal { @Override public void sound() { System.out.println("Meow"); } } class Main { public static void main(String[] args) { Animal myAnimal = new Cat(); // Polymorphism myAnimal.sound(); // Output: Meow } }

Example of shallow copy?

class Address { String city; Address(String city) { this.city = city; } } class Person implements Cloneable { String name; Address address; // Reference to another object Person(String name, Address address) { this.name = name; this.address = address; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); // Shallow copy } } public class Main { public static void main(String[] args) throws CloneNotSupportedException { Address address = new Address("New York"); Person person1 = new Person("John", address); Person person2 = (Person) person1.clone(); // Shallow copy System.out.println("Person1's Address: " + person1.address.city); // Output: New York System.out.println("Person2's Address: " + person2.address.city); // Output: New York // Change the address of person2 person2.address.city = "Los Angeles"; System.out.println("Person1's Address after change: " + person1.address.city); // Output: Los Angeles (also changed for person1) } }

Example of static method?

class MathUtils { public static int add(int a, int b) { return a + b; } } public class Main { public static void main(String[] args) { int sum = MathUtils.add(10, 5); // Calling the static method without creating an object System.out.println("Sum: " + sum); // Output: Sum: 15 } }

Example for Encapsulation?

class Person { private String name; // Private variable // Public method to access private variable public String getName() { return name; } public void setName(String name) { this.name = name; } } the NAME variable is private, so it can't be accessed directly outside the class. The getName() and setName() methods allow controlled access to the variable, encapsulating the data.


Related study sets

Endocrinology Quiz 4 Topics 16-21

View Set

Human Anatomy: Musculoskeletal Cases

View Set

1 - Escoger (Chapter 3)Audio You will hear some questions. Select the correct answers below based on the family tree.

View Set

Lecture 12: Brain and Cranial Nerves e.) Cranial Nerves

View Set

Lincoln Way Physical Education Swim Test

View Set

Chapter 4 Plate Tectonics and Earthquakes

View Set

He Lion, Bruh Bear, and Bruh Rabbit

View Set

(B) DPHY Chapter 9 & 10: Heart Muscle & Rhythmic Excitation

View Set