Key points,

  • Objects have two properties: state and behavior. The instance variable represents the state, whereas the method represents the behavior.
  • For Methods, the size of the instance variable is influenced.
  • Parameters are used in methods. The arguments are passed from the caller to the receiver. Arguments are items that can be passed into methods.
  • We must pass something to a method that takes a parameter. And that something has to be a valid value of the right type.
  • Methods can take the form of a return type. However, the method is declared as void. It signifies that you should not return anything.
  • If a method has numerous parameters, separate them with a comma before passing them. However, you must pass the argument in the correct sequence.
  • Arguments should be of the same type as the parameter.

How to pass arguments to the method.

Working with Encapsulation,

  • Instance variables should be hidden using private access modifiers in encapsulation, and they should only be available via getter and setter methods using public access modifiers.
  • Encapsulation’s goal is to keep implementation data hidden from consumers. When a data type is private, only other members of the same class can access it. The private data type of a class is not accessible to any other class.
  • Using public getter and setter methods, we may alter and read the private data fields. The outer class can then access those private data fields using public methods.
Structure of getter and setter method
  • Because data can only be accessible by public methods, private fields and their implementation are concealed from outside classes. Data concealing is another term for encapsulation.

Implementation steps for encapsulation,

  • Make instance variables private to prevent them from being accessed from outside the class. Only the methods of the class can be used to set and retrieve these variables.
  • The class should provide getter and setter methods to set and get the values of the fields.
Example of Encapsulation

Before it is initialized, what is the value of the instance variable?

  • It is given a value by default.

There is a distinction between an instance variable and a local variable.

  • Instance variables are defined in a class rather than a method.
  • Within a method, local variables are declared. Before they may be used, local variables must be initialized.

Primitive Vs Reference,

  • Use == to compare two primitives to see if they refer to the same object.
  • Use the equals() function to see if two objects are equal.





Works at Virtusa as Associate Engineer | Software Engineer | Java Tutor | Technology Reseacher

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Distributed Testing to speed up build times

How to Configure Firewall, Whitelist and Blacklist in a self-hosted MongoDB server

Serverless Deployment

Code generation and data generator : the best use cases

Telegram news bot using Datanews API

Design Patterns Saga #9: Real Project Situations with Adapter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gawesh Prabhashwara

Gawesh Prabhashwara

Works at Virtusa as Associate Engineer | Software Engineer | Java Tutor | Technology Reseacher

More from Medium

Inheritance and Polymorphism,

How to analyze TCP dump for slow backends

Everything you need to know about Kafka

Debugging k8s PodSecurityPolicy related issues