Lecture 4: OOPS Concept
Mr. Tarus
Outline
Basic Object Oriented Concepts in Java.
Types of variables/attributes/fields in a class.
Ways to initialize an object.
Case program example to demonstrate more than one program executing
as one program.
Access Modifiers
3. Basic Object Oriented Concepts in Java
Outline
1. Classes
2. Objects
class
Template / blueprint that describes the data and behavior associated with the class
instantiation.
logical template to create the objects that share common properties and methods.
group of objects which have common properties.
It is a logical entity.
Think of the class as a sketch (prototype) of a house. It contains all the details
about the floors, doors, windows, etc. Based on these descriptions we build the
house. House is the object.
Since many houses can be made from the same description, we can create many
objects from a class.
A class in Java can contain:
Fields
Methods
Constructors
Blocks
Nested class and interface
Syntax:
class <class_name>{
field;
method;
}
//java class example void sleeping() {
public class Dog1 { [Link]("Dog is Sleeping\
String breed; n");
int age; }
String color; public static void main(String[] args)
void barking() { {
[Link]("Dog is barking\n"); Dog1 d = new Dog1();
} [Link]();
void hungry() { [Link]();
[Link]("Dog is Hungry\n"); [Link]();
} }
}
[Link]
Types of variables/attributes/fields in a class
Local variables:
Variables defined inside methods, constructors or blocks.
Declared and initialized within the method and the variable will be destroyed
when the method has completed.
Instance variables:
Variables within a class but outside any method.
Initialized when the class is instantiated.
Can be accessed from inside any method, constructor or blocks of that particular
class.
Class variables / static variables:
Variables declared within a class, outside any method, with the static keyword.
Variable can be shared by all class instances
[Link]
//Types of variables in java
class VariableType {
static int m=100; //static variable
void method()
{
int n=90; //local variable
[Link]("Local variable = " +n);
}
public static void main(String args[]) {
VariableType vt = new VariableType();
[Link]();
int data=50; //instance variable
[Link]("inatance variable = " +data);
}
object
Run time entity in oop.
An object is a real-world entity.
The object is an entity which has state and behavior.
The object is an instance of a class.
new keyword is used to create new objects.
Characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit,
withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value
of the ID is not visible to the external user. However, it is used internally by the
JVM to identify each object uniquely.
Three steps when creating an object from a class.
Declaration:
− An object declaration using the class name and valid variable name.
Instantiation:
− The 'new' keyword is used to create the object.
Initialization:
− The 'new' keyword is followed by a call to a constructor. This call
initializes the new object.
Pto…
//Java Program to demonstrate having the main method in another class
//Creating Student class.
class Student{
int id;
String name;
}
//Creating another class TestStudent1 which contains the main method
class TestStudent1{
public static void main(String args[]){
Student s1=new Student();
[Link]([Link]);
[Link]([Link]);
}
Ways to initialize an object
NB: Initializing an object means storing data into the object.
1. By reference variable
2. By method
3. By constructor
Initialization through reference variable. [Link]
class Student5{
public int rollno;
String name;
}
public class StudentRef1{
public static void main(String args[]){
Student5 s1 = new Student5();
[Link] = 101;
[Link] = "last Boy";
[Link]([Link]+" " +[Link]);
}
}
//object creating and passing values (By Method). [Link]
public class Puppy {
public Pupp4(String name, int age, String color) {
[Link]("Puppy Name is :" + name );
[Link]("Puppy age is :" + age );
[Link]("Puppy colour is :" + color );
}
public static void main(String []args) {
// create an object myPuppy
Puppy myPuppy = new Puppy( "tommy",23, "Black");
//myPuppy.Pupp4("tommy",23, "Black“);
}
Example [Link]
Create a 3 programs that run (being managed by one main method).
Classes:
Animalia (main class)
Frog
Lizard
Tortoise
Access Modifiers
Helps to restrict the scope of a class, constructor, variable, method, or data
member.
It provides security, accessibility, etc. to the user depending upon the access
modifier used with the element.
Types of Access Modifiers
There are 4 types of access modifiers available in Java:
Default – No keyword required
Private
Protected
Public
Default Access Modifier, [Link]
When no access modifier is specified for a class, method, or data member, it is
said to be having the default access modifier by default.
The default access modifiers are accessible only within the same package.
Private Access Modifier
Specified using the keyword private.
The methods or data members declared as private are accessible only within the
class in which they are declared.
Any other class of the same package will not be able to access these members.
Top-level classes or interfaces can not be declared as private because,
private means “only visible within the enclosing class“.
protected means “only visible within the enclosing class and any
subclasses“.
Protected Access Modifier
Specified using the keyword protected.
The methods or data members declared as protected are accessible within the
same package or subclasses in different packages.
Public Access Modifier
• Nb: Default package and test package
• The public access modifier is specified using the keyword public.
• The public access modifier has the widest scope among all other access
modifiers.
• Classes, methods, or data members that are declared as public are accessible
from everywhere in the program. There is no restriction on the scope of public
data members.