Documente Academic
Documente Profesional
Documente Cultură
DMA vs SMA
Dynamic memory allocation is runtime
Static memory allocation is compile time
Object Oriented Programming
Object means a real word entity such as pen, chair, table etc. Object-Oriented
Programming is a methodology to design a program using classes and objects. Its main
concepts:
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Primitive
Primitive data type
Int, float , double Boolean, string…..
Explicitly
Anything create by customize call is explicitly.
Change the type of variable customlly is call explicitly.
Change the type of variable on runtime is call explicitly.
Implicitly
Anything create by default call is implicitly.
Change the type of variable by customlly
Change the type of variable on compile time is call explicitly.
Instance
Any thing belongs to object is instance
An instance is a specific representation of an object,
an instance is a single object that has been created in memory.
Instances have attribute and behaviors of class
Instance:
Student s1=new student ("Martin"); Student s2=new student ("Kumar");
s1 and s2 are instances of object student the two are unique.
it can be called as reference also.
Basically the s1 and s2 are variables that are assigned an object
All the objects are instances. All the instances may not be objects. Instances may be
"structure instances" or "objects".
worldclock
Object Oriented Programming Tutorial
Instance
The instance of operator is used to test whether the object is an instance of the specified
type (class or subclass or interface).
The instance of in java is also known as type comparison operator because it compares the
instance with type. It returns either true or false. If we apply the instance of operator with
any variable that has null value, it returns false
Instantiating
any thing point to object is call instantiating
B =new class ()// its call instantiating
an instance (chair 3) is a single unique, specific representation of an object (a chair).
Member
Members are just objects or primitive types belonging to a class
The following table provides an overview of the kinds of members a class can contain.
(Rows for...)
Constants
Fields
Methods
Properties
Indexers
Events
Operators
Constructors
Destructors
Types
Property
A property describes some aspect of the object
Properties are a way to expose fields, where fields are the actual variables
Class member variables are called "properties
Variables in the class is call property
Instance variable
A variable that is created inside the class but outside the method is known as instance
variable. Instance variable doesn't get memory at compile time. It gets memory at runtime
when object (instance) is created. That is why, it is known as instance variable.
For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its
state. It is used to write, so writing is its behavior.
Any entity that has state and behavior is known as an object. For example: chair, pen,
table, keyboard, bike etc. It can be physical and logical.
Object is a thing, but instance is a part of a bundle thing. For example, if Car is a class, then
it should contain some of the objects like wheel, steering, door and seat. Now lets come to
the point: an object (wheel) is an instance of the Car
Memory allocated for the member of class at run time is called object or object is the
instance of Class.
Object - An instance of a class that has its own state and access to all of the behaviour
defined by its class.
time zone
Object Oriented Programming Tutorial
Anonymous object
Anonymous simply means nameless. An object that has no reference is known as
anonymous object.
If you have to use an object only once, anonymous object is a good approach.
Class
A class is a group of objects that has common properties. It is a template or blueprint
from which objects are created.
data member
method
constructor
block
class and interface
2) Object is a real world entity such as pen, laptop, Class is a group of similar
mobile, bed, keyboard, mouse, chair etc. objects.
4) Object is created through new keyword mainly Class is declared using class
e.g. keyword e.g.
Object Oriented Programming Tutorial
7) There are many ways to create object in java There is only one way to
such as new keyword, new Instance() method, define class in java using
clone() method, factory method and class keyword.
deserialization.
Aggregation
If a class has an entity reference, it is known as Aggregation. Aggregation represents HAS-A
relationship.
Consider a situation, Employee object contains many information’s such as id, name, email
ID etc. It contains one more object named address, which contains its own information’s
such as city, state, country, zip code etc
New keyword
The new keyword is used to allocate memory at runtime.
time change
Object Oriented Programming Tutorial
Static keyword
The static keyword in java is used for memory management mainly. We can apply java
static keyword with variables, methods, blocks and nested class. The static keyword belongs
to the class than instance of the class.
Static Members
A non-static class can contain static methods, fields, properties, or events. The static member is callable on
a class even when no instance of the class has been created. The static member is always accessed by the
class name, not the instance name. Only one copy of a static member exists. Static methods and
properties cannot access non-static fields and events in their containing type, and they cannot access an
instance variable of any object unless it is explicitly passed in a method parameter.
It is more typical to declare a non-static class with some static members, than to declare an entire class as
static. Two common uses of static fields are to keep a count of the number of objects that have been
instantiated, or to store a value that must be shared among all instances.
Static methods can be overloaded but not overridden, because they belong to the class, and not to any
instance of the class.
time difference
Object Oriented Programming Tutorial
Although a field cannot be declared as static const, a const field is essentially static in its behavior. It
belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the
same ClassName. MemberName notation that is used for static fields. No object instance is required.
Static Block
Static block is mostly used for changing the default values of static variables. This block gets
executed when the class is loaded in the memory.
A class can have multiple Static blocks, which will execute in the same sequence in which
they have been written into the program.
Static Variables
If you declare any variable as static, it is known static variable.
The static variable can be used to refer the common property of all objects (that is
not unique for each object) e.g. company name of employees, college name of
students etc.
The static variable gets memory only once in class area at the time of class loading.
Static Methods
Static Methods can access class variables without using object of the class. It can
access non-static methods and non-static variables by using objects. Static methods
can be accessed directly in static and non-static methods.
If you apply static keyword with any method, it is known as static method.
1. The static method cannot use non static data member or call non-static method
directly.
2. this and super cannot be used in static context.
Static Class
Collection of objects is called class. It is a logical entity.
A Class can be made static only if it is a nested Class. The nested static class can be accessed without
having an object of outer class.
Static Classes and Static Members Class
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be
instantiated. In other words, you cannot use the new keyword to create a variable of the class type.
Because there is no instance variable, you access the members of a static class by using the class name
itself.
Creating a static class is therefore basically the same as creating a class that contains only static members
and a private constructor. A private constructor prevents the class from being instantiated. The advantage
of using a static class is that the compiler can check to make sure that no instance members are
accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class
except Object. Static classes cannot contain an instance constructor; however, they can contain a static
constructor. Non-static classes should also define a static constructor if the class contains static members
that require non-trivial initialization
Memory is allocated for these variable Memory is allocated for these variable at the time
2
whenever an object is created of loading of the class.
Memory is allocated multiple time Memory is allocated for these variable only once
3
whenever a new object is created. in the program.
class_name.variable_name
obj_ref.variable_name
Final keyword
Final Variable if final variable assign one time value then final variable cannot change the
value and assign value by three way
i during deceleration
ii assign value by block {}
iii assign value by constructer
Final Class final class cannot extend
Final method final method cannot override
The final keyword is used to restrict the user. The java final keyword can be used in many
contexts. Final can be:
1. Variable If you make any variable as final, you cannot change the value of final
variable (It will be constant).
2. Method If you make any class as final, you cannot override it.
3. Class If you make any class as final, you cannot extend it.
The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in the
static block only. We will have detailed learning of these. Let's first learn the basics of final
keyword.
time converter
Object Oriented Programming Tutorial
A final variable that is not initialized at the time of declaration is known as blank final
variable.
If you want to create a variable that is initialized at the time of creating object and once
initialized may not be changed, it is useful. For example PAN CARD number of an employee
A static final variable that is not initialized at the time of declaration is known as static blank
final variable. It can be initialized only in static block.
If you declare any parameter as final, you cannot change the value of it.
This Keyword
There can be a lot of usage of java this keyword. In java, this is areference
variable that refers to the current object.
The this () constructor call can be used to invoke the current class constructor (constructor
chaining). This approach is better if you have many constructors in the class and want to
reuse that constructor.
Supper keyword
Super keyword is reference variable of parent class;
Call the overridden method by supper keyword
Mostly use in overridden
Call the super class property method then use super keyword
It is use for avoid naming conflict
The super keyword in java is a reference variable that is used to refer immediate parent
class object.
Whenever you create the instance of subclass, an instance of parent class is created
implicitly i.e. referred by super reference variable.
Access Modifiers::
Access specifier determines the visibility for a certain field / method.
They specify how variables should (or should not) be accessed;
Final or volatile and/or static and/or transient.
But access modifier are properties of a class/method/variable. Access modifier is five types
Overriding
Method Overriding is when a method defined in a superclass or interface is re-defined by
one of its subclasses, thus modifying/replacing the behavior the superclass provides. The
decision to call an implementation or another is dynamically taken at runtime, depending on
the object the operation is called from. Notice the signature of the method remains the same
when overriding.
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in java.
In other words, if subclass provides the specific implementation of the method that has
been provided by one of its parent class, it is known as method overriding.
meeting planner
Object Oriented Programming Tutorial
because static method is bound with class whereas instance method is bound with object.
Static belongs to class area and instance belongs to heap area.
If a class have multiple methods by same name but different parameters, it is known
as Method Overloading.
If we have to perform only one operation, having same name of the methods increases the
readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number
of arguments, if you write the method such as a(int,int) for two parameters, and
b(int,int,int) for three parameters then it may be difficult for you as well as other
programmers to understand the behavior of the method because its name differs. So, we
perform method overloading to figure out the program quickly.
Object Oriented Programming Tutorial
Why Method Overloading is not possible by changing the return type of method?
In java, method overloading is not possible by changing the return type of the method
because there may occur ambiguity. Let's see how ambiguity may occur:
Yes, by method overloading. You can have any number of main methods in a class by
method overloading.
Function Overloading
Function overloading gives you the ability to have more than one version of a
function so that it can behave differently according to the context in which and how
it is called.
Operator overloading
When different operations are performed using the same operator, then it is called operator
overloading.
Operator overloading is a programming language features that allows operators to act differently
depending on the type of data they are operating on
Operators as member functions
Aside from the operators which must be members, operators may be overloaded as member or non-
member functions. The choice of whether or not to overload as a member is up to the programmer.
Operators are generally overloaded as members when they:
When an operator is defined as a member, the number of explicit parameters is reduced by one, as
the calling object is implicitly supplied as an operand. Thus, binary operators take one explicit
parameter and unary operators none. In the case of binary operators, the left hand operand is the
calling object, and no type coercion will be done upon it. This is in contrast to non-member
operators, where the left hand operand may be coerced.
There are many differences between method overloading and method overriding in java. A
list of differences between method overloading and method overriding are given below:
4) Method overloading is the example of compile time Method overriding is the example
polymorphism. of run time polymorphism.
CONSTRUCTOR
A constructor is a special method that is used to initialize a newly created object and is called just after the memory
is allocated for the object.
It is not mandatory for the programmer to write a constructor for the class. If no user defined constructor is provided
for a class, compiler initializes member variables to its default values.
Numeric data types are set to 0 .
char data types are set to null character(\0)
reference variables are set to null
Object Oriented Programming Tutorial
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e.
provides data for the object that is why it is known as constructor.
Default constructor provides the default values to the object like 0, null etc. depending on
the type.
Parameterized constructor
A constructor that has parameters is known as parameterized constructor.
Constructor must not have return type. Method must have return type.
Constructor name must be same as the class Method name may or may not be
name. same as class name.
currency converter
Object Oriented Programming Tutorial
Copy Constructor
There is no copy constructor in java. But, we can copy the values of one object to another
like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
Ans:yes, that is current class instance (You cannot use return type yet it returns a value).
Yes, like object creation, starting a thread, calling method etc. You can perform any
operation in the constructor as you perform in the method.
Constructer Channing
Constructor call on same class Constructor is call Constructor Channing;
In same place cannot contain use super Constructor and sub Constructor
A class { __construct(){}}
b class{
__construct a (){ this->__construct()}
__construct t b (){
// first call Super construct
// then b construct
//and then a construct
}
Constructor chaining occurs through the use of inheritance. A subclass constructor
method's first task is to call its super class' constructor method. This ensures that the
creation of the subclass object starts with the initialization of the classes above it in the
inheritance chain.
there could be any number of classes in an inheritance chain.
every constructor method will call up the chain until the class at the top has been
reached and initialized. Then each subsequent class below is initialized as the chain
winds back down to the orignal subclass. This process is called constructor chaining.
Object Oriented Programming Tutorial
In case we want to call another constructor, this()/super() should be the first line in the
constructor.
CONSTRUCTOR OVERLOADING
Overloading a constructor means typing in multiple versions of the constructor, each
having a different argument list, like the following examples:
1
class Car
2
{
3 Car()
4 {
6 }
7 Car(String s)
{
8
9
}
10
}
11
The preceding Car class has two overloaded constructors, one that takes a string, and
one with no arguments. Because there's no code in the no-arg version, it's actually
identical to the default constructor the compiler supplies, but remember-since there's
already a constructor in this class (the one that takes a string), the compiler won't
supply a default constructor. If you want a no-arg constructor to overload the with-args
version you already have, you're going to have to type it yourself, just as in the Car
example.
4. Make sure you add no argument default constructor because once compiler will not
add if you have added any constructor in Java.
6. Its best practice to have one primary constructor and let overloaded constructor calls
that. this way your initialization code will be centralized and easier to test and maintain.
Destructor
Destructor make for destroy the resources of object
Destructor automatically call when class object is destroy
Destructor is call just before when object going to destroy
Destructor is a instance member function of class
Name of Destructor is same as the name of class
Destructor can never be static
Destructor has no return type
Destructor takes on argument therefore no overloading is possible
Destructor use for destroy the resources before the object destroying
You can create abstract functions, but you need to declare the parent class as abstract, too:
When you have a function in the base class, you can Redefine or Override it in the derived
class.
Redefining a method: A new implementation for the method of base class is given in the
derived class. Does not facilitate Dynamic binding.
Overriding a method: Redefining a virtual method of the base class in the derived class.
Virtual method facilitates Dynamic Binding.
The virtual function provides the ability to define a function in a base class and have a
function of the same name and type in a derived class called when a user calls the base
class function. That is often called run-time polymorphism, dynamic dispatch, or run-time
dispatch because the function called is determined at run time based on the type of the
object used.
The keyword virtual tells the compiler it should not perform early binding. Instead, it should
automatically install all the mechanisms necessary to perform late binding
Abstract Function:
1. It can be declared only inside abstract class.
2. It contains only method definition not the implementation.
3. It must be overridden
, "An Abstract Method must have to implement in the child class"
** I felt it is like.
It is not necessary that an abstract method has to be implemented in a child class, if the
child class is also abstract..
1) An abstract method cant be a private method. 2) An Abstract method cant be
implemented in the same abstract class.
I would say ..if we are implementing an abstract class, you must have to override the
abstract methods from the base abstract class. Because.. Implementing the abstract
method is with override key word .Similar to Virtual method.
It is not necessary for a virtual method to be implemented in an inherited class.
Abstract function cannot have a body and MUST be overridden by child classes
Virtual method: A class can have a virtual method. The virtual method has an
implementation. When you inherit from a class that has a virtual method, you can override
the virtual method and provide additional logic, or replace the logic with your own
implementation.
Virtual method change certain behavior In child class
fun days
Object Oriented Programming Tutorial
INNERCLASS
Inner classes, also called Nested Classes, are nothing but classes that are defined within other classes. The
nesting is a relationship between classes, not objects.
Inner classes have clearly two benefits, name control & access control. In Java, this benefit is not as important
because Java packages give the name control.
Java inner classes have feature that makes them richer and more useful. An object of an inner class has an
implicit reference to the outer class object that instantiated it. Through this pointer, it gains access to any variable of
the outer object. Only static inner classes don’t have this pointer. It is actually invisible when we write the code, but
compiler takes care of it. Inner classes are actually a phenomenon of the compiler and not the JVM.
Inner classes may be defined with following access modifiers : public, protected, private, or with default package
access.
The syntax for inner class is as follows:
2 {
3 code...
5 {
6 code....
}
7
}
8
Inner Classes:
The outer class (the class containing the inner class) can instantiate as many number of inner classobjects as it
wishes, inside it’s code.
If the inner class is public & the containing class as well, then code in some other unrelated class can as well
create an instance of the inner class.
In above case the inner class can be created as follows:
No inner class objects are automatically instantiated with an outer class object.
If the inner class is static, then static inner class can be instantiated without an outer class instance, otherwise,
the inner class object must be associated with an instance of the outer class.
Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what
the access level of the elements is), if the inner class has a variable with same name then the outer class’s variable
can be accessed like this:
<OuterClassName>.this.<variableName>
The outer class can call even the private methods of the inner class.
current weather
Object Oriented Programming Tutorial
1
<access-specifier>
2 class OuterClassName
3 {
5 {
6
}
7
}
8
Static members of the outer class are visible to the static inner class, whatever their access level be.
Non-static members of the outer class are not available, because there is not instance of the outer class.
An inner class may not have static members unless the inner class is itself marked as static.
Sometimes static nested class are not referred to as inner class at all, as they don’t require outer classes instance.
A static inner class is just like any other inner class, but it does not have the reference to its outer class object that
generated it.
There are two more types of inner classes, i.e local inner classes & anonymous inner
classes. The local inner class is defined within a method. Anonymous inner classes are
also defined with in a method but have no name.
Local Inner Classes:
1 <access-specifier>
2 class <OuterClassName>
3 {
Object Oriented Programming Tutorial
4 code...
5 <access-specifier> <return-type>
<MethodName>(<arguments>)
6
{
7 class <LocalInnerClassName>
8 {
9 code...
10 }
code...
11
}
12
code...
13
14
}
15
Local classes are never declared with an access specifier (that is, public or private). Their scope is always
restricted to the block in which they are declared.
Local classes have a great advantage: they are completely hidden from the outside world.
They can not only access the instance variables but local variables of the method(in which they are defined) as
well, but the local variable has to be declared final.
Wrapper class
Wrapper class provides the mechanism to convert primitive into object and object into
primitive.
Those class we only use the primitive data type every data type have own wrapper class.
Java is an object-oriented language and as said everything in java is an object. But what about the primitives? They
are sort of left out in the world of objects, that is, they cannot participate in the object activities, such as being
returned from a method as an object, and being added to a Collection of objects, etc. . As a solution to this problem,
Java allows you to include the primitives in the family of objects by using what are called wrapper classes.
There is a wrapper class for every primitive date type in Java. This class encapsulates a single value for the
primitive data type. For instance the wrapper class for int is Integer, for float is Float, and so on. Remember that the
primitive name is simply the lowercase name of the wrapper except for char, which maps to Character, and int,
which maps to Integer.
worldwide weather
Object Oriented Programming Tutorial
The wrapper classes in the Java API serve two primary purposes:
To provide a mechanism to “wrap” primitive values in an object so that the primitives can be included in activities
reserved for objects, like as being added to Collections, or returned from a method with an object return value.
To provide an assortment of utility functions for primitives. Most of these functions are related to various
conversions: converting primitives to and from String objects, and converting primitives and String objects to and
from different bases (or radix), such as binary, octal, and hexadecimal.
The wrapper object of a wrapper class can be created in one of two ways: by instantiating the wrapper class with
the new operator or by invoking a static method on the wrapper class. We will explore this further in this article.
ABSTRACT CLASS
An abstract class is something which is not concrete, something which is incomplete.
Abstract class specifies what functionality is provided but not how that functionality is provided.
An abstract class is a class that is declared abstract.
Abstract class may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be sub-classed.
An abstract method is a method that is declared without an implementation (without braces, and followed by a
semicolon), like this:
abstract void move To(double X, double Y);
An abstract Method doesn't have method body.
Use abstraction if you know something needs to be in class but implementation of that varies. e.g for Human class
you know there will be eating method but eating method implementation will be different for different country.
If you declare any method in class as abstract,you have declare class also as abstract.
?
package in.java4all.abstractclass;
moon phases
Object Oriented Programming Tutorial
1
package in.java4all.abstractclass;
2
3
public abstract class AbstractClassWithoutAbstractMethod {
4
7 }
eclipses
Object Oriented Programming Tutorial
Whichever concrete Class extends Abstract Class must implement abstract methods of the class.
1
public abstract Human
2
{
3
public void eating(Food food)
4 {
6 }
9 {
o Now, any Human that wants to be instantiated (like a Indian or American) must implement the speaking method -
otherwise it is impossible to create an instance of that class.
o Let's look at a Indian and American subclass that extends the Animal class.
?
INTERFACE
An interface in the Java programming language is an abstract type that is used to specify an interface classes
must implement. Interfaces are declared using the interface keyword, and may only contain signature and constant
declarations (variable declarations that are declared to be both static and final. An interface may never contain
method definitions.
Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement
all of the methods described in the interface, or be an abstract class.
As you've already learned, objects define their interaction with the outside world through the methods that they
expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for
example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press
the "power" button to turn the television on and off.
interface Interface
1
{
2
void test1();
3
abstract void test2();
Object Oriented Programming Tutorial
5 }
Note :Inside a interface all methods are Abstract & Public by default.
Ex.2
interface Interface1
1
{
2
void test1();
3
}
4 interface Interface2
5 {
6 void test2();
7 }
{
9
void test3();
10
}
11
class A implements Interface3 /* All methods in the interface
12 must be implemented within a class.*/
13 {
18 {
19 System.out.println("test2");
}
20
public void test3()
21
{
22
System.out.println("test3");
23
}
24
}
25 class InterfaceMain
26 {
28 {
A d1 = new A();
29
d1.test1();
30
d1.test2();
31
d1.test3();
32
System.out.println("done");
33
}
34 }
35
36
37
38
O/P:
Note: An interface can't inherit from a class in Java but only we can implements.
Concrete Class
A concrete class is a class that can be used to create an object. An abstract class cannot
be used to create an object (you must extend an abstract class and make a concrete class
to be able to then create an object).
if we override all pure virtual function in derived class then it becomes concrete class
An abstract class is meant to be used as the base class from which other classes are derived.
The derived class is expected to provide implementations for the methods that are not
implemented in the base class. A derived class that implements all the missing functionality is
called a concrete class.
A concrete class in java is any such class which has implementation of all of its inherited
members either from interface or abstract class.
Concrete and Abstract are opposite of each other. So let me tell you something about
abstract classes so that you can easily understand Concrete Classes
Abstract Class
An Abstract Class is a class that has one or more pure virtual member functions. You
cannot make an object (instance) of an Abstract Class
Without "virtual" you get "early binding". Which implementation of the method is used gets
decided at compile time based on the type of the pointer that you call through.
Abstract classes whose object can’t be created are call abstract class.
If a class has multiple methods by same name but different parameters, it is known
as Method Overloading.
If we have to perform only one operation, having same name of the methods increases the
readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number
of arguments, if you write the method such as a(int,int) for two parameters, and
b(int,int,int) for three parameters then it may be difficult for you as well as other
programmers to understand the behavior of the method because its name differs. So, we
perform method overloading to figure out the program quickly.
If we create any pure virtual function in class then it becomes abstract class
All classes can be designated as either abstract or concrete. Concrete is the default. This means
that the class can have (direct) instances. In contrast, abstract means that a class cannot have
its own (direct) instances. Abstract classes exist purely to generalize common behavior that
would otherwise be duplicated across (sub)classes. We'll illustrate this with the following
diagram.
calendars
Object Oriented Programming Tutorial
Concrete classes that can be instantiated; abstract classes cannot directly be instantiated.
Abstract classes are super classes which contain abstract methods and are defined such that concrete
subclasses are to extend them by implementing the methods Abstract class cannot be used to
create an object. Whereas, concrete class can be used to create an object.
Concrete means’ ‘existing in reality or in real experience; perceptible by the senses; real''.
Whereas, abstract means 'not applied or practical; theoretical'.
An abstract class can't be instantiated. Whereas, a concrete one can.
An abstract class is one that has one or more pure virtual function. Whereas
a concrete class has no pure virtual functions
A base class is one that does not inherit from any other class. A derived class does inherit
from another class.
An abstract class is one that has one or more pure virtual functions. A concrete class has
no pure virtual.
An abstract class can be either a base class or a derived class (it is derived from another
abstract class). A concrete class can also be either base or derived. You can even derive
an abstract class from a concrete class, by adding a pure virtual function to the derived
class. But in general use, there is one base abstract class and one or more concrete
derived classes.
Interface Class
An interface in java is a blueprint of a class. It has static constants and abstract methods
only.
The interface in java is a mechanism to achieve fully abstraction. There can be only
abstract methods in the java interface not method body. It is used to achieve fully
abstraction and multiple inheritance in Java.
"An interface is a contract: the guy writing the interface says, "hey, I accept things looking
that way", and the guy using the interface says "OK, the class I write looks that way".
An interface is an empty shell, there are only the signatures (name / params / return
type) of the methods. The methods do not contain anything. The interface can't do
anything. It's just a pattern."
printable calendar
Object Oriented Programming Tutorial
But there are many differences between abstract class and interface that are given below.
1) Abstract class can have abstract and Interface can have only
non-abstract methods. abstract methods.
3) Abstract class can have final, non-final, Interface has only static and final
static and non-static variables. variables.
4) Abstract class can have static methods, Interface can't have static methods,
main method and constructor. main method or constructor.
6) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
7) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
timers
Object Oriented Programming Tutorial
2. An abstract class must contain at least one abstract method with zero or more concrete
methods
5. An abstract class may have some definition and at least one abstract method.
6. A subclass of an abstract class must either implement all the abstract methods of the
abstract class or declare itself as abstract.
Inheritance
Inheritance in java is a mechanism in which one object acquires all the properties and
behaviors of parent object.
The idea behind inheritance in java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields
of parent class, and you can add new methods and fields also.
The extends keyword indicates that you are making a new class that derives from an
existing class.
In the terminology of Java, a class that is inherited is called a super class. The new class is
called a subclass.
alarm clock
Object Oriented Programming Tutorial
Abstraction
Abstraction is the concept of hiding irrelevant details. In other words make complex system simple by hiding the
unnecessary detail from the user.
Abstract class specifies what functionality is provided but not how that functionality is provided.
For example one does not want to understand how engine works. Similarly one does not have to understand the
internal implementation of the software objects.
Abstraction provides a way to hide the less essential properties so as to reduce complexity and increase efficiency.
Abstraction advantage
Abstraction main advantage is that every user will get data according to their exact requirement. User will not get
confused with unnecessary data.
Abstraction in Java or Object oriented programming is a way to segregate implementation from interface.
Abstraction in Java is achieved by using interface and abstract class in Java.
An interface or abstract class is something which is not concrete , something which is incomplete.
stopwatch
Object Oriented Programming Tutorial
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as
encapsulation. For example: capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here
Encapsulation in java is a process of wrapping code and data together into a single unit,
for example capsule i.e. mixed of several medicines.
We can create a fully encapsulated class in java by making all the data members of the
class private. Now we can use setter and getter methods to set and get the data in it.
Encapsulation is the technique of making the fields in a class private and providing access to the fields through
public methods. Encapsulation is also called as data hiding.
The benefits of encapsulation:
The ability to modify our implemented code without breaking the code of others who use our code
Encapsulation gives maintainability, flexibility, and extensibility to our code.
The fields of a class can be made read-only or write-only.
A class can have total control over of what is stored in its fields.
A class can change the data type of a field, and users of the class do not need to the changes any of theircode.
By providing only setter or getter method, you can make the class read-only or write-
only.
countdown
Object Oriented Programming Tutorial
It provides you the control over the data. Suppose you want to set the value of id i.e.
greater than 100 only, you can write the logic inside the setter method.
class foo{
private:
int a, b;
public:
foo(): a(0), b(0)
{ }
int add()
{
return a+b;
}
}
Internal representation of any object of foo class is hidden outside the class. -->
Encapsulation.
foo foo_obj(3, 4);
int sum = foo_obj.add();
Implementation of method add is hidden. --> Abstraction.
Use of foo_obj object above is also an example of abstraction as it is used without any
details of its implementation.
Abstraction--- Hiding Implementation--at Design---Using Interface/Abstract classes
Encapsulation is -
Hiding Complexity,
Binding Data and Function together,
Making Complicated Method's Private,
Making Instance Variable's Private,
Hiding Unnecessary Data and Functions from End User.
Encapsulation implements Abstraction.
And Abstraction is -
week number
Object Oriented Programming Tutorial
Data Encapsulation simply means wrapping and controlling access of logically grouped
data in a class. It is generally associated with another keyword
A simple example would be defining a private variable and giving access to it using getter
and setter methods or making a method private as it's only use is withing the class. There is
no need for user to know about these methods and variables.
Note : It should not be misunderstood that encapsulation is all about data hiding only. When
we say encapsulation, emphasis should be on grouping or packaging or bundling related
data and behavior together.
Data Abstraction on the other hand is concept of generalizing so that the underneath
complex logic is not exposed to the user. In Java this is achieved by
using interfaces and abstract classes.
Example -
Lets say we have an interface Animal and it has a function makeSound(). There are two
concrete classes Dog and Cat that implement this interface. These concrete classes have
separate implementations of makeSound() function. Now lets say we have a animal(We get
this from some external module). All user knows is that the object that it is receiving is some
Animal and it is the users responsibility to print the animal sound. One brute force way is to
check the object received to identify it's type, then typecast it to that Animal type and
then call makeSound() on it. But a neater way is to abstracts thing out. Use Animal as
a polymorphic reference and call make Sound() on it. At runtime depending on what the
real Object type is proper function will be invoked.
Abstraction is generalized term. i.e. Encapsulation is subset of Abstraction.
weekdays calculator
Object Oriented Programming Tutorial
Abstraction Encapsulation
Abstraction refers to the act of representing essential features without including the
background details or explanations.
Encapsulation is a technique used for hiding the properties and behaviors of an object and
allowing outside access only as appropriate. It prevents other objects from directly altering
or accessing the properties or methods of the encapsulated object.
Difference between abstraction and encapsulation
1.Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation
(information hiding) prevents clients from seeing it’s inside view, where the behavior of the
abstraction is implemented.
2.Abstraction solves the problem in the design side while Encapsulation is the
Implementation.
Abstraction : Abstraction is process in which you collect or gather relevant data and
remove non-relevant data. (And if you have achieved abstraction, then encapsulation also
achieved.)
Encapsulation: Encapsulation is a process in which you wrap of functions and members in
a single unit. Means You are hiding the implementation detail. Means user can access by
making object of class, he/she can't see detail.
Difference between Abstraction and Encapsulation :-
Abstraction
1. Abstraction solves the problem in the design level.
2. Abstraction is used for hiding the unwanted data and giving relevant data.
3. Abstraction lets you focus on what the object does instead of how it does it.
4. Abstraction- Outer layout, used in terms of design. For Example:- Outer Look of a
Mobile Phone, like it has a display screen and keypad buttons to dial a number.
Encapsulation
1. Encapsulation solves the problem in the implementation level.
2. Encapsulation means hiding the code and data into a single unit to protect the data
from outside world.
3. Encapsulation means hiding the internal details or mechanics of how an object does
something.
4. Encapsulation- Inner layout, used in terms of implementation. For Example:- Inner
Implementation detail of a Mobile Phone, how keypad button and Display Screen are
connect with each other using circuits
Abstraction refers to showing only the necessary details to the intended user. As the name
suggests, abstraction is the "abstract form of anything". We use abstraction in programming
languages to make abstract class. Abstract class represents abstract view of methods and
properties of class.
Abstraction and Encapsulation in Java are two important Object oriented programming
concept and they are completely different to each other.
Encapsulation is a process of binding or wrapping the data and the codes that
operates on the data into a single entity. This keeps the data safe from outside
interface and misuse.
age calculator
Object Oriented Programming Tutorial
Abstraction is the concept of hiding irrelevant details. In other words make complex
system simple by hiding the unnecessary detail from the user.
Polymorphism can be achieved through overriding. Put in short words, polymorphism refers
to the ability of an object to provide different behaviors (use different implementations)
depending on its own nature. Specifically, depending on its position in the class hierarchy.
Inheritance
Polymorphism is an important Object Oriented concept and widely used in all Object Oriented Programming
Language.
Polymorphism word comes from ancient Greek where poly means many. Polymorphism means many form.
Polymorphism in Java means same object behave differently in different place.
For e.g Lady is one of the example polymorphism. Same Lady object will be behave differently when she is sister,
wife and mother.
There are two types of polymorphism in java: compile time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
If you overload static method in java, it is the example of compile time polymorphism. Here,
we will focus on runtime polymorphism in java.
Polymorphism means more than one form, same object performing different operations
according to the requirement.
1. Method overriding
2. Method overloading
date calculator
Object Oriented Programming Tutorial
Method overloading means writing two are or more methods in the same class by using
same method name, but passing the parameters is different.
Method overriding means we use the method names in the different classes, that means
parent class method is used in the child class.
Actually, there are multiple forms of polymorphism and there is quite some controversy over
it; you may even see CS professors who cannot define it properly. I am aware of three
types:
ad-hoc polymorphism (looks like a duck and walks like a duck => is a duck). Can be
seen in Haskell and Python for example.
generic polymorphism (where a type is an instance of some generic type). Can be seen
in C++ for example (vector of int and vector of string both have a member function
size).
subtype polymorphism (where a type inherits from another type). Can be seen in most
OO programming languages (i.e. Triangle is a Shape).
ad hoc polymorphism
ad hoc polymorphism is the act of providing multiple implementations of the same method
for different parameter types. In OOP, it's generally known as method overloading. For
example:
public String format(int a) {
return String.format("%2d", a);
}
If Cat and Dog are both subclasses of Animal, then they share a single interface but are in
fact different types.
I'm going off in a bit of a tangent here, but subtype polymorphism is (I think) the only one
that allows overriding: the act of redefining the behavior of a method defined by a parent
class. This is often confused with overloading which, as we saw before, is a type of
polymorphism and doesn't in fact need sub classing (nor does it need classes, really).
There are two kinds of polymorphism :
1. Compile Time (Static) Polymorphism or (Ad Hoc) Polymorphism.
that is simply Method Overloading and Operator overloading
2. Run Time or (Dynamic) Polymorphism.
The first Term is inherited from The java and C++ terminology.
But in the .Net terminology only the second one (I mean Run Time Polymorphism) is
really supposed as Polymorphism and simply called as Polymorphism.
And as far as I know there are three methods for implementing (Run Time) Polymorphism.
1. Parametric Polymorphism or simply the use of Generics(Templates in c++).
3. Interface-Based Polymorphism.
hijrahi holidays
Object Oriented Programming Tutorial
What do you mean by different method signature for method Overloading in Java
o If number of argument is different.
o If type of argument is different.
o If sequence of argument is different ,only when arguments of different types
In the example given below, both the classes have a data member speed limit, we are
accessing the data member by the reference variable of Parent class which refers to the
subclass object. Since we are accessing the data member which is not overridden, hence
it will access the data member of Parent class always.
christian holidays
Object Oriented Programming Tutorial
Chinese holidays