You are on page 1of 27

OOP

Static Variables and Methods


constructors/access Modifiers/Inheritance
Lecture 3
Static Variables
• A static variable is common to all the instances (objects) of the class
because it is a class level variable.
• you can say that only a single copy of static variable is created and
shared among all the instances of the class
• Memory allocation for such variables only happens once when the
class is loaded in the memory.
• Static variables are also known as Class Variables.
• Static variables can be access directly by using class name.
• No need to create any object to access static variables.
Example
class Student{ class Student{
int rollno; int rollno;
String name; String name;
static String college=“ICB";
String college="ICB";
}
}
Static Methods
• If you apply static keyword with any method, it is known as static
method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an object
of a class.
• A static method can access static data member and can change the
value of it.
example
Constructors
• Constructor is a block of code that initializes the newly created object
• A constructor resembles an instance method in java but it’s not a
method as it doesn’t have a return type
• Constructor has same name as the class and have no return type.
How to create?
public class MyClass{

MyClass(){ //This is the constructor


}
}
example
Types of Constructors
Default constructor
• If you do not implement any constructor in your class, Java compiler
inserts a default constructor into your code on your behalf
no-arg constructor:
• Constructor with no arguments is known as no-arg constructor. the
body of the constructor is empty.
Parameterized constructor
• Constructor with arguments(or you can say parameters) is known as Parameterized;

public class Employee {


int empId;
String empName;

Employee(int id, String name){ //parameterized constructor with two parameters

empId = id;
empName = name;
}
Constructor Chaining
• When A constructor calls another constructor of same class then this
is called constructor chaining.
Java Access Modifiers
• An access modifier restricts the access of a class, constructor, data
member and method in another class.
• In java we have four access modifiers:
• 1. default
• 2. private
• 3. protected
• 4. public
1.Default modifier
• When we do not mention any access modifier, it is called default
access modifier.
• The scope of this modifier is limited to the package only
• This means that if we have a class with the default access modifier in
a package, only those classes that are in this package can access this
class.
• No other class outside this package can access this class.
example
package abcpackage; package xyzpackage;
public class Addition { public class Test {
public static void main(String args[]){
Addition obj = new Addition();
int add(int a, int b){
obj.add(10, 21);
return a+b; }
} }
}
2. Private access modifier
• The scope of private modifier is limited to the class only.
• Private Data members and methods are only accessible within the
class
• Class and Interface cannot be declared as private
• If a class has private constructor then you cannot create the object of
that class from outside of the class.
Example
Protected Access Modifier
• Protected data member and method are only accessible by the
classes of the same package and the subclasses.
• You can also say that the protected access modifier is similar to
default access modifier with one exception that it has visibility in sub
classes.
• Classes cannot be declared protected. This access modifier is
generally used in a parent child relationship.
example
Public access modifier
• The members, methods and classes that are declared public can be
accessed from anywhere.
• This modifier doesn’t put any restriction on the access.
Overview
Principles of OOP
1.Inheritance
• The process by which one class acquires the properties(data
members) and functionalities(methods) of another class is called
inheritance.
• The aim of inheritance is to provide the reusability of code so that a
class has to write only the unique features and rest of the common
properties and functionalities can be extended from the another
class.
Child Class and Parent class
• The class that extends the features of another class is known as child
class, sub class or derived class.

Parent Class:
• The class whose properties and functionalities are used(inherited) by
another class is known as parent class, super class or Base class.
Syntax
class XYZ extends ABC
{

}
Example inheritance
IS-A relationship
• Based on the above example we can say that PhysicsTeacher IS-A
Teacher.
• This means that a child class has IS-A relationship with the parent
class.
• This is inheritance is known as IS-A relationship between child and
parent class

You might also like