4 OOP Concept
4 OOP Concept
Inheritance
• When one object acquires all the properties and behaviours of parent object i.e. known as
inheritance.
• It provides code reusability.
• It is used to achieve runtime polymorphism.
Data members are nothing but simply variables that we declare inside
the class so it called data member of that particular class
• The data, or variables, defined within a class are called instance variables.
• The code is contained within methods.
• NOTE : C++ programmers will notice that the class declaration and the
implementation of the methods are stored in the same place and not defined
separately.
NOTE: we can use or access data of any particular class without using (.) dot
operator from inside that particular class only.
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
Syntax of object
//declaration of object
classname objectname;.
• return type specifies the type of data returned by the method. This
can be any valid data type including class types that you create.
• If the method does not return a value, its return type must be void,
Means you can say that void means no return.
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
Syntax
• Methods that have a return type other than void return a value to the
calling routine using the following form of the return statement:
return value;
• Here, value is the value returned.
• The method name is any legal identifier.
• The Constructor cannot have any return type —not even void
• A default constructor will call the Constructor for the class this one
extends.
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
Varieties of Methods: Constructors
public Time2()
{
setTime( 0, 0, 0 );
}
• Now when we define object c3 our syntax is like Circle c3 = new Circle(new
Point(15,25),10); so first of all it will create object for Point class so constructor of point
class will be called and it will set parameter x and y.
• Then constructor of circle class which has Point class object as an argument along with
one int argument will be called and set all parameter as per program and we get output
like Centre at 15 and 25 Radius = 10 in c3.display().
• It is possible in java to define two or more methods within the same class
that share the same name, but with different parameter declarations (type
signatures).
• When this is the case, the methods are said to be overloaded, and the
process is referred to as method overloading.
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
Method Overloading
• Overloading methods demonstrate the concept of polymorphism.
• We have seen that methods can take parameters as input and process
them.
• It is also common to pass objects as a parameter to methods.
• A method can return any type of data, including class type (object)
that you create.
• Here we illustrate call by value and in next topic we will look at call by reference.
• In call by value when we call any method we pass value as method parameter so
changing in local variables of the method doesn‘t affect the original variables of
class.
• We pass value v.a and v.b as parameter and it will change local
method`s a and b variables.
• This effectively means that objects are passed to method do affect the object
used as an argument.
• So one thing is sure that we have to take care that in every recursive
process there must be a terminate condition to come out from recursion.
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
This Keyword
• There can be a lot of usage of java this keyword.
• In java, this is a reference variable that refers to the current object.
• we are using this keyword to distinguish between local variable and instance
variable.
• The static method can not use non static data member or call non-static method directly.
• this and super cannot be used in static context.
Example:
class A
{
int a=40;//non static
public static void main(String args[])
{
System.out.println(a);
}
}
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
Java static block
• Is used to initialize the static data member.
• It is executed before main method at the time of class loading.
class A2
{
static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
System.out.println("Hello main");
}
}
• Relying on Access Modifiers, you can shield both your class’s private
instance variables and its private methods
protected,
public
package
• This access allows other classes in the same package as your class to
access its data variables as if they were their own.
package
• This level of access assumes that classes in the same package as your
class are friends who won’t harm your class’ data
Or we can summarize:
1. Static
2. Non – Static
• It has access to all of its variables and methods of its outer class and
can refer to them directly.
• A class is created but its name is decided by the compiler which extends the
Person class and provides the implementation of the eat() method.
• An object of Anonymous class is created that is referred by p reference variable of
Person type.
Dr. KRUNAL N. PATEL (IT DEPARTMENT), MBIT
Nested Classes