This document summarizes key concepts in object-oriented programming (OOP) in Java, including static keyword, instance member, class member, inheritance, encapsulation, polymorphism, object, class, constructor, and more. It provides examples to illustrate static variables and methods, the this keyword, super keyword, final keyword, abstract classes vs interfaces, and method overloading as a form of compile-time polymorphism.
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Static Keyword and OOPs Concepts
1. Static Keyword:-
By default all the member of class are instance member. To
associate with it class member we used static key word
Instance member- its represents attribute and behavior of object
Class member- it represents attribute and behavior of class.
Simple def-
we can call attributes & methods of a class without
creating objects.
attributes = variables(int a) + Object variable (C1 o; )
methods = Constructor + functions + Distructors in C++
static data members not duplicate for each Object,
act like a global variable.
for declaring global variables in C & C++, we have
concept of pre processor or declare variable outside
the main method
but in java we have only static declaration for global
variables.
static data members can access with class name, object
& direct also but non static members only access
with objects.
* static can be variables, methods, objects, class
& can be block also.
* any concept use with class name that is static.
static variable:- static variable will have a single copy per
class
classname.var - static variable
ex-
Font.BOLD;
static method:- when u want to call a method using class name
instead of using object of class
classname.methodname() - static method
Integer.parseInt()
static object
classname.objectname - static object
System.out
2. static block: whenever u want to execute a code before execution
of main method u can write a code using static block
static {} - static block.
Qus- how to display welcome msd without using psvm-
class Hello
{
static
{
System.out.println("Ha Ha Ha Ha He He He ");
System.exit(0);
}
}
static class -
Inner class can be static, if inner class is static,
then we can create the inner class object in Outer
class p s v m without using Outer class object - direct.
*** outer class can't be static.
public class Sta2
{
static class Inner
{
void disp()
{
System.out.println("I m inside inner class disp");
}
}
public static void main(String[] args)
{
Inner o = new Inner();
o.disp();
}
}
KeyWord
/*
final keyword - final that means final - fixed - can't changed.
like const in C & C++.
final can be variables, methods & can be class also.
=> if variable is final then can't reassign any value.
=> if method is final then can't override in Child class.
=> if class is final then can't extends - can't inherit.
- final variable example.
final String dsn = "jdbc:odbc:erp"; - now U can't change it */
class F1
{
3. final int a = 1000;
void disp()
{
// a = 10; // error - can't assign any value to a final var a
System.out.println("a = "+a);
}
public static void main(String[] args)
{
F1 o = new F1();
o.disp();
}
}
/*
super keyword - it is use for calling the super class variables ,
methods & Constructors.
super means one step up - just inherited class
- super class variable calling
*/
class S
{
int a = 1000;
}
class S1 extends S // class S1 inherit the prop of class S
{
int a = 100;
void disp()
{
int a = 10;
System.out.println("a = "+a); // local a - 10
System.out.println("a = "+this.a); // currentobject.a -
global a - 100
System.out.println("a = "+super.a); // super class a
calling - 1000
}
public static void main(String[] args)
{
S1 o = new S1();
o.disp();
}}
this operator - it's return the ref of current object. like this
pointer
in C++. it is for -
* where we want to refer the current object.
=>* which is the object variable & which is the argument/local
variable then
we can use this operator.
* to call the current class Constructor(one Constructor to another
Constructor
calling without using new Operator), then we can use this
operator.
4. (imp. for interview) , concept only supported by - Java , VC++ &
C#.
*/
class Th2
{
int a = 100 ;
void disp()
{
int a = 10;
System.out.printf("a = %d n",a); // local - a - 10
System.out.printf("a = %d n",this.a); // currentobject.a ,
global a - 100
}
public static void main(String aa[])
{
Th2 o = new Th2();
o.disp(); // a = 100
}
}
super keyword - it is use for calling the super class variables ,
methods & Constructors.
super means one step up - just inherited class
- super class variable calling
*/
class S
{
int a = 1000;
}
class S1 extends S // class S1 inherit the prop of class S
{
int a = 100;
void disp()
{
int a = 10;
System.out.println("a = "+a); // local a - 10
System.out.println("a = "+this.a); // currentobject.a -
global a - 100
System.out.println("a = "+super.a); // super class a
calling - 1000
}
public static void main(String[] args)
{
S1 o = new S1();
o.disp();
}}
5.
6. OPPS Concept:-
Object
Class
Encapsulation
Inheritance
Abstraction
Polymorphism
All pre define type are object
All user define type are object
Qus= Why java is not 100% OOPS
Ans:-1-everything in java is not consider as a Object. There are
primitive data type such as int , char , Booleans are not object
2- all feature of oops language is not fully supported by java Ex:-
Multiple inheritance
Object - it is a kind of variable that can hold the ref of a class.
Object is the direct ref.
C1 o; - in C++
C1 o = new C1(); - in JAVA
o.disp(); - C1 class disp.
Class:- it is collection of object that share similar attribute, operators or
relasionship Class can exist without an object but reverse can not
Constructor:- is the method, used for initializing the attribute of class.
Attribute= variables + object variable
Constructor has some rules & prop -
rules -
=> Constructor name & class name must be same.
7. => Constructor no return any value that means not void.
prop -
=> every class has one Constructor.
=> if user does not define any Constructor in a class then default
Constructor created.
=> if class has any own Constructor then default Constructor is not
created.
=> if one class has more then one Constructor then that is called Constructor
overloading.
Incapsulation:- Encapsulation is the technique of making the fields in a class
private and providing access to the fields via public methods. If a field is declared
private, it cannot be accessed by anyone outside the class, thereby hiding the fields
within the class. For this reason, encapsulation is also referred to as data hiding
The main benefit of encapsulation is the ability to modify our implemented code
without breaking the code of others who use our code. With this feature
Encapsulation gives maintainability, flexibility and extensibility to our code.
/* EncapTest.java */
public class EncapTest{
private String name;
private String idNum;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getIdNum(){
return idNum;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
8. }
public void setIdNum( String newId){
idNum = newId;
}
}
/////////////
/RunEncap.java */
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName()+
" Age : "+ encap.getAge());
}
}
9. Inheritance:- is the process by which object of one class acquire the properties of
object of another class, a class that is inherited is called a super class and the class
that dose the inheriting is called subclass. It is done by using keyword extends
two most common reason reasons to use inheritance are
1- To promote code reuse
2- To use polymorphism
class A {
void disp(){
System.out.println(" i am inside A display");
}}
class B extends A {
/* void disp(){
System.out.println(" i am inside B display");
}
*/
public static void main(String[] args) {
B o =new B();
o.disp();
} }
10. // Overriding:- if parent and child class has one methods with same name & and
same sign( argument and return type) that is called method overriding.
class types in java –
concrete class abstract class
* only concrete methods * concrete + abstract methods.
* can create object & inherit * can't create instance, inherit only.
abstract class - if one class has one more abstract methods then that
is called an abstract class.
Q- we can create an abstract class without any abstract methods or not?.
ans - yes , just declare as abstract. but can't create the direct instance.
adv - abstract classes used for memory management, memory for methods
provided by the abstract class.
11. condition -
* all abstract methods of an abstract class must be override in child
class else child class is also a kind of abstract class.
future - abstract classes in used when need hierarchy of class like tree structure
Limitation:- only single inheritance example:- one class can extends only one
abstract class
For multiple inheritance we use interface
interface - it is a kind of pure abstract class.
// to create an simple abstract class.
abstract class Calc
{
void sum(int a,int b)
{
System.out.println("Sum is = "+(a+b));
}
abstract void sub(int a,int b);
}
save with Calc.java
12. //class inherit with an abstract class
class MyCalc extends Calc
{
void sub(int a,int b) // method of Calc
{ System.out.println("Sub is = "+(a-b)); }
public static void main(String aa[]) {
MyCalc o = new MyCalc();
o.sum(100,20);
o.sub(100,20);
} }
Diffrence b/w abstract and interface:-
abstract class Interface
concrete + abstract methods. only absract methods
using abstract classes java * using
interfaces java support
support only single inheritance
using interfaces java support
multiple inheritance also.
Constructor – Yes Constructor – no
* all attributes default - normal
Ex:- int a = 10
all attributes default - final
& static. Ex:- final static int a = 10;
default access modifier – default default is public.
13. extends keyword. implements keyword.
class to class - extends
interface to interface - extends
interface to class - implements.
Interface:- interface is collection of implicit abstract methods and static final
data member
Adv:- interface are used for memory management ,
Prop:- support multiple inheritance
Conditions:- all interface methods must be override in child class else child class
is a kind of abstract class .
default access modifier in interface is public
variable declare must be initialize
ex:- int a; - error;
int a = 10; valid
// interface can't have
interface Prob
14. {
// void disp(){} // error
// Prob(){} // error
// int a; // error
// private void disp(); // error
// protected void disp(); // error
// final void disp(); // error
// default in interface
abstract void disp();
public abstract void disp1();
final int a = 10;
static final int b = 10;
public final int v = 10;
public static final int c = 10;
static int d = 10;
}
/////////////////
interface Calc
{
void sum(int a,int b);
void sub(int a,int b);
}
////
interface Calc1
{
void multi(int a,int b);
}
// multiple inheritance with interface
class MyCalc1 implements Calc,Calc1
{
public void sum(int a,int b) // method of Calc
{
System.out.println("Sum is = "+(a+b));
}
public void sub(int a,int b) // method of Calc
{
System.out.println("Sub is = "+(a-b));
}
public void multi(int a,int b) // method of Calc1
{
System.out.println("Multi is = "+(a*b));
}
public static void main(String aa[])
{
MyCalc1 o = new MyCalc1();
o.sum(100,20);
o.sub(100,20);
o.multi(100,20);
}
}
15. Polymorphism: can be implemented in java language in the form of
multiple methods having same name
Type:-
1- Compile Time:- is done using method overloading , its also
called Early binding.
2- Run Time Polymorphism :is done using inheritance. Its also called
Late binding
// user define Constructor with Constructor overloading
class C2
{
int a,b;
C2() // no argument passing Constructor
{
a = b = 0;
}
C2(int x) // one argument passing Constructor
{
a = b = x;
}
C2(int x,int y) // two argument passing Constructor
{
a = x;
b = y;
}
void disp()
{
System.out.printf("a = %d , b = %dn",a,b);
}
public static void main(String aa[])
{
C2 o1 = new C2(); // no argument passing Constructor calling
C2 o2 = new C2(100); // one argument passing Constructor
calling
C2 o3 = new C2(10,20); // two argument passing Constructor
calling
o1.disp(); // a = 0 , b = 0
o2.disp(); // a = 100 , b = 100
o3.disp(); // a = 10 , b = 20
}
}