OOPs
OOPs
Programming Paradigm:
Types or Way of writing a code is known as Programming Paradigm.- Procedural
- OOPs
- Functional
Procedural Paradigm:
set of instructions/Functions it is a chain of functions which will call other function and it will start from one function which is known as main.
Organuize code into set of functions & each func. may call another.
Execution starts with special func. called main.
Problem in this way of coding:
- It is not connected to real world. It means that someone will do something and something will happen with someone.
- To have multiple parameters in a function we use struct in this paradigm and it don't have any method inside it.
Disadvantage:
- Difficult to make sense in big system
- In this way of coding we need to go from one function to another and any function can call any function and that is a problem and this chain of function causing issue.
- It leads to spaghetti code.
Solution to Procedural coding is OOPs.
OOPs
- Object oriented programming.
- Programming paradigm which revolved around entities and entities have it's own behaviour and control it's own attributes.
- Entities are heart of software system.
Terms in OOPs: Java the complete reference book help to understand it in detail.
- Abstraction - some consider is as pillar and some as principle of OOPs
- Principle means - Guidelines, set of rules. Ethics.
- Hiding the functionality of any code.
- Representing a software in terms of ideas
- Purpose of it is to make user free from knowing the internal mechanism of any software, we will get what he want.
- Polymorphism - pillar - foundation
- Poly means many and morph means forms.
- Class/Object can have many forms.
- Parent p = new child(); is allowed in polymorphism as parent can have multiple form as per the child object is created as child can inherit parent properties.
- but child c = new Parent(); is not allowed.
- We can create list of parent as well like this:
- List<parent> parentVariable = new ArrayList();
- parentVariable.add(new parent());
- parentVariable.add(new child());
- It help to solve the issue like below:
- printDetails ( List<user> users) { for (user u: users) { s.o.p (u.name);}}
- Types of Polymorphism:
- Runtime - overriding
- It will be deduced on runtime.
- When Parent and Child class have same method but their working is different.
- Object will be created on runtime not on compile time at compile time only class definition is understood.
- Here the attribute value will be scoped with respect to parent class and method will be prioritized as per child class object is used by parent variable at runtime.
- So child will override the parent method.
- Now if child don't have the method which is called then parent method will be called.
- Compile Time - overloading
- We can have two methods with same name just need to change the parameter.
- So all the methods which has same name and same return datatype and have different parameters in number or have different datatypes, then it is known as method overloading. As compiler can understand it and can understand it through it's synctax and signature.
- Encapsulation - pillar - foundation
- It give you idea of using the medicine which is present in a capsule.
- Use of capsule is to hold the medicine together
- To protect the medicine from the outer world
- Same encapsulation do in software
- It collect attributes and behaviours together
- It even allow us to protect attributes and methods from illegal usage.
- To do it in Java we need to use class.
- Class:
- It is blueprint of any idea. It is a detail of what is needed and how an idea will be implemented.
- Advantage is that we can create multiple objects out of it.
- Class which is blueprint don't have memory and don't occupy space.
- Objects of class have memory and occupy space.
- To achieve it we need to use constructors and access modifiers.
- Inheritance - pillar - foundation
- Heirarchy of class. - It means the parent class behaviour can be inherited by the child class.
- This inheritance is done through the default constructor as first parent constructor is called and then the child one.
- This is known as constructor chaining.
- Example: A -> B -> C -> D then D d = new D(); will give output as A,B,C,D.
- We can make a constructor of any class private.
Part of Encapsulation:
Access Modifiers:
- Define the level of protectioness
- public - everyone can access it.
- private - no one can access it other than the class itself.
- protected - Can be accessed by child class and class itself.
- default - when not provided any modifiers in a class to attributes and to methods.
Constructor:
- Java create default constructor in code if a user don't create a constructor.
- create object of a class
- assigm default values to attributes of a class.
- If an attribute is having primitive datatype then it will assign default value and it any attribute is derived means class in nature then null will be assigned.
- We can create our own constructor then we need to create a method with same name of class and for it we can mark it as public and it's return type will be always class.
- If user create any constructor then Java will not create any default constructor. As it is a facility of Java which will come in picture only when user is not creating any constructor but once you done then Java will give full control to you and will not take any action in the field of constructors.
- Whenever you create a parameter construtor and your code don't have constructor with default values that don't have parameter as input then you have to provide input to your object creation statement.
Copy constructor and it's example with respect to Java code.
class student
{
int age;
string name;
public student(int age, string name)
{
this.age = age;
this.name = name;
}
#create a copy constructor which will accept class object as input or parameter.
public student( student old_student)
{
this.age = old_student.age;
this.name = old_student.name;
}
}
class main
{
student s1 = new student (35, "Piyush");
# Requirement is to create another object which will be exact copy of first object is.
Solution one is :
student s2 = s1; -> It means you are refering the s2 varaible of datatype student to memory location of s1 object. Here s2 is not an object it is a variable.
solution 2: It is not prefer as you need to write too many lines of code to do a copy task. It will not work if attributes are private.
student s2 = new student(10, "Rupesh");
s2.name = s1.name;
s2.age = s1.age;
}
How copy works in Java:
- Objects in java are stored in heap memory and that address will be returned to the variable which is refering to it.
- Variable always get stored in stack memory and primitive datatypes are also stored in stack memory.
- String in Java is an object in Java as it is not a primitive datatype it is a derived datatype.
- Stack memeory variables can hold data or reference memory of another object.
- new keyword is the only way to create an object.
- So <datatype(primitive/class) variable = new <keyword to create object> <class constructor which will initialize the object) #so new and class method together is object.
After copy constructor creation and use some new scenarios will take place and will cause the issue.
As in Java strings are imutable.
As copy constructor will make two objects refering to same location of memory hence if one changed the value other will see it.
Types of Copy in Java:
- Shallow copy
- New Object is created but behind the scene the attibutes of new object still point to old location.
- New & old both share same data.
- They are dependent.
- Deep copy
- Here copy object don't share memory with old one.
- No one share any memory.
- It is very hard to create 100% hard copy in Java
- As Java have almost most of the time nested objects.
Is Java a passed by value or passed by reference?
Java is passed by value. As if you send a variable as parameter to the method and now it is totally dependent on the variable the parameter variable is having a value inside it or a memory reference and as per it Java will work.
Destructor:
- It is used to destroy the objects.
- You don't need it in Java due to Garbage collector in Java.
Sample code of inheritance:
A.java file content:
package com.java.tutorial.inheritance;
public class A {
int age;
String company;
public A() {
System.out.println("I am in A class constructor");
}
public void doSomrething() {
System.out.println("I am doing something");
}
}
B.java file content:
package com.java.tutorial.inheritance;
public class B extends A {
String name;
private B() {
System.out.println("I am in B class constructor");
}
public B(String name) {
this.name = name;
System.out.println("I am in B class constructor with name parameter = "+ name);
}
public void doSomrething() {
System.out.println("I am doing something in B class");
}
}
C.java file content:
package com.java.tutorial.inheritance;
public class C extends B{
float psp;
public C(String name) {
super(name);
// calling parent class constructor Need to be the first line if need to call the parent specific constructor
System.out.println("I am in C class constructor");
}
}
main.java file content:
package com.java.tutorial.inheritance;
public class main {
public static void main(String[] args) {
C c = new C("Puneet");
}
}
Comments
Post a Comment