Core Java
Core Java
Note- For accessing the member of a class you must create the object of that class like the
following-
objectname.methodname();
1. Method with no argument & no return value- if you want to perform any static operation,
then use such type of method.
ex.
**************************************************************************
Sushant T College
**************************************************************************
**************************************************************************
Sol-
class logic
int i;
for(i=1;i<=70;i++)
System.out.print("*");
System.out.println();
class test
obj.Line();
System.out.println("\t\t\tSushant IT College");
obj.Line();
obj.Line();
}
______________________________________________________________________________
___
2. Method with argument but no return value- if you want to perform any dynamic operation
according to given value, but the oocurance of result is more than one then you can use such
type of method.
ex.
**************************************************************************
Sushant T College
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sol-
class logic
int i;
for(i=1;i<=70;i++)
System.out.print(ch);
System.out.println();
}
class test
obj.Line("*");
System.out.println("\t\t\tSushant IT College");
obj.Line("&");
obj.Line("^");
Que- Create a Method named as table() which contains a number as argument & print the
table of given number.
_________________________________________________________
3. Method with argument and return value- if you want to perform any dynamic operation
by a given value & result is an aggregate value then you can use such type of methods.
Que- Create a Method named as max() which contains three numbers as argument & return
the largest one.
Sol-1
class logic
{
int m;
m=a;
m=b;
else
m=c;
return m;
class test
int res=obj.max(100,20,4);
System.out.println("Largest : "+res);
Que- Create a method named as fact() which contains a number as argument & return the
factorial of that number.
Que- Create a method named as power() which contains base number(x) and power
number(n) as argument & return x to the power n.
_______________________________
4. Method with no argument but return value- Such Kinds of method works on the members
of class-
for ex.
class logic
int a,b,c;
a=x;
b=y;
c=z;
int m;
m=a;
m=b;
else
m=c;
return m;
System.out.println("Largest : "+max());
class test
obj.get(100,20,4);
obj.show();
_______________________________________________________________________
Que-Create an overoaded method named as reverse() which reverse() the digits of a number
or characters of a string as per the given argument.
Que- Create an overloaded method named as lcm() which return the lcm of either 2 , 3 or 4
given values.
Sol-2
class logic
int m,i,res;
m=a*b;
for(i=1;i<=m;i++)
res=i;
break;
return res;
m=a*b*c;
for(i=1;i<=m;i++)
{
res=i;
break;
return res;
m=a*b*c*d;
for(i=1;i<=m;i++)
res=i;
break;
return res;
}
class test
______________________________________________________________________________
_______
Constructors- They are used for initilizing the members of a class. the propertoes of
constructors will be-
Types of Constructors
1. Default Constructors
2. Parametrized Constructors
3. Overloaded Constructors
1. Default Constructors- If a constructors did not contains any argument then it is known as
default constructors.
eg.
class cons
String name;
int age;
public cons()
name="Bzar";
age=24;
System.out.println("Name : "+name);
System.out.println("Age : "+age);
class test
obj.show();
}
___________________________________________________________
2. Parametrized Constructors- The constrctors which takes the arguments are known as
parametrized constructors. the value of argument must be passed at the time of object
creation.
class cons
String name;
int age;
name=n;
age=a;
System.out.println("Name : "+name);
System.out.println("Age : "+age);
class test
{
public static void main(String args[])
obj.show();
______________________________________________________________________________
__
3. Overloaded Constructors - If a class contains more than one construtor then this term is
known as overloaded constructor.
class Line
int i;
public Line()
for(i=1;i<=70;i++)
System.out.print("*");
System.out.println();
{
for(i=1;i<=70;i++)
System.out.print(ch);
System.out.println();
class test
Line obj;
obj=new Line();
obj=new Line("&");
_________________________________________________________
for definiting a sub class you must follow the folliwng syntax-
class subclassname extends superclassname
Note - remember only public member of super class inherited in sub class.
class first
privateone();
}
class sec extends first
class test
obj.one();
obj.two();
________________________________________
using this keyword- We can use two variations of this keyword, these are-
1. this statement
2. this() method
int a,b;
this.a=a;
this.b=b;
class test
obj.get(10,20);
obj.put();
}
}
____________________________________________________
this () method- It is used for calling the constructor of class inside the another constructor of
same class.
class thdemo
public thdemo()
System.out.println("First Constructor");
public thdemo(int n)
this();
System.out.println("Second Constructor");
class test
}
}
___________________________________________________________________________
using super keyword- like this we can also use two variations of super keyword-
1. super statement
2. super method
1. super statement - this statement is used for accessing the member of super class inside sub
class.
class first
int a,b;
this.a=a;
this.b=b;
int a,b;
this.a=super.a;
this.b=super.b;
}
class test
obj.get(10,20);
obj.set();
obj.show();
________________________________
2. super() method- It is used for calling the constructor of super class inside the sub class.
class first
{
public first()
System.out.println("First Constructor");
public first(int n)
System.out.println("Second Constructor");
public sec()
super(10);
class test
_______________________________________________________________
1. static variable
2. static methods
3. static class
1. static variable- The are known as class level variables & they share a single copy of the
variable to each object of a class.
class stdemo
num++;
System.out.println("Value : "+num);
class test
{
public static void main(String args[])
obj1.set();
obj2.set();
obj3.set();
obj1.get();
obj2.get();
obj3.get();
______________________________________________________________________________
______
static methods-if you declare a method as static then you can call the particular method along
with the class name without creating the object.
class stdemo
}
class test
stdemo.msg();
______________________________________________
static class - if a class contains the static members then it is known as static class.
method overriding- if we define a method is sub class which signature exactly mathched with
any other method of super class , then this term is known as method overriding.
class first
{
public void msg()
super.msg();
class test
obj.msg();
______________________________
1. final variable
2. final method
3. final class
1. final variable- if you declare a variable as final then you can not change the value of that
variable during execution of the program. Means it will be a constant variable.
class test
{
System.out.println(a);
//a=20;
//System.out.println(a);
______________________________
2. final method - if you define a method as final then you can not override the particular
method.
class first
super.msg();
System.out.println("This is Method of Sub Class");
class test
obj.msg();
_________________________
3. final Class- if you define a class as final means you can not inherit the particular class
{
public void msg()
super.msg();
class test
obj.msg();
_______________________________________________________
interface & abstract class- They are used for desining the model of a system.
interface- it contains the list of unimplemented methods & defines the responsibility of a
class.
Syntax
interface interfacename
method prototype-1
method prototype-2
___________________
___________________
Note - if you implements an interface in your class then you must override all the methods
whatever is declared inside the interface.
interface qamarwork
int rev=0,r;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
return rev;
return n==reverse(n)?true:false;
class test
if(obj.palindrome(121))
System.out.println("Number is Palindrome");
else
}
}
__________________________________________
abstract class- it same like interface but difference is that it contains implemented &
unimplemented methods both. and methods which will be unimplemented must define
asbtract methods. we inherit the abstract class but could not create the object of abstract
class.
int rev=0,r;
while(n>0)
r=n%10;
rev=rev*10+r;
n=n/10;
return rev;
{
public boolean palindrome(int n)
return n==reverse(n)?true:false;
class test
if(obj.palindrome(121))
System.out.println("Number is Palindrome");
else
But there are many differences between abstract class and interface that are given
below.
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only
abstract methods. abstract methods. Since Java 8, it can
have default and static methods also.
3) Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.
6) An abstract class can extend another Java An interface can extend another Java
class and implement multiple Java interfaces. interface only.
8) A Java abstract class can have class Members of a Java interface are public
members like private, protected, etc. by default.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves
fully abstraction (100%).
_______________________________________________________________
packages- It is like a folder which contains classes, and we can import them whenever is
required.
1. create a folder in your system with the same name which you want for your package. for
eg opr
package opr;
int rev=0,r;
while(n>0)
r=n%10;
rev=rev*10+r;
n=n/10;
return rev;
return n==reverse(n)?true:false;
}
}
import opr.*;
class test
if(obj.palindrome(121))
System.out.println("Number is Palindrome");
else
set path=java_path;package_folder_path
__________________________________________________________
Exception Handling-
In a program there may be 3 types of errors could be generated these are-
2. Logical Errors
Compile time errors are traced by the compiler and without debuging this your
program could not execute.
Logical erros are occured due to invalid logic of the programmer and this could be
traced by a programmer.
Run Time Errors- they are known as execeptions and when they occured they terminated the
execution a program.
The exception handling is a techniqe which allows you form handling run time exceptions.
java provides a block named as try.. catch.. block which is used for handling any exeception.
Syntax
try
java code....
}catch(ExceptionClassName objectName)
Message/Action
finally
Message/Action
}
note - finally is an optional block which execute in each case either exception is occured or
not?
import java.util.*;
class test
int a,b,c;
a=sc.nextInt();
b=sc.nextInt();
try{
c=a/b;
System.out.println("Result : "+c);
}catch(Exception e)
System.out.println("Problem in Divide");
finally
System.out.println("Bye..Bye");
}
Built-in Exception Classes- java provides a lot of built in exception classes, which are used for
handling the exception as per the exception type.
Classes Meaning
using single try with multiple catch- if you want to handle the exception according to their
exception type then you can use single try with multiple catch-
class exec
int i;
for(i=1;i<=3;i++)
{
try{
switch(i)
case 1:
int a=10,b=0;
System.out.println(a/b);
break;
case 2:
String s="Bzar";
System.out.println(s.charAt(10));
break;
case 3:
System.out.print(arr[80]);
break;
}catch(ArrayIndexOutOfBoundsException ae)
catch(StringIndexOutOfBoundsException se)
catch(ArithmeticException e)
System.out.println("Number is / By 0");
________________________________________________________________________
throws - if you are not intrested to handle the run time exceptions then you can use throws
statement
import java.io.*;
class demo
int a,b,c;
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=a+b;
System.out.println("Sum : "+c);
import java.util.*;
class ownexec
else
class test
int num;
System.out.print("Enter Number :");
num=sc.nextInt();
try{
obj.inrange(num);
}catch(Exception e)
System.out.println("Error : "+e.getMessage());
_______________________________________________