Question

In: Computer Science

How do objects enhance Java? How do objects relate to classes and methods?

How do objects enhance Java? How do objects relate to classes and methods?

Solutions

Expert Solution

Answer 1:

Objects: It is the basic unit of Object Oriented Programming and it represents the real life entities.
Real-life entities share two characteristics : they all have attributes and behavior.
An object consists of:

  • State: It is represented by attributes of an object. It also shows properties of an object.
  • Behavior: It is represented by methods of an object. It shows response of an object with other objects.
  • Identity: It gives a unique name to an object. It also grants permission to one object to interact with other objects.

Objects are required in OOPs because they can be created to call a non-static function which are not present inside the Main Method but present inside the Class and also provide the name to the space which is being used to store the data.

Example : For addition of two numbers, it is required to store the two numbers separately from each other, so that they can be picked and the desired operations can be performed on them. Hence creating two different objects to store the two numbers will be an ideal solution for this scenario.

Example to demonstrate the use of Objects and classes in OOPs

Declaring Objects (Also called instantiating a class)

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

As we declare variables like (type name;). This notifies the compiler that we will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. So for reference variable, type must be strictly a concrete class name. In general, we can’t create objects of an abstract class or an interface.

Dog tuffy;

If we declare reference variable(tuffy) like this, its value will be undetermined(null) until an object is actually created and assigned to it. Simply declaring a reference variable does not create an object.

answer 2:

Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.

Class

A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:

  1. Modifiers : A class can be public or has default access (Refer this for details).
  2. class keyword: class keyword is used to create a class.
  3. Class name: The name should begin with a initial letter (capitalized by convention).
  4. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  6. Body: The class body surrounded by braces, { }.

Constructors are used for initializing new objects. Fields are variables that provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.
There are various types of classes that are used in real time applications such as nested classes, anonymous classes, lambda expressions.

Methods always part of an object or a class (for static methods you don’t need an instance/object).

In Java 8 with functional interfaces you can write such syntax where it looks like methods (lambdas) passed by as an argument just like an object. It is indeed a functional paradigm injected into the language but behind the scene an instance/object is created from the given lambda and this instance’s method will have the lambda function as the implementation.

An object in Java has two parts:

  1. What the object knows about itself (state) - Instance Variables
  2. What the object can do (behavior) - Methods

Related Solutions

A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
Objects, classes, relationships, attributes, and methods Explain these five terms and how they are related. Then...
Objects, classes, relationships, attributes, and methods Explain these five terms and how they are related. Then provide an example to illustrate the main ideas.
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION...
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG. DO NO USE ARRAY LIST Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for each dog: dogId (int) - must be unique name (string) age (double) - cannot be less than 0 or more than 25 breed (string) sex (char) – m...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Write a Java Program using the concept of objects and classes. Make sure you have two...
Write a Java Program using the concept of objects and classes. Make sure you have two files Employee.java and EmployeeRunner.java. Use the template below for ease. (Please provide detailed explanation) Class Employee Class Variables: Name Work hour per week Hourly payment Class Methods: - Get/Sets - generateAnnualSalary(int:Duration of employment)               annual salary = Hourly payment * Work hours per week * 50 If the employee is working for more than 5 years, 10% added bonus             -void displayAnnualSalary ( )...
Objective: Create a program that uses the methods in the String and Math classes to do...
Objective: Create a program that uses the methods in the String and Math classes to do the following: Ask the user to enter two nouns Tell the user how many letters the nouns are from eachother in the dictionary. Tell the user the last letter of each of the nouns Tell the user the position of the letter "e" in each of the nouns, -1 if no "e" exists in the word. For full credit, you must use quotations around...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
how do io and po payments relate? how do senior trenches and junior trenches relate to...
how do io and po payments relate? how do senior trenches and junior trenches relate to each other? How do they pay investors?
How and why do sinking funds enhance the safety of lenders?
How and why do sinking funds enhance the safety of lenders?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT