We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
< OOPS in Java
OBJECT ORIENTED SVR SYSTEMS
Object-Oriented Programming is a methodology or paradigm to design a program using
classes and objects. It simplifies the software development and maintenance by
providing some concepts defined below:
Class is a user-defined data type which defines its properties and its functions. Class is
the only logical representation of the data. For example, Human beings a class. The
body parts of a human being are its properties, and the actions performed by the
body parts are known as functions. The class does not occupy any memory space till
the time an object is instantiated,
Object is arun-time entity. It is an instance of the class. An object can representa
person, place or any other item. An object can operate on both datamembers and
member functions.
Example 1:
public void getInfo
(Strang (
Anan< OOPS in Java
Example 2:
Pen ne
Note : When an objects created using a new keyword, then space is allocated for the
variable ina heap, and the starting address is stored in the stack memory.
‘this! keyword : ‘this’ keyword in Java that refers to the current instance of,
the class. In OOPS itis used t
1. pass the current object as a parameter to another method
2. refer to the current class instance variable
Constructor : Constructor is a special method which is invoked automatically at the
time of object creation. It is used to initialize the data members of new objects
generally.
@ Constructors have the same name as class or structure.
@ Constructors don’t have a return type. (Not even void)
Constructors are only called once, at object creation.
There can be three types of constructors in Java.
1, Non-Parameterized constructor : A constructor which has no argument is known as
non-parameterized constructor(or no-argument constructor). It is invoked at the
time of creating an object. If we don’t create one then it is created by default by Java.OOPS in Java
2. Parameterized constructor : Constructor which has parameters is called a
parameterized constructor. It is used to provide
different values to distinct objects.
class Student
udent (String mane, int age)
erp ers
3. Copy Constructor : A Copy constructor is an overloaded
constructor used to declare and initialize an object from another object. There is
only [Link] defined copy constructor in Java(C++ has a default one too).
String nane
tudent (Student
thie-nane =
[Link]
nlike languages like C++, Java has no Destructor. Instead, Java has an
efficient garbage collector that deallocates memory automatically.< OOPS in Java
Polymorphism
Polymorphism is the abilitv to present the same interface for differing underlving forms
(data tvoes). With polvmorphism, each of these classes will have different underlying
data. Precisely, Poly means ‘many’ and morphism means ‘forms’.
Types of Polymorphism IMP
1. Compile Time Polymorphism (Static)
2. Runtime Polymorphism (Dynamic)
Let's understand them one by one:
ile Tis ism : The polymorphism which is implemented at the compile
time is known as compile-time polymorphism. Example - Method Overloading
i \ethod overloading is a technique which allows you to have
more than one function with the same function name but with different functionality.
Method overloading can be possible on the following basis:
1. The type of the parameters passed to the function.
2. The number of parameters passed to the function.
public void String name< OOPS in Java
‘untime polymorphism is also known as
dynamic polymorphism. Function overriding is an example of runtime polymorphism.
Function overriding means when the child class contains the method which is already
present in the parent class. Hence, the child class overrides the method of the parent
class. Incase of function overriding, parent and child classes both contain the same
function with a different definition. The call to the function is determined at runtime
is known as runtime polymorphism.
class shay
public void area
System. out. printin( ‘Displays
of Shape
class Triangle extends Shape
class Circle extends Shape
Inheritance
Inheritance is a process in which one obiect acquires all the properties and behaviors of
its parent object automatically. In such a wav. vou can reuse, extend or modify the
attributes and behaviors which are defined in other classes.
In Java, the class which inherits the members of another class is called derived class
and the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.
‘Types of Inheritance:< OOPS in Java
public vosd areal) [
[Link]( ‘Displays Area of Shape’
clase Triangle extends Shape
public void arealint b. ant b)
System. out. printin( (1/2) "bh
2. Hierarchical inheritance : Hierarchical inheritance is defined as the process
of deriving more than one class from a base class.
claee Shape [
public void areal) [
System. [Link]( ‘Displays Area of Shape
class Triangle extends Shape (
public votd aealint b. dnt b)
System. out. printin( (1/2) *b+h
class Circle extends shape [
public void area(int r) {
3. Multilevel inheritance : Multilevel inheritance is a process of deriving a class from
another derived class.
clase Shape
public voad areal) {
System. [Link]| ‘Displays Area of Shape’
class Triangle extends shape [
public void arealint b, int b) [< OOPS in Java
class EquilateralTriangle extends Triangle
4, Hybrid inheritance : Hybrid inheritance is a combination of
simple, multiple inheritance and hierarchical inheritance.
Package in Java
Packaae is a aroun of similar types of classes, interfaces and sub-packages. Packages can be
built-in or user defined
Built-in packages - java, util, io etc.
Access Modifiers
Java
> Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
> Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. Ifyou do not specify any access level, it wil be the
lefault.
> Protected: The access level of a protected modifier is within the packaae and outside the
package through child class. If you do not make the child class, it cannot be accessed from< OOPS in Java
> Puplic: [ne access lever of @ DUDIIC Moaitier Is evervwnere. It canbe accessed Trom withir
the class, outside the class, within the package and outside the package.
package newpackage
clase Account
public
ring none
protected String emai
private
public void setPassword(Stzing paseword
thie
public class Sample
public static void main(Steing are
Account al = new
[Link] = ‘Apna College
Encapsulation
Encapsulation is the process of combinina data and functions into a sinale unit called class. In
Encapsulation, the data is not accessed directv; it is accessed throuah the functions present
inside the class. In simpler words, attributes of the class are kept private and public getter and
setter methods are provided to manipulate these attributes. Thus, encapsulation makes
the concept of data hiding possible.(Data hidina: a lanauage feature to restrict access to.
members of an obiect, reducing the negative effect due to dependencies. e.g. "protected
“private” feature in Java).< OOPS in Java
Abstraction
We try to obtain an abstract view, model or structure of a real life problem, and reduce its
unnecessary details. With definition of properties of problems, including the data which are
affected and the operations which are identified, the model abstracted from problems can be
a standard solution to this type of problems. It is an efficient way since there are nebulous
reablife problems that have similar properties.
In simple terms, it is hiding the unnecessary details & showing only the essential
parts/functionalities to the user.
Data binding : Data binding is a process of binding the application Ul and business logic. Any
change made in the business logic will reflect directly to the application UI
Abstraction is achieved in 2 ways
- Abstract class
Interfaces (Pure Abstraction)
1, Abstract Class
@ An abstract class must be declared with an abstract keyword.
@ It can have abstract and non-abstract methods.
@ It cannot be instantiated.
@ It can have constructors and static methods also.
@ It can have final methods which will force the subclass not to change the body of the
method.
system. out This animal breathes «< OOPS in Java
System out prInInT Wow, you Have
void walk
Syston
class Chicksa extends) Asiana
[Link](*Wow, you have
fen walks on 2 legs
public static void nain(Steing a
Horse ho
2. Interfaces
@ All the fields in interfaces are public, static and final by default.
@ All methods are public & abstract by default.
@ A class that implements an interface must implement all the methods declared in the
interface.
@ Interfaces support the functionality of multiple inheritance.
face Animal
vosd walk
class Horse implements Aninal< OOPS in Java
clase Chicken ixplenente Animal
public void =
System. out .printl
public class OOPS
public static void main(String arg
Static Keyword
Static can be:
1. Variable (also known as a class variable)
2. Method (also known as a class method)
3. Block
4. Nested class
class Student [
static String echo
String nane
public class OOPS
public static void main(String ares
\[Link] = ‘JMV< OOPS in Java
[Link] = "Meena
[Link] = *Beena*
System. out .printin(e1, school)
Systen. out .printIn([Link])