
JAVA OOPS CONCEPTS
Oops concepts
1. Class
2. Object
3. Data member
4. methods
5. Encapsulation
6. Inheritance
7. Polymorphism
8. Abstraction
9. Important keywords
a. Super
b. Static
c. This
class
· Blueprint of object
· It defines the attributes/features and behavior of object
syntax:
class class_name
{
Data members
Member functions/methods
}
example
car
color
mileage
punch
red
78
Honda city
Grey
90
Object:
· Reference/Instance of class
· To access class members, an object is created
· Class when created will not get any memory
· Whenever we create a new object, it gets new memory of class type
· N number of objects can be created for single class
Syntax:
classname object_name = new classname();
Data member:
· Data values used in program
· They are written as variable declarations
Variable:
It is name given to memory location where data value is stored
Syntax for variable declaration:
Datatype variableName = dataValue;
Example
int marks=90;
marks (integer type)
90
Method/member function
Set of statements which when executed will perform some task
Syntax defining method:
Returntype methodName(parameters)
{
statements
}
Invoking/calling method
Objectname.methodname(parametervalues);
ENCAPSULATION
· Combining of data members and related member functions in single unit(class)
· Hiding data and member functions from other classes of program
· Access specifiers can be used to control access to these class members
Example
class Student
{
int Id;
String Name;
Void Display()
{
…..
}
}
INHERITANCE
· Creating a new class by using features of existing class
· Deriving a child class by inheriting features of parent class
· By writing extends keyword followed by parent classname beside the child class, we inherit parent class features into child class
Use: Code reusability
Simple/single inheritance
Class A
{
int id;
}
Class B extends A
{
Void display()
{
S.o.p(id);
}
}
Types of inheritance (supported using classes):
Single inheritance
A
|
B
Multilevel inheritance
A
|
B
|
C
Hierarchical inheritance (tree like structure)
A
|B |C
|X |y |I |j
Types of inheritance(supported using interfaces):
Multiple inheritance
A B
| |
C
Hybrid inheritance
A B
| |
C
| |
D
Polymorphism
Existing in many forms
Poly-many; morphism-forms
Types:
Static/compile time
Overloading
Dynamic /runtime
Overriding
Static/compile time
Method overloading
When in a class, method with same name exists multiple but with different type of parameters
Example:
class A
{
void display()
{
System.out.println("welcome");
}
void display(int a)
{
System.out.println("a "+a);
}
void display(char grade)
{
System.out.println("grade "+grade);
}
}
Method overriding
In inheritance, when child class redefines parent class method its called overriding
Class A
{
Void display()
{
S.o.p(“hi”);
}
}
Class B extends A
{
@Override
Void display()
{
S.o.p(“welcome”);
}
}
Abstraction
Showing necessary details and hiding functionality
Abstraction is achieved in java using
Abstract class
· Class that contains at least one abstract method is abstract class
· Abstract method is a method without definition
· The abstract method is implemented /given definition by child class of abstract class.
· Abstract class cannot be instantiated/cannot have object creation.
· Abstract class must be declared abstract using abstract keyword.
abstract Class Z
{
abstract Void display1(); //abstract method
Void display2() //concrete method
{
S.o.p(“hi”);
}
}
Class A extends Z
{
@Override
void display1()
{
S.o.p(“implemented by A”);
}
}
Interface
Pure abstract class
Interface interface_name
{
Non abstract methods
}
interface Z
{
Void display1(); //abstract method
default Void display2() //concrete method
{
S.o.p(“hi”);
}
}
Class A implements Z
{
@Override
void display1()
{
S.o.p(“implemented by A”);
}
}
IMPORTANT KEYWORDS
super
static
this
super
super KEYWORD USED IN INHERITANCE
· to refer to parent class in child class, super keyword is used.
Class A
{
Int id=100;
}
Class B extends A
{
Int id=200;
Void display()
{
S.o.p(super.id);
S.o.p(id);
}
}
Static:
· Static members get memory only once in program
· The same memory is used throughout the program
· Variables and methods in java can be declared as static
· Static members are not bounded with objects
Static variable
static int a=100;
static method
static void display()
{
}
this
acts as current class object
to refer to class members in same class, this keyword acts as class object.
When the instance variable and local variable have same name, then this keyword is used with class member i.e. to refer to instance member this keyword is used.
class A
{
int id1=100;
void display1()
{
int id1=200;
System.out.println("id1: "+ id1);
System.out.println("id1: "+ this.id1);
}
}
JAVA - OBJECT ORIENTED PROGRAMMING PRINCIPLES COURSE
SPECIALLY DESIGNED AND MADE WITH EFFORTS FOR BEGINNERS
SO, ALL THE BEST AND PLEASE DO JOIN MY COURSE TO GET THE BEST
HELLO EVERYONE
I AM HARSHIKA WITH 12+ YEARS OF TEACHING/TRAINING EXP...
IN THIS COURSE I HAVE TRIED MY BEST TO EXPLAIN JAVA OOPS CONCEPTS IN SIMPLE WAY WITH VERY SIMPLE AND EASY PRACTICAL EXAMPLES
I KEPT THEORY AND PROGRAMS SIMPLE FOR BEGINNERS, SO THAT IT WILL BE EASY FOR THEM TO UNDERSTAND AND REMEMBER CONCEPTS
Oops concepts
1. Class
2. Object
3. Data member
4. methods
5. Encapsulation
6. Inheritance
7. Polymorphism
8. Abstraction
9. Important keywords
a. Super
b. Static
c. This
Java is a popular programming language.
Java is used to develop mobile apps, web apps, desktop apps, games and much more.
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
Mobile applications (Especially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
And much, much more!
Why Use Java?
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
It is one of the most popular programming languages in the world
It has a large demand in the current job market
It is easy to learn and simple to use
It is open-source and free
It is secure, fast and powerful
It has huge community support (tens of millions of developers)
Java is an object-oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs
As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa