Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Unit 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Object Oriented Programming Concepts

1.Fundamentals of Classes
A class is a blueprint for creating objects in programming.
It defines a set of attributes (properties) and methods (functions) that objects
created from the class will have.
Attributes are variables that hold data, and methods are functions that perform
actions on the data or interact with the object in some way.
For example, a class called "Person" might have attributes like "name" and "age",
and methods like "speak" and "walk".
When you create an object (also called an instance) from a class, it inherits all of the
attributes and methods defined in the class.
You can create multiple objects (instances) from the same class, and each object will
have its own values for the attributes.
Classes can be used to model real-world objects, or to organize code into reusable
and modular components.

2.A Simple Class


A simple class is a basic example of a class that can be used to understand the
basic structure and functionality of classes.
In Java, a class is defined using the "class" keyword, followed by the name of the
class.
Inside the class, you can define fields and methods.
Fields are variables that hold data for each instance of the class.
Methods are functions that perform actions on the data or interact with the object
in some way.
For example, here's a simple class in Java called "Person" that has two fields (name
and age) and one method (speak):
Person person = new Person("Alice", 25);
3.Creating Class Instances
In object-oriented programming, a class is a blueprint for creating objects. Once
you have defined a class, you can create instances (or objects) of that class, which have
their own unique properties and methods.

To create an instance of a class in Java, you use the `new` keyword followed by the class
name and parentheses. For example, if you have defined a class named `Car`, you can
create a new instance of the `Car` class like this:
Car myCar = new Car();

4.Adding methods to a class


To add a method to a class in Java, you define it within the class definition using
the following syntax:
public returnType methodName(parameterList) {
// method body
}
Here, `public` is an access modifier that specifies the visibility of the method (in this case,
it is public and can be accessed from anywhere), `returnType` is the data type of the
value returned by the method (or `void` if it does not return a value), `methodName` is
the name of the method, and `parameterList` is a comma-separated list of parameters
that the method takes (or an empty parameter list if it takes no arguments).

5.Calling Functions/Methods
In object-oriented programming, you can call functions or methods to perform
operations on objects. To call a method on an object, you use the dot notation, which
involves specifying the object name, followed by a dot (.), and then the method name.

Here is the syntax for calling a method on an object in Java:


objectName.methodName(arguments);
Here, `objectName` is the name of the object on which the method is being called,
`methodName` is the name of the method, `and arguments` is a comma-separated list of
arguments (if the method takes any).

6.Abstraction
Abstraction is one of the fundamental concepts in object-oriented programming.
It is the process of hiding the implementation details of a system or object and exposing
only the relevant information or interface to the user. In other words, abstraction allows
you to focus on what an object does, rather than how it does it.
public abstract class Shape {
public abstract double getArea();
public abstract double getPerimeter();
}

7. Encapsulation
Encapsulation is a fundamental concept in object-oriented programming (OOP)
that refers to the practice of keeping the internal details of an object hidden from the
rest of the program. The idea behind encapsulation is to provide a way to ensure that
the data within an object is only accessible through specific methods that are defined for
that object.

8.Using 'this' Keyword


In Java, the `this` keyword is used to refer to the current instance of a class. It can
be used to access or modify the instance variables of the class or to invoke the methods
of the current object.
The main purpose of using `this` keyword is to resolve any ambiguity that might arise
between the instance variables and local variables or parameters with the same name.

9.Constructors
Constructors are special methods in a class that are used to initialize the object
of that class. They are called when an object of the class is created. The constructor has
the same name as that of the class, and it does not have any return type, not even void.

In Java, there are two types of constructors:


*1.Parameterized constructors (no need to explain not in syllabus)*

2.Default constructors
A default constructor is a constructor that is provided by Java if a class does not
have any constructor defined explicitly. The default constructor is a no-argument
constructor, which means it takes no parameters.

The default constructor is an empty constructor that does nothing, except initializing the
object with default values. For example, if the class has instance variables of type `int`,
`double`, `boolean`, etc., then the default constructor initializes them to `0`, `0.0`, `false`,
etc.

10.More on methods:

Passing by value: When you pass a primitive data type (like int, double, boolean,
etc.) as an argument to a method, it is passed by value. This means that a copy of
the value is passed to the method, and any changes made to the value inside the
method do not affect the original value.

Passing by reference: When you pass an object as an argument to a method, it is


passed by reference. This means that the method receives a reference to the
object, not a copy of the object. Any changes made to the object inside the
method affect the original object.

Access control: You can use access modifiers (like public, private, protected, etc.)
to control the access to methods. For example, a private method can only be
accessed within the same class, while a public method can be accessed from any
class.
Methods that return values: A method can return a value using the return
statement. The return type of the method must match the type of the value
being returned. For example, a method that returns an int value would have a
return type of int.

Polymorphism and method overloading: Polymorphism is the ability of an


object to take on many forms. Method overloading is a feature of Java that
allows a class to have two or more methods with the same name, but with
different parameters. This allows the same method name to be used for different
purposes.

Recursion: Recursion is a programming technique where a method calls itself to


solve a problem. Recursion can be a useful technique for solving complex
problems, but it can also lead to stack overflow errors if not used correctly.

Nested and inner classes: A nested class is a class that is defined inside another
class. An inner class is a non-static nested class that has access to the instance
variables and methods of the outer class. Nested and inner classes can be useful
for organizing code and creating more modular and reusable code.

You might also like