Java Unit 3
Java Unit 3
Java Unit 3
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.
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();
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.
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.
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.
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.
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.
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.