unit-II (Java) Padma
unit-II (Java) Padma
Sainikpuri, Secunderabad.
BSC V SEM
Programming in Java(CS525)
UNIT- II
K. Padma Priya
Lecturer,
Department of Computer Science,
Bhavan’s Vivekananda College,
Sainikpuri, Secunderabad.
Programming in Java
UNIT-II
Programming in Java
Unit II Syllabus:
Class Methods: Constructors--- Method Overloading --- Static
Members --- Nesting of Methods --- Inheritance --- Overriding Methods
–- Final Variables and Methods --- Final Classes --- Abstract Methods
and Abstract Classes – Visibility Control.
Arrays and Strings: One-Dimensional Array --- Two-Dimensional
Array ---String Class.
Interfaces (Multiple Inheritance): Defining Interfaces --- Extending
Interfaces -- Implementing Interfaces.
Learning Outcomes:
class Student
{
int rno;
String name;
Student() // Default Constructor
{
rno=1111;
name=“Ram”;
System.out.println(“rno=“+rno+”name=“+name);
}
} // class Student is closed
class Ma
{
public static void main(String arg[])
{
Student s1=new Student();
Student s2=new Student();
}
} // class Ma is closed
// Second Example program for Parameterized Constructor
import java.lang.*;
class Student
{
int rno;
String name;
Student(int r, String n) // Parameterized constructor
{
rno=r;
name=n;
System.out.println(“rno=“+rno+”name=“+name);
}
} // class Student is closed
class Ma
{
public static void main(String arg[])
{
Student s1=new Student(11, ” Ram”);
Student s2=new Student(22, ” Laxman”);
}
} // class Ma is closed
Method Overloading in Java
Suppose you have to perform addition of the given numbers but there
a(int,int) for two parameters, and b(int,int,int) for three parameters then
In this example, we have created two methods that differs in data type.
The first add method receives two integer arguments and second add
class Student{
int rollno;
String name;
static String college;
s1.display();
s2.display();
}
}
S
// Example program for Method Overriding
class Parent class Over
{ {
int x; public static void main(String arg[])
void display() {
{ Child oo=new Child();
x=200; oo.display();
} }
} // class Parent is closed } // class Over is closed
class Child extends Parent
{
int y;
void display()
{
y=400;
System.out.println(“x=“+x+”y=“+y);
}
} // class Child is closed
Abstract Class
A class which contains the abstract keyword in its declaration is known as abstract class.
•Abstract classes may or may not contain abstract methods, i.e., methods without body ( public void get(); )
•But, if a class has at least one abstract method, then the class must be declared abstract.
•If a class is declared abstract, it cannot be instantiated.
•To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
•If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
A Java abstract class is a class which cannot be instantiated, meaning you cannot create new instances of an
abstract class. The purpose of an abstract class is to function as a base for subclasses. This Java abstract class
tutorial explains how abstract classes are created in Java, what rules apply to them. This tutorial gets into the
purpose of abstract classes in Java in more detail towards the end of this text.
}}
final keyword
1) final variable
final variables are nothing but constants. We cannot change the value
of a final variable once it is initialized. Lets have a look at the below
code:
class Demo
{
final int MAX_VALUE;
void myMethod()
{
MAX_VALUE=101;
}
public static void main(String args[])
{ Demo obj=new Demo();
obj.myMethod();
}
}
final class
A final class not be inherited. If method parameters are declared final then the
variable in all CAPS.) final, finally and finalize are three different terms. finally is
J ava array is an object that contains elements of similar data type. I t is a data structure
where we store similar elements. We can store only fixed set of elements in a java array.
Array in java is index based, first element of the array is stored at 0 index.
o Multidimensional Array
Single Dimensional Array in java
Syntax to Declare an Array in java
1. dataType[] arr; (or)
2. dataType arr[];
Instantiation of an Array in java
arrayRefVar=new datatype[size];
Example of single dimensional java array
class Testarray{
public static void main(String args[]){
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]); }}
Multidimensional array in java
In such case, data is stored in row and column based index (also known as matrix form).
Syntax to Declare Two dimensional Array in java
dataType[][] aa; (or)
dataType aa[][]; (or)
}}
Strings:
Creating a String
There are two ways to create a String in Java
1. String literal
2. Using new keyword
String literal
In java, Strings can be created like this: Assigning a String literal to a String
instance:
Method bodies exist only for default methods and static methods.