0% found this document useful (0 votes)
264 views

Oops

OOPS is an object-oriented programming technique that uses objects and classes to model real-world scenarios. It provides some key benefits over traditional procedural programming including abstraction, encapsulation, inheritance and polymorphism. Some key aspects of OOPS include classes, which provide templates for objects with attributes and behaviors; objects, which are instances of classes that occupy memory; and inheritance, which allows classes to inherit attributes and behaviors from parent classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
264 views

Oops

OOPS is an object-oriented programming technique that uses objects and classes to model real-world scenarios. It provides some key benefits over traditional procedural programming including abstraction, encapsulation, inheritance and polymorphism. Some key aspects of OOPS include classes, which provide templates for objects with attributes and behaviors; objects, which are instances of classes that occupy memory; and inheritance, which allows classes to inherit attributes and behaviors from parent classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

OOPS

OOPS is a technique to write programs that reflect real world scenarios and
represent real world situations like they exist in real world..

That is we write our programs in terms of objects and classes instead of variables
and datatypes.

It is a new way of programming or a new approach to programming.

Problems with procedures :

1) no dynamic declarations.. There is a structured way of programming. All the


declarations are to be completed at one place. Once an executable statement starts,
we cannot declare any more variables in this programs.

2) Once the program size increases to 2 or 3 lakh lines of code, then it becomes
unmanageable.

3) Lot of function calls are needed to modularize..

4) Also the abstraction mechanism is very poor in these languages.

**********************************************************
OOPS

The keyword of OOPS are :

1) Abstraction
2) Encapsulation
3) Class and object
4) Inheritance
5) Polymorphism.

Any language that follows all the above principles can be called as an OOP
language.
Java is 100% pure oop language.

**********************************************************

Class : Is a logical abstraction. An abstract datatype.


An user-defined datatype.
It is also a blueprint from which we can create objects...

It is we the programmers define how our classes should be and how they should
behave..

A class will have state/data/attributes and


behaviour/functionality/methods...

A class is an abstraction that combines (encapsulates) the data and the methods
that can be applied on that data.

Since class is just a logical entity, it doesn't occupy any memory.

ex : class Car{
int year; //attributes....
String model;
//methods....
void startCar(){
syso("Car is started");
}
void run(){
syso("Car is running");
}
void stopCar(){
syso("Car stopped");
}

public static void main(String []args){

Car maruti=new Car(); //instatinating an


object..
Car n=new Car();

Car audi=new Car(),benz=new Car(),bmw=new Car();//multiple objects creation.......

int x=10;
maruti.year=1999;
maruti.model="zen";

maruti.startCar();
maruti.run();
maruti.stopCar();

}
}

Object : An object is an instance of a class..And since objects are real, they


occupy memory in the heap.

An object is also real world object that can be distinguishable from other such
objects...

Objects are created with the keyword "new".

The (.) operator is used to reference the class variables and the class methods on
the object.

*********************************************************
method overloading

It is a technique where multiple methods can have the same name but different
signatures..

void run() ------> method signature...

class A{
int add(int a,int b){
return (a+b);
}

double add(double a,double b){


return(a+b);
}

int add(int a,int b,int c){


return(a+b+c);
}

public static void main(){

A a=new A();

syso(a.add(10,12));

syso(a.add(12.5,23.5));

syso(a.add(12,23,34));

}
}

The method to be dispatched depends on the matching signature. This is called


dynamic dispatch.

Since the compiler can distinguish between the methods by looking at the method
signatures, this is an example of
"compile time polymorphism"...

We cannot overload on return type only.

**********************************************************
constructor

A constructor is a special method that initializes an object.

It gives some initial values to the object.

Rules : 1)The name of the constructor should be the same as the class.
2) Constructor cannot return any value.
3) It can be overloaded.

There are 3 types of constructors:

1) Default constructor.
2) Parameterized constructor.
3) Overloaded constructor.

class Student{
int rollno;
String name;
String city;

Student(){} //default constructor....


The JVM will internally put a default constructor for every class even if we
do not put one.It will not be visible to us.

Student(int r,String n){

rollno=r;
name=n;
}

Student(int r,String n,String c){


rollno=r;
name =n;
city =c;
}

**********************************************************
this keyword

The 'this' keyword return the current class reference.

**********************************************************
static keyword

The static keyword is used for memory management.


It can be applied to 3 different entities.

1) static variable: If a variable is declared as static, it belongs to the class


area.

Memory is allocated only once.

We donot need any object creation to call this variable inside the main.

It is like a global variable.

It can be applied on every object by default.

2) static method : A method, when declared as static, belongs to the entire class
and we can call this method directly inside the main without any object creation...

A static method can access the static variable and also change it.

3) static block : This was introduced from the jdk1.7 onwards.... This is the first
block that gets executed even before the main method....

**********************************************************

Inheritance (18-04-2017)

Inheritance is a technique thru which a class can pass on its attributes and
methods to another class.

The class that passes over is called the Parent/Base/Super class.

The class that receives is called child/derived/sub class.

A parent can have many children but a child can have only one parent in java.

Java doesn't support multiple inheritance.It only supports multi-level inheritance.

The keyword 'extends' is used to acheive inheritance.

Inheritance represents an "IS-A" relationship.

Child "Is-A" child of Parent.


**********************************************************
ex:

class A{
int a;
String b;
void start(){}
void run(){}
}
class B extends A{

}
class C extends A{
public static void main(String []args){
B b=new B();
b.start();
b.run();
}
}

**********************************************************
method overriding

It is a technique thru which the child class can change the behaviour it inherited
from the parent class.

rules: The methods should be exactly same(including the signature).


The classes should have an "Is-A" relationship between them.

********************************************************** class A{
int a;
String b;
void start(){}
void run(){}
}
class B extends A{
void start(){
syso("B started");
}
void run(){
syso("B is running");
}
class C extends A{
void start(){
syso("A started");
}
void run(){
syso("A is running");
}

public static void main(String []args){


B b=new B();
b.start();
b.run();
}
}

**********************************************************
note : Since the method to be dispatched depends on the object that we create at
run-time,this overriding can also be called as "run-time polymorphism".

**********************************************************
super keyword
The super keyword is used to reference the super class variable inside the child
class..If both the child class and the super class have the same variable name
inside them, then we can call the super class variable inside the child class using
the keyword "super".

class A{
int speed=100;
void run(){
syso("car is running at speed :" + speed);
}
class B extends A
int speed =120;
void run(){
syso("Car is running at speed :"+ super.speed);
}

**********************************************************
final keyword

The final keyword can be applied on an entity to restrict the user.

It can be used on

1) variable : a final variable is like a constant. If we make a variable final its


value cannot be changed.

2) method : If we make a method final then it cannot be overridden.

3) class : If we make a class final it cannot be inherited.

**********************************************************
abstract class

An abstract class is a class with atleat one abstract method. An abstract method is
an incomplete method which is declared but not defined in the parent class.

The class that inherits the abstract class should implement the abstract method..

This is one of the ways java can acheive abstraction...

The keyword 'abstract' is used to declare a class as abstract.

An abstract method starts with the keyword abstract and ends with a semicolon(;).

An abstract class can have both abstract and non-abstract methods.

**********************************************************

abstract class A{
abstract void run();
}
class B extends A{
void run(){
syso("Car is running");
}
public static void main(String args[]){

B b =new B();
b.run();
A a=new A(); //error...
A a=new B(); //reference object....
}
}

Note : We can also create an object of the abstract class but we should point it to
the child class(reference object).

**********************************************************
Interface

Interface is like a pure abstract class.


It contains only abstract methods.
All the methods inside an Interface are by default abstract. We need not use the
keyword abstract.

We can declare an interface using the keyword 'interface'.


The class should implement the interface using the keyword 'implements'.

We can also create a reference object for the interface.

First big advantage of interface is Java can implement multiple inheritance thru
the concept of interfaces...

Second advantage is : an interface can extend another interface...also called


interface inheritance...

Third advantage : A class can at the same time extend another class and also
implement any number of interfaces...

**********************************************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy