CSE Module 1
CSE Module 1
Object-Oriented Programming
• In this model, Program is divided into small parts called Objects.
• It follows bottom up approach.
• It have access specifiers like Public, Private and Protected.
• Adding new data and functions is easy.
• It is more secure
• Overloading is possible in this paradigm.
• In this model, data is more important than function.
• Ex: C++, Java, Python etc.
• Abstraction
• It is a process of hiding implementation details and exposes only the
functionality to the user.
• In abstraction, we deal with ideas and not events.
• This means the user will only know “what it does” rather than “how it
does”.
Real-Life Example- A driver will focus on the car functionality (Start or
Stop, Accelerate or Break), he or she does not bother about how the
Accelerate or brake mechanism works internally. And this is how the
abstraction works.
• Encapsulation is the process of wrapping code and data together
into a single unit.
Real-Life Example: A capsule which is mixed of several medicines. The
medicines are hidden data to the end user.
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
AprilVIT-AP
12, 2022University
7 / 85
Four main Pillars of OOPs
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
}
s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}
p u b l i c c l a s s Test {
p u b l i c v o i d main ( S t r i n g [ ] a r g s ) {
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}
void
• Java programming mandates that every method provide the return
type.
• ava main method doesn’t return anything, that’s why it’s return type
is void.
p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
return 0;
}
}
main
• This is the name of java main method.
• It’s fixed and when we start a java program, it looks for the main
method.
p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d mymain ( S t r i n g [ ] a r g s )
{
System . o u t . p r i n t l n ( ” H e l l o World ” ) ;
}
}
p u b l i c c l a s s Test {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
f o r ( S t r i n g s : a r g s ){
System . o u t . p r i n t l n ( s ) ;
}
}
}
You can also declare a variable without assigning the value, and
assign the value later:
i n t myNum ;
myNum = 1 5 ;
System . o u t . p r i n t l n (myNum ) ;
Note that if you assign a new value to an existing variable, it will
overwrite the previous value:
i n t myNum = 1 5 ;
myNum = 2 0 ; // myNum i s now 20
System . o u t . p r i n t l n (myNum)
Arithmetic Operators
Name Description
Addition(+) Adds together two values
Subtraction(-) Subtracts one value from another
Multiplication(*) Multiplies two values
Division(/) Divides one value by another
Modulus Returns the division remainder
Increment(++) Increases the value of a variable by 1
Decrement(- -) Decreases the value of a variable by 1
Assignment Operators
Operator Example
= x=5
+= x+=3
-= x-=3
*= x*=3
/= x/=3
Example
i n t x = 10;
x += 5 ;
Comparison Operators
Operator Example
== x == y
!= x != y
> x>y
< x <y
>= x> = y
<= x<=y
Name Description
Logical and Returns true if both statements are true
Logical OR Returns true if one of the statements is true
Logical NOT Reverse the result, returns false if the result is true
x < 5 || x < 4
Everything in Java is associated with classes and objects, along with its
attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
create a class
p u b l i c c l a s s Main {
int x = 5;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
36 / 85
Multiple Objects
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj1 = new Main ( ) ; // O b j e c t 1
Main myObj2 = new Main ( ) ; // O b j e c t 2
System . o u t . p r i n t l n ( myObj1 . x ) ;
System . o u t . p r i n t l n ( myObj2 . x ) ;
}
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
System . o u t . p r i n t l n ( myObj . x ) ;
} Mangalampalli Assistant Professor,
Dr. S Sudheer Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
39 / 85
Modify attributes
Example
p u b l i c c l a s s Main {
i n t x = 10;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
myObj . x = 2 5 ; // x i s now 25
System . o u t . p r i n t l n ( myObj . x ) ;
}
}
If you don’t want the ability to override existing values, declare the
attribute as final
Example
p u b l i c c l a s s Main {
f i n a l i n t x = 10;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myObj = new Main ( ) ;
myObj . x = 2 5 ; // w i l l g e n e r a t e an e r r o r : c a n n o t a s
System . o u t . p r i n t l n ( myObj . x ) ;
}
}
p u b l i c c l a s s Main {
public void f u l l T h r o t t l e () {
System . o u t . p r i n t l n ( ” The c a r i s g o i n g f a s t ” ) ;
}
p u b l i c v o i d s p e e d ( i n t maxSpeed ) {
System . o u t . p r i n t l n ( ” Max s p e e d i s : ” + maxSpeed ) ;
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Main myCar = new Main ( ) ;
myCar . f u l l T h r o t t l e ( ) ;
myCar . s p e e d ( 2 0 0 ) ;
}
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
F i e l d S h a d o w i n g E x a m p l e ob = new F i e l d S h a d o w i n g E x a m p l e ( )
ob . d i s p l a y ( ) ;
}
}
c l a s s Encapsulate {
p r i v a t e S t r i n g geekName ;
private int geekRoll ;
p r i v a t e i n t geekAge ;
p u b l i c i n t getAge ( )
{
r e t u r n geekAge ;
}
p u b l i c S t r i n g getName ( )
{
r e t u r n geekName ;
}
import java . io . ∗ ;
import java . lang . ∗ ;
import java . u t i l . ∗ ;
c l a s s one {
public void print geek ()
{
System . o u t . p r i n t l n ( ” S a i ” ) ;
}
}
c l a s s two e x t e n d s one {
public void p r i n t f o r () {
System . o u t . p r i n t l n ( ” f o r ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
68 / 85
Example for Single Inheritance
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
two g = new two ( ) ;
g . print geek ();
g. print for ();
g . print geek ();
}
}
import java . io . ∗ ;
import java . lang . ∗ ;
import java . u t i l . ∗ ;
c l a s s one {
public void print geek ()
{
System . o u t . p r i n t l n ( ” S u d h e e r ” ) ;
}
}
c l a s s two e x t e n d s one {
public void p r i n t f o r () {
System . o u t . p r i n t l n ( ” f o r ” ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
71 / 85
c l a s s t h r e e e x t e n d s two {
public void print geek ()
{
System . o u t . p r i n t l n ( ” S u n e e t h a ” ) ;
}
}
p u b l i c c l a s s Main {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
t h r e e g = new t h r e e ( ) ;
g . print geek ();
g. print for ();
g . print geek ();
}
}
class A {
public void print A ()
{
System . o u t . p r i n t l n ( ” C l a s s A ” ) ;
}
}
c l a s s B extends A {
public void print B ()
{
System . o u t . p r i n t l n ( ” C l a s s B ” ) ;
}
}
class Vehicle
{
i n t maxSpeed = 1 2 0 ;
}
c l a s s Car e x t e n d s V e h i c l e
{
i n t maxSpeed = 1 8 0 ;
void display ()
{
System . o u t . p r i n t l n ( ” Maximum Speed : ” + s u p e r . maxSpeed ) ;
}
}
c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
Car s m a l l = new Car ( ) ;
small . display ();
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
78 / 85
Super Contd..
Use of super with methods: This is used when we want to call parent
class method. So whenever a parent and child class have same named
methods then to resolve ambiguity we use super keyword.
c l a s s Person
{
v o i d message ( ) {
System . o u t . p r i n t l n ( ” T h i s i s p e r s o n c l a s s ” ) ;
}}
c l a s s Student extends Person {
v o i d message ( )
{
System . o u t . p r i n t l n ( ” T h i s i s s t u d e n t c l a s s ” ) ;
}
void display ()
{
message ( ) ;
s u p e r . message ( ) ;
}
}
c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] )
{
S t u d e n t s = new S t u d e n t ( ) ;
s . display ();
}
}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
80 / 85
Super contd..
Use of super with constructors: super keyword can also be used to
access the parent class constructor.
c l a s s Person
{
Person (){
System . o u t . p r i n t l n ( ” P e r s o n c l a s s C o n s t r u c t o r ” ) ;
}}
c l a s s Student extends Person
{
Student ()
{
super ( ) ;
System . o u t . p r i n t l n ( ” S t u d e n t c l a s s C o n s t r u c t o r ” ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor,
Object-Oriented
Senior Grade1
Programming
School of Fundamentals
Computer Science and Engineering
April 12,
VIT-AP
2022 University
81 / 85
super contd..
c l a s s Test
{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
S t u d e n t s = new S t u d e n t ( ) ;
}
}
c l a s s Honda e x t e n d s B i k e {
v o i d r u n ( ) { System . o u t . p r i n t l n ( ” r u n n i n g s a f e l y w i t h
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
Honda honda= new Honda ( ) ;
honda . r u n ( ) ;
}
}
c l a s s Honda1 e x t e n d s B i k e {
v o i d r u n ( ) { System . o u t . p r i n t l n ( ” r u n n i n g s a f e l y w i t h 1
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
Honda1 honda= new Honda1 ( ) ;
honda . r u n ( ) ;
}
}