Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, an object is an instance of a class that has state (attributes) and behavior (methods). An object represents a real-world entity, such as a person, a car, or a bank account.
Object-oriented programming is a programming paradigm that is based on the concept of objects. Object-oriented programming involves designing software systems by modeling real-world entities as objects with attributes and methods.
The three basic characteristics of object-oriented programming are:
Encapsulation: Encapsulation is the process of hiding the implementation details of an object from the outside world, and providing a simple and consistent interface for interacting with the object. Encapsulation ensures that the object's state is not directly accessible by other objects, and can only be modified through the object's methods.
Inheritance: Inheritance is the process of creating a new class by inheriting the attributes and methods of an existing class. Inheritance allows you to reuse code and build new classes that are based on existing classes. The existing class is called the superclass or base class, and the new class is called the subclass or derived class.
Polymorphism: Polymorphism is the ability of objects of different classes to be treated as if they are objects of a common superclass. Polymorphism allows you to write code that can work with objects of different types, as long as they share a common interface. Polymorphism is achieved through method overriding and method overloading.
Here's an example of a simple object in Java:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public void sayHello() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } }
In this example, we have a Person
class that represents a person. The Person
class has two attributes: name
and age
. The Person
class also has a sayHello
method that prints out a greeting that includes the person's name and age.
We can create a Person
object and call its sayHello
method like this:
Person person = new Person("John", 30); person.sayHello(); // Output: "Hello, my name is John and I am 30 years old."
In summary, an object in Java is an instance of a class that has state (attributes) and behavior (methods). Object-oriented programming is based on the concept of objects, and involves designing software systems by modeling real-world entities as objects with attributes and methods. The three basic characteristics of object-oriented programming are encapsulation, inheritance, and polymorphism.