0% found this document useful (0 votes)
0 views8 pages

Color Print1

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including classes, objects, constructors, polymorphism, inheritance, encapsulation, abstraction, and packages. It explains key OOP principles with examples, such as method overloading, access modifiers, and the use of abstract classes and interfaces. Additionally, it discusses the static keyword and its applications in Java.

Uploaded by

abhihyd500032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views8 pages

Color Print1

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including classes, objects, constructors, polymorphism, inheritance, encapsulation, abstraction, and packages. It explains key OOP principles with examples, such as method overloading, access modifiers, and the use of abstract classes and interfaces. Additionally, it discusses the static keyword and its applications in Java.

Uploaded by

abhihyd500032
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

OBJECT ORIENTED PROGRAMMING SYSTEMS s2.

name = "Shradha";

JAVA
s2.age = 22;
s2.getInfo();
}
}
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 : Example 2:
class Pen {
String color;
Class is a user-defined data type which defines its properties and its
public void printColor() {
functions. Class is the only logical representation of the data. For System.out.println("The color of this Pen is " + this.color);
example, Human being is a class. The body parts of a human being are its }
}
properties, and the actions performed by the body parts are known as
public class OOPS {
functions. The class does not occupy any memory space till the time an public static void main(String args[]) {
object is instantiated. Pen p1 = new Pen();
p1.color = blue;

Pen p2 = new Pen();


Object is a run-time entity. It is an instance of the class. An object can p2.color = black;
represent a person, place or any other item. An object can operate on
Pen p3 = new Pen();
both data members and member functions. p3.color = red;

p1.printColor();
p2.printColor();
Example 1: p3.printColor();
class Student { }
}
String name;
int age;
Note : When an object is created using a new keyword, then space is
public void getInfo() { allocated for the variable in a heap, and the starting address is stored in
System.out.println("The name of this Student is " + this.name); the stack memory.
System.out.println("The age of this Student is " + this.age);
} ‘this’ keyword : ‘this’ keyword in Java that refers to the current
} instance of the class. In OOPS it is used to:
1. pass the current object as a parameter to another
public class OOPS {
method
public static void main(String args[]) { 2. refer to the current class instance variable
Student s1 = new Student();
s1.name = "Aman";
s1.age = 24;
s1.getInfo();

Student s2 = new Student();


Constructor : Constructor is a special method which is invoked object. There is only a user defined copy constructor in Java(C++ has a
automatically at the time of object creation. It is used to initialize the data default one too).
members of new objects generally.
class Student {
● Constructors have the same name as class or structure.
String name;
● Constructors don’t have a return type. (Not even void)
int age;
● Constructors are only called once, at object creation.

Student(Student s2) {
There can be three types of constructors in Java.
this.name = s2.name;
this.age = s2.age;
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.
class Student {
String name;
int age;

Student() {
System.out.println("Constructor called");
} Note : Unlike languages like C++, Java has no Destructor. Instead, Java
} has an efficient garbage collector that deallocates memory
automatically.

2. Parameterized constructor : Constructor which has parameters is called a


parameterized constructor. It is used to provide

different values to distinct objects.


class Student {
String name;
int age;

Student(String name, int age) {


this.name = name;
this.age = age;
}
}

3. Copy Constructor : A Copy constructor is an overloaded


constructor used to declare and initialize an object from another
Polymorphism System.out.println(name);
System.out.println(age);
Polymorphism is the ability to present the same interface for differing }
underlying forms (data types). With polymorphism, each of these classes
}
will have different underlying data. Precisely, Poly means ‘many’ and
morphism means ‘forms’.

Types of Polymorphism IMP Runtime Polymorphism : Runtime polymorphism is also known as dynamic
polymorphism. Function overriding is an example of runtime
1. Compile Time Polymorphism (Static) polymorphism. Function overriding means when the child class contains
2. Runtime Polymorphism (Dynamic) the method which is already present in the parent class. Hence, the child
class overrides the method of the parent class. In case of function
Let’s understand them one by one : overriding, parent and child classes both contain the same function with a
different definition. The call to the function is determined at runtime is
Compile Time Polymorphism : The polymorphism which is implemented at known as runtime polymorphism.
the compile time is known as compile-time polymorphism. Example -
Method Overloading class Shape {
public void area() {
System.out.println("Displays Area of Shape");
Method Overloading : Method 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: class Triangle extends Shape {
public void area(int h, int b) {
1. The type of the parameters passed to the function. System.out.println((1/2)*b*h);
}
2. The number of parameters passed to the function.
}
class Circle extends Shape {
class Student { public void area(int r) {
String name; System.out.println((3.14)*r*r);
int age; }
}
public void displayInfo(String name) {
System.out.println(name);
}

public void displayInfo(int age) {


System.out.println(age);
}
Inheritance
public void displayInfo(String name, int age) {
Inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such a way, you can reuse, }
extend or modify the attributes and behaviors which are defined in other
classes.
3. Multilevel inheritance : Multilevel inheritance is a process of deriving a
In Java, the class which inherits the members of another class is called
class from another derived class.
derived class and the class whose members are inherited is called base class.
class Shape {
The derived class is the specialized class for the base class.
public void area() {
System.out.println("Displays Area of Shape");
Types of Inheritance :
}
1. Single inheritance : When one class inherits another class, it is known
}
as single level inheritance
class Triangle extends Shape {
class Shape {
public void area(int h, int b) {
public void area() {
System.out.println((1/2)*b*h);
System.out.println("Displays Area of Shape");
}
} }
} class EquilateralTriangle extends Triangle {

class Triangle extends Shape { int side;

public void area(int h, int b) { }

System.out.println((1/2)*b*h);

}
4. Hybrid inheritance : Hybrid inheritance is a combination of
simple, multiple inheritance and hierarchical inheritance.
}

2. Hierarchical inheritance : Hierarchical inheritance is defined as the


process of deriving more than one class from a base class.
class Shape {
public void area() {
System.out.println("Displays Area of Shape");
}
}
class Triangle extends Shape {
public void area(int h, int b) {
System.out.println((1/2)*b*h);
}
}
class Circle extends Shape {
public void area(int r) {
System.out.println((3.14)*r*r);
}
Package in Java
}

Package is a group of similar types of classes, interfaces and sub-packages. public class Sample {
Packages can be built-in or user defined. public static void main(String args[]) {

Account a1 = new Account();


Built-in packages - java, util, io etc.
a1.name = "Apna College";

a1.setPassword("abcd");
import java.util.Scanner; a1.email = "[email protected]";

import java.io.IOException; }

Access Modifiers in Java

➢ Private: The access level of a private modifier is only within the class. It cannot Encapsulation
be accessed from outside the class.

➢ Default: The access level of a default modifier is only within the package. It Encapsulation is the process of combining data and functions into a single unit
cannot be accessed from outside the package. If you do not specify any access called class. In Encapsulation, the data is not accessed directly; it is accessed
level, it will be the default. through 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
➢ Protected: The access level of a protected modifier is within the package and manipulate these attributes. Thus, encapsulation makes the concept of data
outside the package through child class. If you do not make the child class, it hiding possible.(Data hiding: a language feature to restrict access to members of
cannot be accessed from outside the package. an object, reducing the negative effect due to dependencies. e.g. "protected",
"private" feature in Java).
➢ Public: The access level of a public modifier is everywhere. It can be accessed
from within the class, outside the class, within the package and outside the
package.

package newpackage;

class Account {

public String name;

protected String email;

private String password;

public void setPassword(String password) {

this.password = password; Abstraction


}
System.out.println("This animal breathes air");

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, Animal() {


including the data which are affected and the operations which are identified, System.out.println("You are about to create an Animal.");
the model abstracted from problems can be a standard solution to this type of }
problems. It is an efficient way since there are nebulous real-life problems that }
have similar properties.
class Horse extends Animal {
In simple terms, it is hiding the unnecessary details & showing only the Horse() {
essential parts/functionalities to the user. System.out.println("Wow, you have created a Horse!");
}
void walk() {
System.out.println("Horse walks on 4 legs");
Data binding : Data binding is a process of binding the application UI and business
}
logic. Any change made in the business logic will reflect directly to the application }

UI.
class Chicken extends Animal {
Chicken() {
System.out.println("Wow, you have created a Chicken!");
}
Abstraction is achieved in 2 ways :
void walk() {
- Abstract class System.out.println("Chicken walks on 2 legs");
- Interfaces (Pure Abstraction) }
}

1. Abstract Class public class OOPS {


public static void main(String args[]) {
● An abstract class must be declared with an abstract keyword.
Horse horse = new Horse();
● It can have abstract and non-abstract methods. horse.walk();
horse.breathe();
● 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.

abstract class Animal {


abstract void walk();
void breathe() { 2. Interfaces
● All the fields in interfaces are public, static and final by default.
public class OOPS {
● All methods are public & abstract by default.
public static void main(String args[]) {
● A class that implements an interface must implement all the methods declared
in the interface. Horse horse = new Horse();

● Interfaces support the functionality of multiple inheritance.


horse.walk();

interface Animal {
}

void walk();
}

class Horse implements Animal {


Static Keyword

public void walk() {


Static can be :

System.out.println("Horse walks on 4 legs"); 1. Variable (also known as a class variable)

} 2. Method (also known as a class method)

3. Block
}

4. Nested class

class Student {
class Chicken implements Animal {

static String school;


public void walk() {

String name;
System.out.println("Chicken walks on 2 legs");

}
}

public class OOPS {


}
public static void main(String args[]) {
Student.school = "JMV";
Student s1 = new Student();
Student s2 = new Student();

s1.name = "Meena";
s2.name = "Beena";

System.out.println(s1.school);
System.out.println(s2.school);
}
}

You might also like