http://www.javatpoint.com/java-oops-concepts
1) http://www.tutorialspoint.com/compile_java_online.php
http://goo.gl/EGmh0Z
http://goo.gl/xD6PuL
2) ls -> for listing
dir
3) save file with name sree.java
4) javac sree.java
5) ls
>>> sree.java sree.class
6) java sree
-------------------------------------------------------------
Example of an instance variable:
class Taxes { int count; /*...*/ }
Class variables – also known as static member variables
Example of a class variable:
class Taxes { static int count; /*...*/ }
----------------------------------------------------------------------------
lass variables only have one copy that is shared by all the different objects of a class, whereas every object has it’s own personal copy of an instance variable. So, instance variables across different objects can have different values whereas class variables across different objects can have only one value.
Advantage of naming conventions in java
By using standard Java naming conventions, you make your code easier to read for yourself and for other programmers. Readability of Java program is very important. It indicates that less time is spent to figure out what the code does.
Name | Convention |
---|---|
class name | should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc. |
interface name | should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc. |
method name | should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc. |
variable name | should start with lowercase letter e.g. firstName, orderNumber etc. |
package name | should be in lowercase letter e.g. java, lang, sql, util etc. |
constants name | should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc. |
Class in Java
A class is a group of objects that has common properties. It is a template or blueprint from which objects are created. |
A class in java can contain:
- data member
- method
- constructor
- block
- class and interface
Syntax to declare a class:
==================================================================
class Student
{
String name;
public static void main(String[] args)
{
Student myname=new Student();
Student hername=new Student();
myname.name="sree";
hername.name="sahi";
System.out.println(myname.name);
System.out.println(hername.name);
}
}
==================================================================
class Info{
int roll;
String name;
void insert(int r, String n)
{
roll=r;
name=n;
}
void display()
{
System.out.println("my name is "+name +"and my roll no is "+roll);
}
public static void main(String[] args)
{
Info student1=new Info();
Info student2=new Info();
student1.insert(99,"Sree");
student2.insert(2,"Sahi");
student1.display();
student2.display();
}
}
int roll;
String name;
void insert(int r, String n)
{
roll=r;
name=n;
}
void display()
{
System.out.println("my name is "+name +"and my roll no is "+roll);
}
public static void main(String[] args)
{
Info student1=new Info();
Info student2=new Info();
student1.insert(99,"Sree");
student2.insert(2,"Sahi");
student1.display();
student2.display();
}
}
==================================================================
What are the different ways to create an object in Java?
There are many ways to create an object in java. They are:
========================================================
Annonymous object
=========================================
Creating multiple objects by one type only
|
==========================================================
Method Overloading in Java
If a class have multiple methods by same name but different parameters, it is known as Method Overloading.
----------------------------------------------------------------------------------------------------------
=========================================================
Different ways to overload the method
There are two ways to overload the method in java |
|
1)Example of Method Overloading by changing the no. of arguments
In this example, we have created two overloaded methods, first sum method performs addition of two numbers and second sum method performs addition of three numbers.
2)Example of Method Overloading by changing data type of argument
In this example, we have created two overloaded methods that differs in data type. The first sum method receives two integer arguments and second sum method receives two double arguments.
----------------------------------------------------------------------------------------------------------
=========================================================
Constructor in Java
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation
Types of java constructors
There are two types of constructors:
- Default constructor (no-arg constructor)
- Parameterized constructor
Example of default constructor
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation. |
Q) What is the purpose of default constructor?
Default constructor provides the default values to the object like 0, null etc. depending on the type.
Example of default constructor that displays the default values
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor. |
Constructor Overloading in Java
Example of Constructor Overloading
class StudentDetails{
int roll;
String name;
int marks;
StudentDetails(int r,String n)
{
roll=r;
name=n;
}
StudentDetails(int r,String n,int m)
{
roll=r;
name=n;
marks=m;
}
void display()
{
System.out.println("my name is : " +name +"and my roll no is :"+ roll + "my makrs"+ marks );
}
public static void main(String [] args)
{
StudentDetails s1=new StudentDetails(9,"sree");
StudentDetails s2=new StudentDetails(2,"sahi",499);
s1.display();
s2.display();
}
}
=================================================================
Java static keyword
The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.
The static can be:
- variable (also known as class variable)
- method (also known as class method)
- block
- nested class
Example of static variable
Program of counter by static variable
As we have mentioned above, static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value. |
Java static method
If you apply static keyword with any method, it is known as static method.
- A static method belongs to the class rather than object of a class.
- A static method can be invoked without the need for creating an instance of a class.
- static method can access static data member and can change the value of it.
{
int roll;
String name;
static String clg="abc";
Student(int r,String n)
{
roll=r;
name=n;
}
static void change()
{
clg="xyz";
}
void display()
{
System.out.println("my name is "+name + " my roll is : "+ roll + " my clg "+clg);
}
public static void main(String args[])
{
Student A=new Student(1,"sri");
A.display();
Student.change();
Student B=new Student(2,"sahi");
B.display();
}
}
-----------------------------------------------------------------------------------------------------------
================================================================
this keyword in java
The this keyword can be passed as argument in the constructor call.
We can pass the this keyword in the constructor also. It is useful if we have to use one object in multiple classes. Let's see the example: |
=====================================================================
class B{
A obj;
B(A obj)
{
this.obj=obj;
}
void display()
{
System.out.println(obj.data);
{
}
}
}
class A {
int data=9;
A()
{
B b=new B(this);
b.display();
}
public static void main(String args[])
{
A a=new A();
}
}
====================================================================
class Address {
String city,state,country;
Address(String city, String state,String country)
{
this.city=city;
this.state=state;
this.country=country;
}
}
class Emp{
int id;
String name;
Address addr;
Emp(int id,String name,Address addr)
{
this.id=id;
this.name=name;
this.addr=addr;
}
void display()
{
System.out.println(id + " "+name);
System.out.println(addr.city +" "+addr.state + " "+ addr.country );
}
public static void main(String args[])
{
Address a1=new Address("hyd","ts","IN");
Address a2=new Address("ohi","akr","US");
Emp e1=new Emp(99,"sree",a1);
Emp e2=new Emp(02,"sahi",a2);
e1.display();
e2.display();
}
}
=====================================================================
class B{
A obj;
B(A obj)
{
this.obj=obj;
}
void display()
{
System.out.println(obj.data);
{
}
}
}
class A {
int data=9;
A()
{
B b=new B(this);
b.display();
}
public static void main(String args[])
{
A a=new A();
}
}
====================================================================
class Address {
String city,state,country;
Address(String city, String state,String country)
{
this.city=city;
this.state=state;
this.country=country;
}
}
class Emp{
int id;
String name;
Address addr;
Emp(int id,String name,Address addr)
{
this.id=id;
this.name=name;
this.addr=addr;
}
void display()
{
System.out.println(id + " "+name);
System.out.println(addr.city +" "+addr.state + " "+ addr.country );
}
public static void main(String args[])
{
Address a1=new Address("hyd","ts","IN");
Address a2=new Address("ohi","akr","US");
Emp e1=new Emp(99,"sree",a1);
Emp e2=new Emp(02,"sahi",a2);
e1.display();
e2.display();
}
}
=====================================================================
Inheritance in Java
The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.
Why use inheritance in java
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
Hope this will help you....Java Interview Questions
ReplyDelete