Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOPs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Constructor ::

-It is also instance of a class.


-It is used to initialize an object of class, due to this reason we can’t use any return type like
void.
-It is used to initialized data member of a class & to load non static member (i.e instance
variable) of a class in to object.
-In java programming language, each and every class has constructor.(Means Default
constructor)
-While creating constructor ---
-constructor name should be same as class name
-We should not declare any return type i.e void for the constructor
-Any no. of constructor can be declared inside a java class by passing different arguments
or parameter in to constructor signature () but constructor name should be same as class
name
Due to the arguments, constructors are classified in to two types –
a) Default constructor: Created by compiler (Its means no argument constructor)
-When we do not declare any constructor in java class then at the time of compilation
compiler will be provided constructor for the class that constructor known as default
constructor.
Ex-We can say, main method (main (String args [])) or any static method is a default
constructor.
b) User define constructor: If the programmer is defined constructor in java class then it is
considered as user define constructor it is classified two types
1) No argument constructor: Constructor with no argument or parameter of a class it is
known as zero argument or non-argument constructor.
-Zero argument () is same as default constructor signature ();
Ex Constrictor name () {With body}
2)Argument or parameterized constructor: A constructor is called parameterized contractor
when it accepts a specific parameter or argument
-We can achieve Parameterized constructor within same class by passing different
arguments
- We can achieve Parameterized constructor within same class by passing different Data
type and different variable as arguments
- We can achieve Parameterized constructor within same class by passing same data type
with different variable name as argument or parameter
--------------------------------------------------------------------------------------------------------------------------
**Can we declare constructor as private?
ANS -We can make constructor Public, private, protected and default,
- If we declare constructor as private means we restricted a class to create object to
outside of the class means we can’t create the instance of the class from outside of
the class because private belongs to class only & it shows the compile time error.
** Can we declare constructor as final?
Ans- No, constructor can’t be final, we cannot override (Inherited) the constructor so write
final before constructor is no sense hence java does not allow final keyword before
constructor
*** Can we make constructor as static?
Ans-We cannot define a static constructor in java if we are trying to define constructor with
static keyword a compile time error will occurred because static means class level existence
*** Can constructor overridden?
Ans- No, constructor cannot be overridden because it does not have any return type & its
name is same as the class name if we try to write a super class constructor in the sub class
then compiler treat as method & except a return type it will be generate a compile time
error
***What is encapsulation?
Ans-Encapsulation is used to hide the data, internal structure & implementation details of a
object.
The class should be encapsulated class contain data member should be private & method
should be public.
Def:: It is he process of combining or binding a data member to its corresponding method
with hiding the data member in to a single unit is called as encapsulation
-In encapsulation we used getter and setter to read & write purpose.
-We keep variable and method in one place, means in class which is the base of the
encapsulation
-Keyword return use to return from method when its execution is complete.
***Inheritance****
Def: In java programming one class can acquire the properties of another class with the help
of extends Keyword is called as inheritance.
-Extends KW is used to increase the functionality of new class .
-Inheritance in java use for method overriding, for code reusability.
a)-Single Level inheritance: When class is inherited another class with the help of extend
KW in that case Perform single inheritance.
b)-Multi Level Inheritance::(perform between 3 or more than 3 classes)
-In this one sub class can acquire the properties of super class & this phenomena of chain of
inheritance are known as multi-level inheritance .
c)-Multiple inheritance ::
-In this one sub class can acquire the properties of two super classes at a same time is
known as multiple inheritance.
-Java class doesn’t support multiple inheritance because it cause ambiguity, it means JVM &
object variable get confuse for whom need to inherited the properties of super class to sub
class.

d)- Hierarchical Inheritance::


-It takes place between one super class & multiple sub classes.
-The properties of parent class or base class or super class can acquire by multiple
independent child class or extended class or derive class or subclass is called as Hierarchical
inheritance.
e)-Hybrid Inheritance :: (Comb-Multiple inheritance &Hierarchical Inheritance)
-In this case one sub class can acquire the properties of two super class & two sub classes
acquire the property of one super class
Note:: Object class is the super class of the all classes
***Polymorphism***
Def :: One object shows the different behaviour at different stages of life cycle(Relation with
classes)is known as polymorphism.
-In this we can perform a single action in different ways .
1-Compile time polymorphism ::
-In this case method declaration going to get bonded to its definition at compile time base
on argument so this called as compile time polymorphism. OR
-At compile time, Java knows which method to call by checking the method signature ()
where we can pass the argument or parameter called as static binding or early binding.
-In early binding, method definition gets bonded with method declaration at the time of
compilation or before execution or before run time the program.
-The argument or parameter value are fixed through out the program or method so it
known as static binding.
-Compile time Polymorphism can be achieved through method overloading.
*Method overloading
-Declaring the multiple methods with same method name but different argument or
parameter in a same class
-Method overloading, we can achieve in two way ,By passing same argument & By using
different data type with variable .
*Run time polymorphism
-In this method declaration going to get bind with to its definition at the time of execution
or run time base on object creation is known as Run time polymorphism or dynamic binding
or late binding.
-For achieving run time polymorphism, we must need to perform inheritance.
*Method overriding
-By acquiring super class method in to sub class with the help of extends KW & Changing the
implementation according to sub class specification that is called method overriding.
-Method overriding is best example of runtime polymorphism.
****Abstraction***
Def:: It is process of hiding the data implementation programme & providing only
functionality to the end user is known as abstraction.
-Abstraction we can achieve by two way ---
a) Abstract class::
-A class is a which is declare with abstract keyword is known as abstract class.
-It is a combination of complete method & incomplete methods are present in
abstract class.
-We are not able to create an object of abstract class. In abstract class we have static
method constructor as well.
-We can do Only non-static method as an abstract method.
***Concrete class***
-A class which provide the definition to the all incomplete method(Abstract
method)which are present inside abstract class by using inheritance with the help of
extends Keywords is called as concrete class.
***Interface***
Def :: Interface is same as class we can say blue print of class
-An Interface abstract in nature ,It means only abstract(Incomplete) methods are
present.
-An Interface is declared with interface keyword.
Syntax -- > public interface interface name {}
-An interface is mechanism to achieve the abstraction.
-From java 8, we can have default & static method in an interface as well as private
method.
-We can declare variable and data types (Means data member) inside the interface
by
Default it is static & public as well as final.
-We can write only static method inside interface also we can write main method but
we can not write non static method (only incomplete or abstract non static method
we can write)
-We cannot create constructor inside an interface
-We can not create an object of an interface class because we cannot complete non-
static method inside an interface, to complete the abstract method of interface we
need to use class so ,that we are able to create an object of that class is called as
implementation class.
-We need to create relation between interface & class we have to used KW
“implements”.
-While completing incomplete interface method in to implemented class it is
mandatory to use public access specifier in front of the method.
-We can achieve all the types of inheritance with multiple inheritance through
interface
-We can extends more than 1 interface in to another interface means interface
supports multiple inheritance by using extends KW
- Also, we can use multiple interfaces in to implemented class by using implements
KW
***Casting
Def:: In casting, converting one type of information in to another type of information is
known as casting.
a) Primitive casting (Conversion casting)-To convert one data type information in to
another type of data type information
-Implicit casting (Automatic conversion)::
-The lower data type of information converted in to higher data type information is
called as implicit casting or widening casting .
-In this casting the memory size goes on increasing therefore we called it as
widening casting.
Flow of data type conversion-
byte(1byte)> short(2byte>int(4byte)>long(8byte)>float(4byte)>double(8byte)
-Explicit or manual or narrowing casting ::
Def :: In this casting Higher data type of information converted in to lowe data type
of information.
-While doing explicit casting data loss takes place so it is called as narrowing casting.
-Narrowing casting must be done manually by placing or writing the data type name
in parenthesis() Infront of variable which is to be converting .hence we called as
manual conversion
-This casting is useful for in incompatible data type here automatic conversion can
not be done.
Flow of data conversion
byte(1byte)< short(2byte>int(4byte)<long(8byte)<float(4byte)<double(8byte)
Ex-1--- float a =20.23f;
Short d= (short-manual)a;
Ex-2—char x= ‘A’;
byte Y=(byte-Manual)x;
-Boolean data type::

Boolean return True or False value so converting true in to false and vice versa are
not possible hence java doesn’t support the Boolean casting.
b) Non primitive casting::
Def ::To convert the one type of class(User defined class) in to another type of class
is known as non-primitive type of casting.
1_Upcasting:: To acquire the sub class properties in to super class is called as
upcasting.
-We have to perform the inheritance operation between two classes before perform
form upcasting operation on it.
-At the time of upcasting the properties which are inhered from the super class are
only eligible for the operation & the new or extra properties which are added or
declare inside the sub class are not eligible for upcasting.
-While performing the upcasting the object orientation get change,
Syntax-- > Parent p = new child ();
Ex- super class= father class
Sub class= son class
Father s = new son ();
-

You might also like