0% found this document useful (0 votes)
4 views

javamod3

The document provides an overview of Java classes, objects, methods, and various programming concepts such as inheritance, method overloading, and visibility control. It explains how to define classes, create objects, and utilize constructors, along with the significance of static members and final classes. Additionally, it includes examples of using wrapper classes and arithmetic operations with Integer objects.

Uploaded by

lisha4206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

javamod3

The document provides an overview of Java classes, objects, methods, and various programming concepts such as inheritance, method overloading, and visibility control. It explains how to define classes, create objects, and utilize constructors, along with the significance of static members and final classes. Additionally, it includes examples of using wrapper classes and arithmetic operations with Integer objects.

Uploaded by

lisha4206
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

MODULE - 3

Classes , objects and Methods:

● Anything we wish to represent in a Java Program must be and capsulated in a


class that defines the state and behaviour of the basic program components
known as objects.

● Classes create objects and objects used methods to communicate between


them.

● Classes provide a convenient method for packing together a group of logically


related data items and function that work on them.

● In Java the data items are called fields and the functions are called methods.

● Calling a specific method in a object is described as sending the object a


message.

● A class is essentially a description of how to make an object that contains fields


and methods.

● It provides a thought of template for an object and behaves like a basic data type
such as int.

Defining a Class:

● A class is a user defined data type with a templates that serves to define its
properties.

● Once the class type has been defined we can create variables of the type using
declarations that are similar to the basic data type declarations.

● In Java this variables are termed as instances of classes which are the actual
objects.
The basic form of a class definition is:

● Because the body is empty this class does not contain any properties and their
for cannot do anything, we can however compile it and even create objects using
it.

● Classname and superclassname are valid Java identifiers.

● The keyword extends indicate that the properties of the superclass name class
are extended to the classname class. This concept is known as inheritance.

● Fields and methods are declared inside the body.

Fields Declaration:

● Data is encapsulated in a class by placing the data fields inside the body of the
class definition.

● These variables are called instance variables because they are created
whenever and object of the classes instantiated.

● We can declare an instance variables exactly the same way as we declare local
variables.
METHODS DECLARATION:

● Methods are declared inside the body of the class but immediately after the
declaration of instance variables.

● The general form of a method declaration is:

● The type specify is the type of value the method would return.

● This could be a simple data type such as int as well as any class type.

● It could even be void type if the method does not return any value.

● The method name is a valid identifier.

● The parameter list is always enclosed in parenthesis.

● The list contains variable names and types of all the values we want to give to
the method as input.

● The variables in the list are separated by commas.

● In the case where no input data are required the declaration must retain the
empty parentheses.
Example:

● Note that the method has a return type of void because it does not return any
value.

● We pass two integer values to the method which are then assign to the instance
variables length and width.

● The getData method is basically added to provide values to the instance


variables.

● We can directly use the length and width inside the method.

Assume that we want to compute the area of rectangle Defined by the class. This can
be done as follows:
● The new method rectArea() computes area of the rectangle and returns the
result.

● Since the result would be an integer the return type of the method has been
specified as it.

● Also not that the parameter list is empty.


CREATING OBJECTS:

● An object in Java is essentially a block of memory that contains Space to store all
the instance variables.

● Creating an object is also referred to as instantiating an object.

● Objects in Java are created using the new operator.

● The new operator create an object of the specified class and returns the
reference to the object.

Here is an example of creating an object of type Rectangle:


ACCESSING CLASS MEMBERS:

● All variables must be assigned values before they are used.

● Since we are outside the class we cannot access the instance variables and the
methods directly.

● To do this we must use the concerned object and the dot operator as shown
below:
CONSTRUCTORS:

● Java supports a special type of method called a constructor that enables and
object to initialize itself when it is created.

● Constructors have the same name as the class itself.

● Secondly they do not specify a return type not even void.This is because they
return the instance of the class itself.
METHOD OVERLOADING:

● In Java it is possible to create methods that have the same name but different
parameter list and different definitions. This is called method overloading.

● Method overloading is used when objects are required to perform similar task but
using different input parameters.

● When we call a method in an object ,java matches of the method name first and
then the number and type of parameters to decide which one of the definitions to
execute this process is known as polymorphism.

● To create an overloaded method all we have to do is to provide several different


method definitions in the class all with the same name ,but with different
parameter lists.

Here is an example of creating an overloaded method:


STATIC MEMBERS

● The members that are declared static as shown are called static members.

● Since these members are associated with the class itself rather than individual
objects the static variables and static methods are often referred to as class
variables and class methods in order to distinguish them from there counterparts
,instance variables and instance methods.

● Static variables are used when we want to have a variable common to all
instances of a class.

● Java create only one copy for a static variable which can be used even if the
class is never actually instantiated.
NESTING OF METHODS:

● A Method can be called by using only its name by another method of the same
class this is known as nesting of methods.
INHERITANCE : EXTENDING A CLASS
● Java classes can be reused in several ways.

● This is basically done by creating new classes , reusing the properties of existing
ones.

● The mechanism of deriving a new class from an old one is called inheritance.

● The old class is known as the base class or super class or parent class and the
new one is called the sub class or derived class or Child class.

● The inheritance allows subclasses to inherit all the variables and methods of their
parent classes .

Inheritance may take different forms:

1. Single inheritance(only one super class)


2. Multiple inheritance(several super classes)
3. Hierarchical inheritance(one super class, many sub classes)
4. Multi level inheritance(derived from a derived class)

These forms of inheritance are shown in figure 8.3


● The keyword extends signifies that the properties of the superclassname are
extended to the subclassname.

● The sub class will now contain its own variables and methods as well as those of
the super class.
OVERRIDING METHODS:
● There may be occasions when we want an object to respond to the same method
but have different behaviour when that method is called.

● That means we should override the method defined in the superclass.

● This is possible by defining a method in the subclass that has the same name ,
same arguments and same return type as a method in the super class.

● Then when that method is called , the method defined in the sub classes invoked
and executed instead of the one in the super class this is known as overriding.
FINAL VARIABLES AND METHODS:

● If we wish to prevent the subclasses from overriding the members of the super
class, declare them as final using the keyword final as a modifier.

● Making a method final ensures that the functionality defined in this method will
never be altered in anyway.

● Similarly the value of a final variable can never be changed.

● Final variables behave like class variables and they do not take any space on
individual objects of the class.

FINAL CLASSES

● A class that cannot be subclassed is called a final class.

● This is achieved in Java using the keyword final as follows:


● Any attempt to inherit these classes will cause an error and the compiler will not
allow it.

● Declaring a class final prevent any unwanted extensions to the class.

FINALIZER METHODS:

● Java supports a concept called finalization which is just opposite to initialization.

● The Java Runtime is an automatic garbage collecting system .It automatically


frees up the memory resources used by the objects.

● The garbage collector cannot free the object resources such as file descriptors or
window system fonts.

● In order to free these resources we must use a finalizer method.

ABSTRACT CLASSES AND METHODS

● In Java we can indicate that a method must always be readefined in a subclass


does making overriding compulsory.

● This is done using the modifier keyword abstract in the method definition.
VISIBILITY CONTROL

● In some situations it is necessary to restrict the access to certain variables and


methods from outside the class.

● We can achieve this in Java by applying visibility modifiers to the instance


variables and method.

● The visibility modifiers are also known as access modifiers.

● Java provides 3 types of visibility modifiers: public, private and protected.

public access

when We declare a variable as public , that is made visible to all the classes outside the
class

friendly access

● When no access modifier is specified, defaults to a limited version of public


accessibility known as friendly level of access.

● The difference between the public access and the friendly access is that the
public modifier makes fields visible in all classes regardless of their packages
while the friendly access makes fields visible only in the same package but not in
other packages

protected access

● The visibility level of a protected field lies in between the public access and the
friendly access.

● That is the protected modifier makes the fields visible not only to all classes and
subclasses in the same package but also to subclasses in other package.
private access

● private fields enjoy the highest degree of protection.

● They are accessible with only in their own class.

● They cannot be inherited by subclasses and therefore not accessible in sub


classes.

● A method declared as private behaves like a method declared as final.

● It prevents the method from being subclassed.

private protected access


Program on vector class
Output:
Wrapper class program

public class WrapperClassDemo {


public static void main(String[] args) {
// Creating Integer objects using different constructors and methods

// Using constructor
Integer num1 = new Integer(10); // Wrapping primitive int 10

// Using valueOf method


Integer num2 = Integer.valueOf("20"); // Parsing string "20" to Integer

// Auto-boxing - assigning primitive int directly to Integer


Integer num3 = 30;

// Displaying the values


System.out.println("num1: " + num1);
System.out.println("num2: " + num2);
System.out.println("num3: " + num3);

// Performing arithmetic operations


int sum = num1 + num2; // Auto-unboxing and addition
int difference = num3 - num1; // Auto-unboxing and subtraction

// Displaying the results of arithmetic operations


System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);

// Converting Integer to int


int num2Primitive = num2.intValue(); // Unwrapping Integer to int

// Displaying the converted value


System.out.println("num2 as primitive int: " + num2Primitive);
// Parsing a string to get an Integer
String numStr = "40";
Integer parsedNum = Integer.parseInt(numStr);

// Displaying the parsed Integer


System.out.println("Parsed Integer from string: " + parsedNum);

// Using wrapper class methods


System.out.println("Max between num1 and num2: " + Integer.max(num1, num2));
System.out.println("Min between num2 and num3: " + Integer.min(num2, num3));

// Comparing Integer objects


System.out.println("num1 equals num2? " + num1.equals(num2));
System.out.println("num1 compareTo num3: " + num1.compareTo(num3));
}
}

output

You might also like