OOPs Concept - 1(Practicals)

What is class and object in OOPs?

Class

Let's take "Student" class for example,
Name of Class : Student
Attributes or Properties of Student class could be : name, rollNumber, course etc.
Operations of Student class could be : getDetails, setDetails etc.

Let's represent this Student class in Java.

class Student{

        private String name;
        private String rollNumber;
        private String course;
        //other attributes can go on here.

       public void setDetails(){
         //logic for setting student details will go here
       }

       public void getDetails(){
          //logic for setting student details will go here
       }
 }


For declaring a class we have to use class keyword followed by class name. Class name should be meaningful. It is considered as good practice if it relates to some real world entity. Then we define it's body or scope using {}. Within class scope or body we can declare it's attributes and operations.

Basic syntax for Attribute follows following convention :
<access-modifier> <data-type> <attribute-name>;

Access Modifiers could be private, protected and public. If none of these are specified then default or package modifiers will be assigned to attributes.
We will go through these modifiers in details in upcoming posts.

Data type defines what type of data, attribute is going to handle. In our case all three attributes are going to handle String type of data.
We will go through data types in details in upcoming posts.

Attribute name could be anything which relates to class.
Following conventions needs to be followed while naming an attribute :
Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed. Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are ijkm, and n for integers; cd, and e for characters.


Object

If we want to use above "Student" class in our program than we have to create its object first. In Java objects are created using new keyword.

Basic syntax for creating objects in Java is :
<class-name> <object-name> = new <class-name>();

For example, if we have to create two objects of Student declared above then we can do as follow :
Student object1 = new Student();
Student object2 = new Student();

We can operate on this Objects using dot(.) operator of Java.
For Example :
object1.getDetails();
object2.getDetails();

This is the basic about classes and objects in java, we will continue to discuss this in subsequent posts.

Share:

OOPs Concept - 1

Java is an object oriented programming language which means it supports OOPs concepts. In this post I am going to discuss OOPs concepts which will help you not only in Java but in other OOPs and OOPs based languages.

Basic OOPs concepts are as follow :
1. Class
2. Object
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation

Now we will discuss these concepts one by one.

Class

It is a logical entity which groups common feature and behavior of different entities.It is the blueprint, or plan, or template, that describes the details of an object.Class doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

Class is composed of three things: a name, attributes (properties), and operations.

See OOPs Concept - 1(Practicals)  for coding perspective of class.


Object

An object can be considered a thing that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the Hand(object) can grip something, or a Student(object) can give their name or address.
In pure OOP terms an object is an instance of a class.

An Object is composed of three things : identity, state and behavior.

Identity is that attribute of Object which differentiate between two unique objects. For example student's roll number.

State represents what are current values present in object's attributes.

Behavior defines what operations can be performed on that object.

For practicals on these topics you can visit on : OOPs Concept - 1(Practicals)
Share:

Features of JAVA

Features of Java

This is one of the important topic which is asked in interviews at fresher level. Java is a favorite language for many developers because of many reasons. Features of Java is an important section from examination point of view as well as from interview point of view. You are definitely going to face one or more question related to this section.


Features of Java are as follow :
  1. Platform Independent
  2. This is the most important feature of Java as you are definitely going to face one question related to this point.
    Java is WORA language that means you can write code in Java once and runs it anywhere on any machine you want. Unlike other programming languages like C or C++, which are machine dependent, Java do not generate machine code directly after compilation. C or C++ generate executable which are machine dependent but Java generates bytecodes which do not execute directly on machine.
    Bytecodes are intermediary codes not machine code, they are also called IL (Intermediate Language) code. They get converted into machine code by Java interpreter (JVM). Different operating systems has their own implementation of JVM which converts bytecodes according to OS on which code is being run.
    That’s why Java is called WORA language because you have to write code only once and JVM will generate machine code according to OS.

    JavaByNishesh Image 1
  3. Object Oriented
  4.  Java is an object oriented language. I believe most of you are aware of OOPs concept, if not, then always remember one very important feature of OOPs that is "Every thing in this real world having common feature and behavior can be classified in form of classes and then we can create object of those classes having specific feature and behavior".
    Take for an example, there is "Animal" class which represents all common feature and behavior of all Animals, but if we want a specific animal like a Dog then we have to create an object of Animal class for representing a Dog.

    Most important thing about this feature is that.
    Java is not a complete OOPs language because it has the concept of primitive data types. In complete OOPs language everything is in form of classes. Smalltalk is an example of complete OOPs language.

  5. Robust
  6. Robust stands for "Strong". Java has strong memory management than C or C++. Concept of pointers has been eliminated in Java. Java is robust or strong Programming Language because of its capability to handle Run-time Error, automatic garbage collection, lack of pointer concept, Exception Handling. All these points makes It robust Language.
  7. Multi Threading
  8. A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it shares the same memory. Threads are important for multi-media, Web applications etc.
    When any Language execute multiple thread at a time that language is known as multi threaded Language.
  9. Secured
  10. It is more secured language compare to other language; In this language all code is converted into byte code after compilation which is not readable by human.
    When it comes to security, Java is always the first choice. With java secure features it enable us to develop virus free, temper free system. Java program always runs in Java runtime environment with almost null interaction with system OS, hence it is more secure.
  11. High Performance
  12. It have high performance because of following reasons;
    • This language uses Bytecode which is more faster than ordinary pointer code so Performance of this language is high.
    • Garbage collector, collect the unused memory space and improve the performance of application.
    • It have no pointers so that using this language we can develop an application very easily.
    • It support multithreading, because of this time consuming process can be reduced to execute the program.


    





Share:

Popular Posts

Recent Posts

Followers

Total Pageviews