Features of java

Object Oriented
Platform Independent and Portable
Compiled and Interpreted
Robust and Secured
Distributed
High performance
Multithreaded and Interactive


OOP Features

Classes: A class is nothing but a template or structure for creation of diff objects.
Object: An entity of the class.
Inheritance: The derived class uses the properties of super class. Code Reusability
Polymorphism: One name multiple form, Method overloading, Method Overriding.
Encapsulation: It binds together the code and data, it manipulates and keep them safe from outside interference and misuse. It is the protective container.
Abstraction: Hides the details from users.


Variables

A Variable is a named part of data memory having some value.

Java

Categories of Variable:

1) Static Variable
2) Non Static Variable

Types of Variable:

1) Class Variable: It’s a member variable i.e it is associated with the class. Class variable are known as static member variable and there’s only one copy of that variable is shared with all instances of that class. If changes made to that variable, all other instances will see the effect of the change.
2) Instance Variable: Instance variable belong to the instance of a class, thus an object. And every instance of a class has its own copy of that variable. Changes made to the variable don’t reflect in other instances of that class.
3) Method Variable: A variable which is defined inside a method. Its scope is within the method only.

Example to explain in more detail.

Class Variable Method Variable Instance Variable
Class Class_Variable
{int a=10;
static int b=20;psvm(sa[])
{Class_Variable cv1= new        Class_Variable();
cv1.a=30; cv1.b=40;Class_Variable cv2= new Class_Variable();
cv2.a=50; cv2.b=60;Class_Variable cv3= new Class_Variable();
S.o.p(cv1.a);//30
S.o.p(cv1.b);//60

S.o.p(cv2.a);//50
S.o.p(cv2.b);//60

S.o.p(cv3.a);//10 InstanceVar
S.o.p(cv3.b);//60 ClassVar

}

}

Class a

{

void met1()
{
int x=10;

S.o.p(x);

}

psvm(sa[])

{

A a=new A();

a.met2();

}

void met2()

{

met1();// 10

S.o.p(“BACK”); // BACK

}

}

OUTPUT:

10
BACK

Class A

{

int x=10;

void met1()

{

S.o.p(x);

}

}

Class I_V

{

psvm(sa[])

{

A a=new A();

a.met1();  //10

S.o.p(a.x); //10

}

}


Static & Non Static Methods

  • A static method is related to its corresponding class.
  • To call static method creation of object is not required. The static method call be directly called using class name followed by method name. E.g method_name();
  • Non static methods are related to instance of the class and not directly related to class.
  • Therefore, to call non static method, it can only be done by creating object of the class.
  • Limitation of static method is that it can access only static class variable, and not instance variable.
  • A static method cannot refer to ‘THIS’ or ‘SUPER’ keywords.
  • [NOTE]: The instruction given to the interpreter is to call the main() method. Hence main() method should always be declared as static. That’s why main() method is called without any object being created.
  • Example:

class P

{

int a=10;

void met1()

{

S.o.p(a);

}

}

class Q

{

static int b=20;

static void met2()

{

S.o.p(b);

}

}

class MetMain

{

psvm(sa[])

{

P p=new P();

p.met1();     // 10

Q.et2();        // 20

}

}


Constructor

A constructor is just like a method having same name as the class name, but without any return type and it is automatically called whenever an object is created.
A constructor initializes objects and the class variables.

Types of Constructor:

1) Default constructor: No parameters are passed to the constructor.

2) Parameterized constructor: Parameters are passed to the constructor.

3) Copy constructor: A constructer is passed as a parameter to the constructor.

Default constructor Parameterized constructor
class A

{

A()

{

S.o.p(“THIS IS DEFAULT  CONSTRUCTOR”);

}

psvm(sa[])

{

A a=new A();  // THIS IS DEFAULT  CONSTRUCTOR

}

}

OUTPUT:

THIS IS DEFAULT  CONSTRUCTOR

class A

{

int id;

String name;

A(int i, string s)

{

id=i;

name=s;

S.o.p(id+”=”+name);

}

}

class Const

{

A a=new A(22,””XYZ);  // 22=XYZ

}

OUTPUT:

22=XYZ


Polymorphism

Overloading and Overriding comes under polymorphism. We will see method overloading and method overriding in detail.

Method Overloading:

Based on only return type overloading is not done. For overloading method name should be same but parameters should be different with different definition. i.e
void met1(int a, float b)
{*********}
void met1(float c, int d)
{*********}

Method Overriding:

In method overriding the entire signature of the methods are same.
The overriding method should not be less accessible than the overridden method.
Overriding is always done in sub class and not in super class.
E.g

class A
{
void met1()

{
S.o.p(“A”);
}
}
class OR extends A
{
void met1()

{
S.o.p(“OR”);
}

psvm(sa[])
{
OR p=new OR();
p.met1(); //OR
}
}


“This” Keyword

“This” keyword is used to refer to an instance variable. When either the member variable or the method variable hides/ overrides the class variable. “This” keyword is used to call a constructor of the same class from another constructor of that class by passing appropriate arguments.


“Super” keyword

Super keyword is used to refer to super class constructor or parent class method which is overridden.


Typecasting

Typecasting means converting one datatype into another datatype. The result may be successful conversion or some loss of precision.


Dynamic Binding

Dynamic binding is also known as Late Binding, Runtime Polymorphism, Dynamic Method Invocation, & Virtual Method Invocation (Cathe Sera).
In this we create object of super class but call the constructor of sub class, only to check whether the super class is overridden or not.
A x=new A();
B y=new B();
x=y;


Abstract Class & Method

Abstract class and method are similar to virtual in c++.
A method without body or definition should be declared as abstract class i.e If a class has an abstract method then the whole class should be declared as abstract class.
Any class extending abstract class must define and override all of its abstract methods of super class.
An abstract class may have non abstract methods also.
Abstract class cannot be instantiated.
An abstract class may not have any abstract method and this is done to prevent creation an instance of that class and made only for inheritance or overriding purpose.
Making constructor private.
Advantage of abstract class is that they allow us to maintain a structure and force the sub class to implement (override) some methods.
It enables Dynamic Binding.


Interface

Interface is just like class in java but having all of its methods as public, abstract and all of its variable as public, static and final.
Interface allows to implement multiple inheritance, which was not possible using just class.
A class extends another class, while interfaces are implemented by class.


Strings

In java strings are class objects and they are implemented using two classes.
1) String
2) String Buffer

A java string is an instantiated object of class String.
Java String has inbuilt methods.


Multithreading

Advertisements