2BSC(Ds)&MPCS java unit-2 notes(1)
2BSC(Ds)&MPCS java unit-2 notes(1)
Example of an Object:
Declaration
Instantiation
Initialization
Syntax:
ClassName objectname= new ClassName();
Example:
Addition obj=new Addition();
Example Program:
Class Addition
{
Int a,b,c; //instance variables
Void sum() //method
{
A=10;
B=20;
C=a+b;
System.out.println(“addition of two numbers is: “ +c);
}
Class Maddition
{
Public static void main(String args[])
{
Addition obj=new Addition(); //object creation
Obj.sum();
}
}
************
Initializing The Instance Variables:
Instance Variable:
The variables that are declared inside the class but
outside the scope of any method are called instance
variables in Java.
The instance variable is initialized at the time of the
class loading or when an object of the class is created.
An instance variable can be declared using different
access modifiers available in Java like default, private,
public, and protected.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
*************
Constructors:
Java constructors are special types of methods that are used to
initialize an object when it is created. It has the same name as
its class and is syntactically similar to a method. However,
constructors have no explicit return type.
Syntax:
Class classname
{
Classname()
{
//statements;
}
}
Rules for Creating Java Constructors
You must follow the below-given rules while creating Java
constructors:
Default Constructor:
If you do not create any constructor in the class, Java provides a
default constructor that initializes the object.
(or)
A constructor that have no parameter is known as default
constructor.
Syntax:
Class ClassName
[
ClassName()
{
Constructor body;
}
}
2.parameterized constructor:
***********
Chapter – 4
Methods in java
Method:
A method is a block of code which only runs when it is called.
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also
known as functions.
Methods must be declared inside the body of the class but
immediately after the declaration of instance variables.
The general form of a method declaration is :
Syntax:
returnType methodname(parameter list)
{
body;
}
A method has two parts:
1.Method header or Method prototype.
2.Method Body
Method header or prototype:
It contains method name, method parameters and method
return data type. Method prototype is written in the form:
Syntax: returntype methodname(parameter1 parameter2….)
Example: void sum(n1,n2)
Method body:
Below the method header, we should write the method body.
Method body consists of a group of statements which contains logic
to perform the task. Method body can be written in the following
format:
syntax:
{
statements;
}
example: {
double=a+b;
System.out.println(“sum of two=” +c);
}
Program:
class Method
{
int a,b,c;
void sum()
{
a=10;
b=20;
c=a+b;
System.out.println(“addition of a and b is=” +c);
}
}
class Method1
{
public static void main(String args[])
{
Method obj=new Method();
obj.sum();
}
}
output: Addition of a and b is=30
******************
Recursion:
A method calling itself is known as ‘recursive method’ and that
phenomenon is called “recursion”.
example:
//factorial with recursion
class Recursion
{
static long factorial(int num)
{
long result;
if(num==1)
return 1;
result=factorial(num-1)*num;
return result;
}
public static void main(String args[])
{
Ssytem.out.println(“factorial of 5 is:”);
System.out.println(Recursion.factorial(5));
}
}
output: Factorial of 5 is:
120
*************
Chapter - 5
Inheritance
Inheritance is a mechanism in which one class acquires all
properties and behaviours of another class with the specific
relationship.
Inheritance is one of the key features of OOP that allows
us to create a new class from an existing class.
The new class that is created is known as subclass (child
or derived class) and the existing class from where the
child class is derived is known as superclass (parent or
base class).
The extends keyword is used to perform inheritance in
Java.
Syntax:
Class A
{
Body of class A;
}
Class B extends A
{
Statements;
}
Types of Inheritance:
1.Single inheritance
2.multilevel inheritance
3.hierarchical inheritance
4.Multiple inheritance.
**************
The Keyword ‘super’ :
The “super” keyword is used in Java to refer to the immediate parent
class of a subclass. It provides a means to access and call the
members (methods and fields) of the parent class from within the
context of the subclass. This is particularly useful when both the
parent and child classes have members with the same names.
If we create an object to super class, we can access only the
super class members, but not the sub class members. But if we
create sub class object, all the members of both super and sub
classes are availableto it . This is the reason, we always create an
object to sub class in inheritance. Some times, the super class
members and sub class members may have same names. In that case,
by default only sub class members are accessible. In this situation we
are using “ super “ keyword in java.
Chapter:2
Introduction to oops
Object oriented programming:
Object oriented programming is an approach that provides a way of
modularizing programs by creating portioned memory area for both
data and functions that can be used as templates for creating copies
such modules on demand.
The main aim of the oop is to bind together the data and the
functions that operate on them so that no other part of the code can
access this data except that function.
Oop concepts are as follows: (or) characterstics of OOP :
Class
Object
Abstraction
Encapsulation
Inheritance
Polymorphism
Message communication
Class:
A class is a collection of objects or blueprint of an object.
It is a model to create an object. Once a class has been defined ,
we can create any number of objects belonging to that class.
Class contains variables and methods. For example,
Mango,apple,and orange are members of the class Fruit.
Ex: Fruit Mango;
Object:
Student
Object
No
Name
Data
Marks
Total()
Result()
Methods
Attributes
Feathers
Lay eggs
Polymorphism :
poly means ‘several’ and Morphism means ‘forms’. polymorphism means the ability
to take more than one form. A single function name can be used to perform different tasks. It is
extensively used in implementing inheritance.
Shape
Draw( )
` ***************
Problems in pop:
The code reusability freature is not present in procedural-
oriented programming. we have to rewrite the same
programming code many times.
Procedural code is Difficult to relate with real- world objects,
this may make it difficult to design.
we can not perform operations like encapsulation, inheritance
etc..
Procedural programming mainly focuses on procedures or
functions .
Less attention is given to the data.
The data and functions are separate from each other
The procedural programming approach does not work well for
large and complex systems.
************
chapter 1: Strings
and
chapter5: inheritance