Color Print1
Color Print1
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;
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(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.
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);
}
System.out.println((1/2)*b*h);
}
4. Hybrid inheritance : Hybrid inheritance is a combination of
simple, multiple inheritance and hierarchical inheritance.
}
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[]) {
a1.setPassword("abcd");
import java.util.Scanner; a1.email = "[email protected]";
import java.io.IOException; }
➢ 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 {
We try to obtain an abstract view, model or structure of a real life problem, and }
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) }
}
● It can have final methods which will force the subclass not to change the body of
the method.
interface Animal {
}
void walk();
}
3. Block
}
4. Nested class
class Student {
class Chicken implements Animal {
String name;
System.out.println("Chicken walks on 2 legs");
}
}
s1.name = "Meena";
s2.name = "Beena";
System.out.println(s1.school);
System.out.println(s2.school);
}
}