Introductions to OOPs By D.Nagasree
Oops Concepts Class/Object Encapsulation
Abstraction Inheritance Polymorphism
Class/object Class A class can be defined as a
template/blueprint that describes the behavior/state that the object of its type support Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking,
eating. An object is an instance of a class. We know a class is a model of creation objects . This means the properties and actions of the objects are written in the class. Properties are represented by variables and actions of the objects are represented by methods. Syntax: [modifier] class ClassName { (variables, methods, constructors....) }
Example:
class Person { String name; //properties----variables int age; void talk() {} //actions -----methods
Void walk() {} }
class Person {String name; //instance variables int age; void talk() { System.out.println(“Hello I am”+name); System.out.println(“my age is”+age); } } Person sree=new Person(); Classname objectname=new Classname(); The object reference (hash code) internally represents heap memory where instance variables are stored. There would be pointer (memory address) from heap memory to a special structure located to method area. I n method area, a table is available which contains pointers to
static variables and methods. This is how the instance variables and methods are organized by JVM internally at run time
To Display hash code of object import java.lang.*; class Person {String name; int age; void talk() {System.out.println("Hello I am "+name); System.out.println("my age is"+age);
}}
Class SecondClass {public static void main(String args[]) { Person sree=new object Person(); System.out.println(“sree
contains"+sree.hashCode()); }} import java.lang.*; class Person {String name; int age; void talk() {System.out.println("Hello I am "+name); System.out.println("my age is"+age); }} class SecondClass {public static void main(String args[]) { Person sree=new Person(); sree.talk();//call the talk() method }}
E: Inagasreeloops/j avac SecondClass.java AL
E: \nagasreeloops) java SecondClass Hello Iam:null My age is:0
Default values of the
instance variables as used by Java compiler Data type Default value byte 0 short 0 int 0 long 0 float 0 double 0 char A space String null boolean false
Initialization of Instance variables import java.lang.*; class Person {String name; int age; void talk() {System.out.println("Hello I am "+name); System.out.println("my age is"+age); }} class SecondClass {public static void main(String
args[]) { Person sree=new Person(); sree.name=“Nagasree"; sree.age=26; sree.talk(); }} import java.lang.*; class Person {String
age=24; void talk() name=“sirisha”; int
{System.out.println("Hello I am "+name); System.out.println("my age is"+age); }} class SecondClass {public static void main(String args[]) { Person sree=new Person(); sree.name=“nagasree"; sree.age=26; sree.talk(); }}
Protection for data is always needed.
All companies are worried about security of data, which is compulsory. Eg: Just think, in an organization’s database , an employee salary is stored as 12,500.00 and is modified as 1,25,000.00. This is sensitive data and should not be modified by any external source. So we can use private a ccess specifier to protect data
import java.lang.*; class Person {private String
int age=26; void talk() name=“nagasree"; private {System.out.println("Hello I am "+name); System.out.println("my age is"+age); }} class SecondClass {public static void main(String args[]) { Person sree=new Person(); sree.talk(); }} import java.lang.*; class Person {private String
int age=26; void talk() name=“nagasree"; private {System.out.println("Hello I am "+name); System.out.println("my age is"+age); }} class SecondClass {public static void main(String naga=new args[]) { Person sree=new Person(); Person Person(); sree.talk(); naga.talk(); }}
Access Specifiers: An access specifier is a key word that
specifies how to access the members of a class or a class itself. We can use access specifiers before a class and its members. There are four Access specifiers in Java: public: public members of a class are accessible every where outside the class. So any other program can read them and use
private members of a class them. private: are not accessible any where outside the class. They are accessible only with in the class by the methods of that class. protected: protected members of a class are accessible
out side the class, but generally with in same
default is used by compiler directory default: when no access specifier is written by the user
Constructor A constructor is a block of code which
is similar to a method that is used to initialize the object and instance variables. Rules: The constructor’ s name and class name should be same. And the name constructor name end with braces . No return type and cannot be static, abstract, final &synchronized. Eg: Person()// default constructor {} Eg:- Person(String s, int
i)//parameterized constructor {} Constructor is called at every time an
object is created using ‘new’ keyword, at least once. Person sree=new Person();//here default constructor is called Person sree=new Preson(“nagasree”,25); // here parameterized constructor is called A constructor is automatically called and executed at time of creating an object. If some values are passed to the object, then the parameterized constructor is called. A constructor is called and executed only once per object. This means when we
create an object, the constructor is called. When we create second object, again the constructor is called second time. import java.lang.*; class Student {int age; String name; Student() {System.out.println("Hello I am a constructor”); }} class SecondClass {public static void main(String args[]) { Student s1=new Student(); } } import java.lang.*; class Person { private String name; private int age; Person(){name=“nagasree"; age=26; }
Person (String s, int i){ name=s; age=i; } void talk(){ System.out.println("Hello I am "+name); System.out.println("my age is"+age); } }
class SecondClass {public static void main(String args[]) { Person p1=new Person(); p1.talk(); Person p2=new Person(“sai",24); p2.talk(); }}
Write a Java Program to Accept
name and age and display if he is young or old(>60)
Methods A method represents a group of
statements that perform a task. It may calculate the data and return the result A methods has two parts: Method header or Method prototype Method
body
Method body Eg: {
statements; }{
double c=a+b; System.out.println(“sum is = ”+c); }
Examples for creation and calling of method Eg :double sqrt(double num) {}returndatatype methodname(parameter1 , paprameter2, ..)
If a method returns some values, then
a return statement should be written within the body of the method, as: return x; return (x+y); return -1; return obj; return arr; Eg: { double c=a+b; return c; }
Note:
A method can never return more than
one value. Ex: return x,y; //returning 2 values return x; return y; //2 executable return statements A method without parameters and without return type A method without parameters and with return type A method with parameters without return type. A method with parameters and with return type.
Method without parameters and without return type import java.lang.*; class Sample {private double
x, double y) {num1=x; num1,num2; Sample(double num2=y; } void sum() { double r=num1+num2; System.out.println("sum is ="+r); }} class Methods {public static void main(String args[]) {Sample s=new Sample(10,22.5); s.sum(); }}
Method without parameters and with return type import java.lang.*; class Sample {private double
x, double y) {num1=x; num1,num2; Sample(double num2=y; } double sum() { double r=num1+num2; System.out.println("sum is ="+r); return r; }} class Methods {public static void main(String args[]) {Sample s=new Sample(10,22.5); s.sum(); }}
Method with parameters and return type import java.lang.*; class Sample {double sum(double r; num1,double num2) { double r=num1+num2; return }} class Methods {public static void main(String args[]) {Sample s=new Sample(); double x=s.sum(10,22.5); System.out.println("sum is ="+x); }}
import java.lang.*; class Sample {int num1=1,num2=5;
//without parameters without return type void sum() {double r=num1+num2; System.out.println("sum is ="+r); }//with parameter without return type int mul(int a) { System.out.println(a*a); }//without parameter with return type int display(){System.out.println("hello"); return 7; } //with parameters with return type int add(int a,int b) { int r=a+b; System.out.println(r); return r; }}class Methods1 {public static void main(String args[]) { Sample s=new s.mul(5); s.display(); s.add(4,5); }} Sample(); s.sum();
Method overloading
Defining two or more methods with
same name within a class.They must differ in number or types of parameters This is called as method overloading.
Example for Method Overloading: class Sample {void add(int a,int b) {System.out.println("sum of two="+(a+b)); } void add(int a,int b,int c) { System.out.println("sum of three="+(a+b+c)); } }class Methods { public static void main(String args[]) { Sample s=new Sample();
s.add(10,20); s.add(10,500,90); }}
Static Method Static methods are the methods which do not act upon
instance variables of a class. static methods are declared as ‘static’ Classname.methodname();
The reason why static methods can
not act on instance variables is that the
JVM first executes the static methods and then only it creates objects. Since objects are not available at the time of calling the static methods, the instance variables are also not available. class Sample {static d ouble sum(double {double r=num1+num2; return r; num1,double num2) }} class Methods {public static void main(String args[ ])
{double x=Sample.sum(10,22.5); System.out.println("sum is ="+x); }}
class Sample { private int x; Sample(int n) { x=n; } static void access() {System.out.println("sum is
="+x); } } class Methods {public static void main(String args[]) {Sample obj=new Sample(10); } } class Sample { static int x=44;
static void access() {System.out.println("sum is ="+x); }} class Methods {public static void main(String args[]) {Sample.access(); } }
Static Block Declare as ‘static’ static{ Statements; } Note: JVM executes a static block on
highest priority basis. Executes before
main() method. class Methods {static { System.out.println("Static
block"); } public static void main(String args[]) { System.out.println("static method"); }} public class Stat2 {static int a; { a=68; System.out.println(“non static block "+a); }static {a=45; System.out.println("static block "+a); }Stat2() {System.out.println("default constructor "+a); }
Program for order of execution of non static ,static, and constructors: Stat2(int i) {System.out.println("parameter constructor "+a); }public static void main(String[]args) { Stat2 t=new Stat2(1111); m=new Stat2(); Stat2 {System.out.println("non static block "+a); }}}
Without main can we execute a program Class Test {static {System.out.println(“static block”); } }//we get an error main not found
This will not work in Java 9 Class Test {static {
System.out.println("static block"); System.exit(0); }}
Local variable
Ex:-private int x; void modify(int a) {x=a; } class Sample {private int x; void modify(int a) {x=a; }void access() { System.out.println("x= "+x); System.out.println("a= "+a); }} class Methods {public static void main(String args[]) {
Sample s=new Sample(); s.modify(10); s.access(); }}
Ex:- class Sample {private int x; void modify(int x) {x=x;//here both x refers only the local variable } } If we have the same name for instance variable and local variable, i.e., x:-it denotes by default the
local variable only. How to refer the instance variable in the above case? This is done by using‘this’keyword.
‘this’ keyword: It refers to the object of the class
where it is used. ‘this’ is used to avoid name conflicts. When an object is created to a class, a default reference is also created internally to the object. That default reference is ‘this’ . So ‘this’ can refer to all the things of present object. instance variables this constructors methods
Ex: Private int x; void modify(int x) {this.x=x; //store local variable x into
present class instance variable x. }Here
‘this.x’ refers to the present class instance variable and just ‘x’ refers to the local variable. class Sample {private int x; void modify(int x) {this.x=x; } void access() { System.out.println("x= "+x); }} class Methods {public static void main(String args[]) { Sample s=new Sample(); s.modify(10); s.access(); }}
Using ‘this’ for chaining of constructors • Constructor chaining i s the process
of calling one constructor in another
constructor using this() , when two or more constructors belong to the same object or class • Constructor call must be the first statement in a Constructor This is called explicit constructor invocation. class Student {int rollno; String name; Student() {this(11); System.out.println("iam a default constructor"); }Student(int rno) { this(11,"ram"); System.out.println("iam a single parameterised constructor"); } Student(int rollno,String name)
{System.out.println("iam a parameterised constructor with two parameters"); this.rollno=rollno; this.name=name; }void stuDetails() {System.out.println(rollno); System.out.println(name); }public static void main(String args[]) { Student }} stu=new Student(); stu.stuDetails();
C:\Users\Nagasree\Desktop/javac Student.java C:\Users\Nagasree\Desktop/java Student iam a parameterised constructorwith two parameters iam a single parameterised constructor iam a default constructor ram
Instance Methods Instance methods are the methods
which act upon instance variables. We call them as objectname.methodname(). Note: Instance methods can access not only instance variables but also static variables directly. 2types of instance methods: Accessor
methods Mutator methods