--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
E-mail : ruparleducation@gmail.com
Contact No : 7600044051
Programming with java Unit 1
Question 1 :: OOP Concept
Class:
- Class is a collection of data member and data function
- Class is a template that define the form of an object
- Class is an implementation of encapsulisation concept of OOP
- Class is nothing but one kind of structure of data binding
- Class is user defined data type we can use class in a main function or in any event using an object
- Object is a variable or instance of the class
- We can create as many instance of class as we want
- Usually with the help of class we define a big project in a small part class make modification and improvements of large and complex program
- Usually we create data member as a private and member functions are public but it is not compulsory
- Class contains constructor destructor nested class methods etc…
Object
- Object key to understand object oriented Technology
- An object is an entity that has state and behaviour
- It can be physical or logical tangible or intangible
- Following is some characteristics of an object
- State : State the present data value of an object
- Behaviour : Present behaviour or a functionality of an object such as amount deposit in a bank and withdraw from the bank
- Identity : Object identity is typically implemented using an unique ID the value of the ID is not visible to the external user heat is used internally by the system to identify each object uniquely
- For example Pen is an object its name is Synosoft and its colour is a blue it is known as a state and it is used to write something so writing is its behaviour object is an instance of a class. Class is a blueprint or template from which object are created so, the object is an instance of a class
Encapsulation
- Encapsulation is an object oriented programming based concept.
- Encapsulation protect important data inside the class
- Which we do not want to be exposed outside the class
- Encapsulation process means binding the Data Member and Member function in single unit
- Data member means variable property etc... while the Member function means method and the class is one of the best example of an encapsulation
- Encapsulation hide private or unwanted data from outside the class
- With the Encapsulation we can make variable property and method to private
- Inheritance is a mechanism in which one class acquire all the properties and behaviour of another parent class
- The idea behind Inheritance is just you create a new class that are built upon an existing class
- When you inherit from an existing class you can reuse method and field of parent class you can add new method and field also
- Inheritance represent “is a” relationship. It is also known as parent child relationship
- Inheritance also known as generalisation
- Java use extend keyword to create inheritance
- The main advantages of inheritance
- Reusability
- Save time
- Help in modularization of code
Polymorphism
- Polymorphism is a concept by which we can perform a single action by different ways
- Polymorphism is derived from Greek word where poly means many and morphism means forms,
- So polymorphism means many forms there are two types of polymorphism in Java
- Compile time polymorphism
- Runtime polymorphism
- We can perform polymorphism in Java by method overloading and overriding
- If overload static method in java it is the example of compile time polymorphism
- While the runtime polymorphism are the dynamic method dispatch is a process in which a call to an overloading is resolved at runtime rather than Compiletime.
- In this process overriding method is call to the reference variable of a superclass
- The determination of the method to be call is based on object being referred by the reference variable
- Polymorphism can be static or dynamic
- In a static polymorphism response to a function is determined at the compile time and the dynamic polymorphism is decided at runtime
- The static polymorphism linking function with an object during compile time is called early binding it is call Static Binding
- In a dynamic polymorphism you have to create abstract class that used to provide partial class implementation of an interface.
- Implementation is a completed when derived class inherit from it
- Abstract class contain abstract method which are implemented by derived class
Question 2 : Constructor
- Constructor are special Member function whose task is to initialise the object of its class
- It is treated as a special Member function because its name is same as the class name
- Java constructor are invoked when the object is created
- It is a name such because it construct the value it means provide the data from the object
- It means there are used to initialise the object
- Every class has constructor when we don’t explicit declare constructor from any Java class
- The compiler create a default constructor for the class which does not have any return type
- Following is some characteristics of constructor
- Interface cannot have constructor
- Constructor cannot be private
- Constructor can be overloaded
- Constructor cannot return a value
- Constructor do not have any return type not even void
- Abstract class can have constructor
- Constructor name must be similar to the class name inside which is created
- Constructor are automatically call when an object is created
- Instance variable and method of a class are known as member of a class
- Constructor are not a member for this reason constructor cannot be inherited but can be accessed by subclass
- Constructor do not get inherited only their member like variable and the method get inherited so declared a class as a final is useless and has no meaning as a constructor cannot be overriding
- There are two types of the constructor
- Default constructor
- Parameterized constructor
- Constructor having a no any parameter is known as default constructor or non parameterized constructor
- A constructor having an argument is known as parameterized constructor
Question
3: Finalize() method
{
Finalize code
}
- Sometime an object may fall under automatic deallocation by Java but it is need to perform some action.
- Then the object is to be save from automatic instructions to handle such a situation
- Java provide a mechanism call finalize with the use of method finalize
- This finalize Method can be put inside the class before the object is destroyed
- Java run finalize method before it goes for the object destroy
- User can define whatever action is to be taken inside the finalize()
- By using finalize we can define a special action that will occur when an object is just about to reclaimed by the garbage collection
- Following is a Syntax to define a finalize() method
{
Finalize code
}
- According to above syntax the finalize() method will specify the action that must be perform before an abject is destroy.
class FinalizeExample
{
public void finalize()
{
System.out.println(“Finalize Method Call”);
}
public static void main(String []args)
{
FinalizeExample f1 = new FinalizeExample();
FinalizeExample f2 = new FinalizeExample();
f1 = null;
f2 = null;
System.gc();
}
}
}
- According to above example finalize method call automatic. If the object is no longer require using System.gc() method.
- A class member must be access the use of an object of it’s class.
- But sometime we want to define a class member that will be used independently without creating any object of that class.
- It is possible in Java to create a member that can be used itself without reference to specific and instance.
- To create such a member prefix declaration with static keyword.
- When a member is declared static it can be accessed before an object of a class are created and without reference to any object.
- One can declare both method and variable to be static the most common example of static member is main function main function
- main() function is declared as static because it must be called before any object
- Instance variable declared as a static actually global variable when objects of its class declared no copy of static variable is made
- When we declare method as static there are several restrictions that is...
- One call only another static method
- Only access static data
- Cannot refer to this or super in any way
- following is an example of static variable and static method
- According to above example class student continent instance variable as well as static variable
- It means in string variable call separately when an object is created by the static member variable assign its value for all the objects
- it means it not allow to access separately by the object
Question 5 : overloading constructor
- Constructor is special Member function of a class and its not return a value even void
- whenever object create constructor call automatically because it used to initialize the value of the object field
- Java support the concept of constructor overloading
- it means constructor is called based upon the parameters specified that the time of object creation
- sometimes there is need to initialize an object in different ways and this can be done using constructor overloading
- in a constructor regarding the name of the constructor you must be same while the number of argument or type of argument must be differ from each other
Example...
- According to above example class Abc have to constructor first is non-parameterize constructor and second is parameterize constructor.
- Method overloading :
- If class have multiple method having same name but different in parameters it is known as Method overloading
- If we have to perform only one operation having same name of the method it increase readability of the program
- we can define method loading and different two ways
- By changing my number of argument
- By changing the data type
- it allows users to define same name method with different signature due to this reason we do not need to create a different method for same purpose with different name
- According to above syntax class is a keyword to create class and class name is name of the class which contain two separate method which is either different from there datatype or may be number of argument
- According above example class contain two separate method with name sum which contain a different number of arguments and from the main function object of class A pass appropriate parameter to their function and compiler automatic execute them based of their number of argument
Question 6 :: Varargs
- Varargs is allow the method to accept 0(zero) or multiple argument
- Before varargs either we use overloaded method or take an array as the method parameter
- But it was not consider in good programming because it was created maintenance problem
- If we don’t know how many argument we will have to pass in the method where varargs is the better approach
- The main advantages of varargs is that...
- We don’t have to provide any overload method so redundant the code
- Following is syntax of varargs
{
Executable Code...
}
- According to above syntax return type specify any premative or non-premative datatype
- Method name is name of the method it must be in a valid form it require the 3 dots(...) after the datatype which defines number of arguments
- Following is example of varargs
{
static void display(String... str)
{
System.out.println("Varargs Demo");
}
public static void main(String args[])
{
display();
display("A");
display("A","B","C","D");
}
}
- According to above example display function have argument of string type and it contain varargs values and display each you values separately using for loop from the main function
- We call display function with 3 different types and each time it gave appropriate value in a program in a varargs there can be only one variable argument in the method and variable arguments must be the last argument
Question
: Creating and using Class with members
Answer
: Java is an object-oriented
programming language.
Everything in Java is
associated with classes and objects, along with its attributes and methods.
For example:
in real life, a car is an
object. The car has attributes, such as weight and
color, and methods, such as drive and brake.
To create a
class, use the keyword
class
:
public class Abc
{
int x = 5;
int x = 5;
Void disp(void)
{
System.out.println(“Value of X is :”+x);
}
}
}
According to above class Abc
is a class and variable x and disp() is member of a class.
:: Best Of Luck ::
Please share and give your comment... Thank you