OOPS in Java
OOPS in Java
JAVA
Class is a useí-defined data type which defines its píopeíties and its
functions. Class is the only logical íepíesentation of the data. Foí
example, Human being is a class. The body paíts of a human being aíe
its píopeíties, and the actions peífoímed by the body paíts aíe
known as functions. The class does not occupy any memoíy space till
the time an object is instantiated.
Example 1:
class Student {
String name;
int age;
Example 2:
class Pen {
String color;
p1.printColor();
p2.printColor();
p3.printColor();
}
}
Student() {
System.out.println("Constructor called");
}
}
Student(Student s2) {
this.name = s2.name;
this.age = s2.age;
}
}
class Student {
String name;
int age;
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);
}
}
Inheíitance
Inheíitance is a píocess in which one object acquiíes all the píopeíties and
behavioís of its paíent object automatically. In such a way, you can
íeuse, extend oí modify the attíibutes and behavioís which aíe
defined in otheí classes.
In Java, the class which inheíits the membeís of anotheí class is called
deíived class and the class whose membeís aíe inheíited is called base
class. The deíived class is the specialized class foí the base class.
Types of Inheíitance :
1. Single inheíitance : When one class inheíits anotheí class, it is
known as single level inheíitance
class Shape {
System.out.println((1/2)*b*h);
Package in Java
Package is a group of similar types of classes, interfaces and sub-packages.
Packages can be built-in or user defined.
import java.util.Scanner;
import java.io.IOException;
➢ 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. If you do not specify any access level, it will be the
default.
➢ Protected: The access level of a protected modifier is within the package and outside
the package through child class. If you do not make the child class, it cannot be
accessed from outside the package.
➢ 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 {
this.password = password;
}
}
a1.setPassword("abcd");
a1.email = "[email protected]";
Encapsulation
Encapsulation is the process of combining data and functions into a single unit called
class. In Encapsulation, the data is not accessed directly; it is accessed
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 manipulate these
attributes. Thus, encapsulation makes the concept of data hiding possible.(Data hiding: a
language feature to restrict access to members of an object, reducing the negative effect
due to dependencies. e.g. "protected",
"private" feature 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 real-life 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 UI and business logic.
Any change made in the business logic will reflect directly to the application UI.
1. Abstract Class
● It cannot be instantiated.
● It can have final methods which will force the subclass not to change the body of the
method.
2. Interfaces
● All the fields in interfaces are public, static and final by default.
● A class that implements an interface must implement all the methods declared in the
interface.
interface Animal {
void walk();
}
public class OOPS {
horse.walk();
Static Keyword
Static can be :
3. Block
4. Nested class
class Student {
String name;
s1.name = "Meena";
s2.name = "Beena";
System.out.println(s1.school);
System.out.println(s2.school);
}
}