Java4teachus

Tuesday, December 25, 2018

Types Of Data Members & Methods In a Class

In every java programming language , A class can containing  two types of Data Members .They are :

1.Non-static data members.

2.Static  data member.

Instance Data Member 


➢Instance data members memory space is created each every time whenever the object is created.

Instance data member are used for storing specific values which are suitable  for individuals.
➢Instance data member declaration should not be preceded by 'Static keyword' .
      Ex:- int a=10,20,30,40; (Instance Data Member) 
➢Datatype V 1 ,V 2,V 3........V n ;.
➢Each & every instance data member must be access with respect to Object name
     (Object . Instance Data Member Name)

➢Instance data member value are not shareable.
➢Instance data members are also called "Object level Data Members".Below following examples are the instance data type members.

 EX :  int student Id ;

       String course;
       String student name ;
       float marks;
Static-vs-non-Static-data-members.jpeg

Static Data Members 


➢ Static  data members memory space is created  once only when we defined the class then irrespective  number of times objects are created.

➢ Static date members  are used for storing  common values which are suitable for all programmers.
➢ Static data members declaration is must be preceded by "static keyboard "
➢ Static" data type V 1,V 2.......V n ;
➢ Each & every data member must be access to respect to object even with class name (obj. class).
➢ Static   data members are shareable
➢ Static data member are also called "class level data members ". Below following examples are the static data type members.

EX:  static string course ;  

         static string Delhi  ;
         static string pi ;
NOTE:-   Each every final variable must be static but a static variable  value may are  not be final .
       static final float PI= 3.14f ;
       static final String s="Java";

Tuesday, December 18, 2018

Different B/W the Class vs Object In JAVA

Hash code or hexadecimal code:

When we create an object  with new operator by referring internally for created java execution provides a unique numerical id called Hash code / hexa decimal format called hexa decimal code.

The purpose of hash code or hexa decimal code is always used for identifying the objects or provides identity object.

ClassLoaderSubSystem:

A ClassLoaderSubSystem is one of the java program available  as a part of java software and whose job role is to load the .class file from "Secondary Memory into Main Memory". without loading .class file name we cannot create a object.

Class :
  • A Class is collection of  'Data Members & Methods' .
  • The definition of  class does not contain memory space for  Data Members & Methods .
  • Class definition is always consider as  'Logical existence' .
  • When we execute the Java program the definition of class will be loaded once in the in the Main Memory  with the help of "ClassLoaderSubSystem" .
  • When we  write a java program the definition of a class will exists only once .
  • The definition of class resides in Secondary Memory in the form of  class name  .
Class-vs-Object-In-java.jpeg

Object :
  • Instance of a class is caned an  'Object'. 
  • Memory space is created for  Data Member & Methods  only when we create an object 
  • Object creation is always consider as 'physical existence'. 
  • After loading  class file name in the Main Memory  and later we can create the  object  .
  • With respect to one class definition we can create multiple objects 
  • Objects residing in   Main Memory.

Note 

  1. All "Objects " of java resides in the "Heap Memory". 
  2. All "Methods" of java resides & executes in "Stack Memory".
  3. All  "figurative constants"  values (directly used). resides in "Associatine memory" .
  4. All the above three memories are of the parts of "Main Memory" .

Wednesday, December 05, 2018

Definition Of Object & Create an Object in Java

Create an Object 

  • We know that when we define a class memory space is not created so that data members and method but whose memory is created when we create an  "Object".
  • In other word to enter for the data member of a class, first we must allocate memory space for data member by creating an"Object" .
  • To do any data processing we must create an "Object".
  • To create an object either must exist a class definition otherwise it is compile error.
Definition of object
  • Instance of a class is called an"Object" ( instance is nothing but allocating sufficient amount of memory space for "Data members & Method" ).
  • Each & every class variable is called an "Object".
  • Each & every grouped item is called "Object".  
  • (in grouped item is a variable which allows to store multiple value of same type or different type or both types.)
Creating an object in java
  • Creating an object in nothing but allocating the sufficient amount of memory space for data members and methods of a class. By following dynamic memory allocation with the help of new operator and it is called "dynamic memory allocation" operator.

New Operator In Java 

  • It  allocates  sufficient  amount of memory space for data member and method of specified  class.  
  • It takes an address / reference of created memory space and placing it into L.H.S variable i.e.   is  object  name.
  • Out  of  many  way  of creating an object in java with 'New Operator' .
  • We can create an object with two syntax's  they are:

Syntax:-1

<Class name>  <object> = new  <Class name > ----> 1

➽ create an object of Student class.
       
     Student  S= new Student( );


➽ Create an object of Circle class.

     Circle   C= new Circle( );

➽ Create an object of Employee Class.

     Employee e = new Employee( );


From the above examples we create an object by using the Class name called Employee or Programmer defined data type.

Here Employee is the 'programmer defined datatype' and 'e' is one of the 'reference variable or object' of Employee class. In Java by default reference variable contain 'Hash code / Hexa decimal code' which is very high security for applications.


Monday, August 27, 2018

Define a Java Program With Class Syntax

In previous post we have already learnt about on the Syntax of a class. But we would like known about the program of class syntax.

Q. Write a class definition for computing subtraction of two number?

program

class Sub
{
int a, b, c ;
void accept( )
{
a=40;
b=30;
}
void add( )
{
int c=a-b;
}
void  display( )
{
System.out.println(“value of a="+a);
System.out.println(“value of b=”+b);
System.out.println(“sub=”+c);
}
}

Q . Write a class definition for computing multiplication of  three number?.

program

class Multiplication
{
int a, b, c ,d  ;
void accept( )
{
a=30;
b=60;
c=30
}
void mul( )
{
d= a*b*c;
}
void  display( )
{
System.out.println(“value of a="+a);
System.out.println(“value of b=”+b);
System.out.println("value  of  c=" +d);
System.out.println(“Mul=”+c);
}
}

Q . Write a class definition for computing (a+b)2?

program

class Sqr
{
int a,b,c;
void accept()
{
a=10;
b=20;
}
void sqr()
{
c=a*a+b*b+2*a*b;
}
void display()
{
System.out.println(“value of a="+a);
System.out.println(“value of b="+b);
System.out.println(“square="=c);
}
}

Q . Write a class definition for computing a 2 ?

program

class Square
{
int a,b;
void accept()
{
a=10;
}
void square()
{
b=a*a;
}
void display()
{
System.out.println(“value of a="+a);
System.out.println(“square="+b);
}
}

Q . Write a class definition for computing area of triangle ?

program

class Triangle
{
int b,h;
float i;
void accept()
{
b=1;
h=2;
}
void triangle()
{
i=0.5*b*h;
}
void display()
{
System.out.println(“value of b="+b);
System.out.println(“value of h="+h);
System.out.println(“area of triangle="+i);
}
}

Q. Write a class definition for computing area of circle ?.

program

class AreaOfCircle
{
int r;
double pi,s;
void accept()
{
r=10;
pi=3.14;
}
void circle()
{
s=pi*r*r;
}
void display()
{
System.out.println(“value of r="+r);
System.out.println(“area of circle="+s);
}
}

Q. Write a class definition for computing area of Rectangle ?.

program


class AreaOfRectangle
{
int a,l,b;
void accept()
{
l=100;
b=200;
}
void areaOfRectangle()
{
a=l*b;
}
void display()
{
System.out.println(“value of l="+l);
System.out.println(“value of b="+b);
System.out.println(“areaofrectangle="+a);
}
}

Q. Write a class definition for computing the circumstance of circle ?.

program

class CircumferenceOfCircle
{
int r;
double pi,c;
void accept()
{
pi=3.14;
r=20;
}
void circumferenceOfCircle()
{
c=2*pi*r;
}
void display()
{
System.out.println("value of r="+r);
System.out.println("value of pi="+pi);
System.out.println("CircumferenceOfCircle="+c);
}
}

Q. Write a class definition for computing the Volume of  a cube?.

program

class VolumeOfCube
{

int b,h,v;

void accept()

{

b=1;

h=2;

}

void volumeOfCube()
{
v=b*h;
}
void display()
{
System.out.println("value of b="+b);
System.out.println("value of h="+h);
System.out.println("VolumeOfCube="+v);
}
}

Q .Write a java with class definition for computing sum of two number?

program

class Sum
{
int a, b, c ;
void accept()
{
a=10;
b=20;
}
void add( )
{
int c=a+b;
}
void  display( )
{
System.out.println (“value of a="+a);
System.out.println (“value of b=”+b);
System.out.println(“sum =”+c);
}
}

In the above program, Whenever we write the java program then we must have to define the class name by using the keyword called as "class".  Here the class name what we defined that is Sum

A logic of the above program is c= a+b. Here  a,b  are the two input variables which is used to store the values  and c is the output variable of a java program. 

accept(), add(), display() are the methods which is always shows Business Logic of a java program.

Thursday, August 23, 2018

Syntax Of a Class in Java

Definition Of Class


A class is collection of Data members where members can be defined as member variable and member methods within a Block of statements is called class. In java a class can be written by using the keyword called "Class". A class is related to properties and Fields is also known as Abstract data type.

Syntax Of a Class

   class  <ClassName>
   {
        Variable declaration (Or) Data members ;
        Methods definition ;
   }

➠ The members of a class can be public or public. In java, we cannot have global variables or stand alone functions. If a variable or a function has to be written, It must be a member of some or the other class. This is also reason java is a "Object oriented programming language".

➠ Method definition has four parts. They are:
  • The name of the Method( Method Name)
  • The type of the value the method return (type)
  • A list of parameters .
  •  The body of the method.                                                                 
In the below diagram shown as class name called Tree. But a Tree is one of the class name and different type of trees is called  "Object".

Different-type-of-classes-with-class-name-called-Mobile.png


In the above diagram shows class name called Mobile. But here Mobile is one of the class name and different type of Mobiles is called  "Object".

Different-type-of-classes-with-class-name-called-Tree.png

Explanation

  • Class is one of the keyword which is used for developing "Programmer Defined Data type" and to model the new world applications.
  • <class name> is one of the java valued  variable name treated as name of the class. In java if any  class contains either one word more than one word then words all first must be capital. This rule is also known as Hungarian rule in java.
               Example:-  class StudentInfo, class EmployeeInfo .
  • In java each and every name  is treated as  Programmer Defined Data Type.
  • Variable declaration represents set of data members which is selected from based on <class name>.  
  • Methods definition represents the set of methods meant for performing different type of operation. In java all the Methods must be defined inside the class only but not outside of a class.
  • In oops concepts we have 2 types of methods they are:    
           a) Member method             
           b) Non-Member method
  • A Member method is always available with in the scope of the class and it can access the values present in the data member.
  • A Non- member is not available within the scope of the class and they cannot access values present in the data member.
  • Java programmer permits only member method for providing high security and never permits non-member methods.
  • The definition of the class must be embedded within open bracket ( { ) and closing bracket ( } ). The definition of the class may are may not be terminated in semicolon ( ; ).
  • Hence all the class definition contains "logical existence" and acts of formula.
  • Define a class for representing student information.    
class Student
{
int std no :
String std name;
float M 1 ,M 2 ,M 3;
int getNoOfHoursStudy ( )
{
Return (3)
}
float getTotalMarks ( )
{
float total =m 1 +m 2 +m 3 ;
return total ;
}
String getGrade ( )
{
Return” distinction” ;
}
}

Saturday, August 18, 2018

Definition Of Class in Java

Purpose Of a Class

In previous post we already learnt about the OOPS Principles. Now  we discuss about on the first concept in oops principle called as "class".
  • The purpose of class concept  is used to  develop the  programmer defined data type and also to develop the  "REAL WORLD APPLICATIONS".
  • Each and every java program must be start with concept of class. Without class concept there is no java program.
  • The purpose of developing programmer defined data type is that to store "Multiple value of same " or " different type or both the types " .
  • To develop programmer defined data type with class concept, we use the keyword called "class".
  • Programatically each & every class name is treated as "programmer defined data type".

Definition :-

"A class is a collection of data member & method" is known as class.
                                                 (or)
"The process of  binding the data  members and associate methods in a single unit " is called class .
  • In java we must have  to define a class keyword. Because the memory space is not created for the Data Members and Methods. But whose memory space is created only when we create an object with respect to corresponding "class name" .
  • Data member of a class are also known as "Attributes (or) Fields (or) Properties ". Where as "Methods" are also called as "Behavior (Or) Accessories".
  • Programatically data members and methods are associated with in a class. But  without class name the memory space and  available as it is with object along with memory space.
In real world the definition of class expressed by class diagrams in the helps of xml software like rational rows, turbos annalist (IBM pro). Structure of a class diagram is shown below. 

Structure Of a  Class :-

Structure-Of-A-Class-in-Java.png
                                     
Q . Draw a class diagram  or storing student information ?

Diagram-of-a-class.png

Class : Class is a collection of Data Members where members can be members variables or members functions. Classes are used to prepare ADTs. In the above example, "Student" is the class.

Properties : The members of a class are called Properties Or Attributes . These are also called as "Data Members" . In the above diagram 'Number, Name, Marks' are properties.

Methods : The member function of a class can be called as "Methods". In the above diagram getNoOfHoursStudy(), getTotolMArks(), getGrade() are the methods or behavior. 

Monday, August 13, 2018

OOPS Principle's|Feature's|Concept's in Java

Object Oriented Programming Language

OOPS means "Object Oriented Programming Language" available as a part of Java language. It is one of the most important concept in java which is useful for developing any Software or Applications easily. Today the real software industry are using only OOPS concepts rather then POP concepts. Suppose  if any programming languages satisfies objected oriented principles then it is called OOPS.

Example:-   
  • Small Talk 
  • ADA                                               
  • Object pascal            
  • Object COBOL
  • C++
  • Java
  • .Net
  • Oracle 
  • Perl .
If we use Object Oriented Programming Language (Java) for development of  distributed application we get the following points.
  1. It Posses platform independent 
  2. The data is visiting between client & server side application in the form of hyper text.
  3.  It Provides high security to the data by default.
  4. The data is visiting between client & server side application in the form  of  Objects and leads to effective communication.
  5. The data is represented around the object only.
In the real industry if any programming language is Object Oriented Programming then it has to be  satisfy 8 principles. They are:- 
  1. Classes
  2. Object
  3. Data encapsulation 
  4. Data abstraction 
  5. Inheritance 
  6. Polymorphism
  7. Dynamic binding
  8. Message Passing

class:

  • The class is at the core of java. It is the logical code which is based upon on the entire java language is built because it defines the shape and nature of object.
  • So that, a class can forms the basis on object oriented programming in java.
  • The most important thing to understand that a class defines a new data types. After that can be used to create the objects of that type only.
  • A class is a template for an object, and object is an instance of a class. Thus a class is a collection of objects.
          Example:- Mango, Orange, Apple 

Object:

  • Objects are the basics on the run time entities in an object-oriented system. They may be represented anything a program may handle. 
  • Any programming language problem is analyzed in terms of objects and the nature of communication between them.
  • program objects should be chosen such that they match closely with the real-world objects. 

Data abstraction:


The wrapping up of data and methods into a single unit called class is also known as encapsulation. Data encapsulation is the important features of a class. The data is not accessible to outside world and only those methods, which are wrapped in the class then we can access it.

Encapsulation:


Data Encapsulation is the mechanism that combined to together the code and the data it manipulates, and keep the both safe and from outside interference and misuse. One way to think about encapsulation is a protective wrapper that prevents the code defined outside the wrapper.

Inheritance:


Inheritance is the process of which objects of one class acquire the object properties of another class. Inheritance supports the concept of hierarchical diagram. In oop, the concept of inheritance provides the ideas of runnability. This means that we can add the additional features to an existing class without modifying it. This is possible to deriving a new class from the existing one. The new class will have the combined features of both the classes.

polymorphism:


Polymorphism mean the ability that to take one or more than one form is called polymorphism. For Example : An operation may exhibit different types of behavior in different instances. The behavior depends upon on the type of data used in the operation.

Polymorphism plays an important role in allowing objects which is having different internal structures to shares the same external interface. Polymorphism is extensively used in implementing inheritance.

Dynamic binding:


Dynamic Binding refers to the linking of a produce call to the executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of call of run-time. It is associated with polymorphism and inheritances.

Messaging Passing:


An object - oriented programming language consists of a set of objects that communicate with each other. The process of programming in an object oriented language involves the following steps.
    1. It creates the class that define the objects and their behavior.
    2. Creating the objects from the class definitions.
    3. It establishing the communication among the objects.