SlideShare a Scribd company logo
1 of 18
Lab Report:
Object-oriented programming Lab
Arafat Sahin Afridi
PC-A
ID: 211-15-3971
Course Code: CSE 222
Course Title: Object-oriented programming Lab
Department of Computer Science and Engineering
Daffodil International University
Class and Object:
OBJECT:
An object is an identifiable entity with some characteristics, stateand
behaviour. Understanding the concept of objects is much easier when we
consider real-life examples around us because an objectis simply a realworld
entity.
Example
Object: House
State: Address, Color, Area
Behavior: Open door, close door
Class:
A class is a group of objects that share common properties and behavior.
For example, we can consider a car as a class that has characteristics like
steering wheels, seats, brakes, etc. And its behavior is mobility. But we can say
Honda City having a registration number: 4654 is an ‘object’ that belongs to the
class ‘car’.
It was a brief description of objects and classes. Now we will understand the
Java class in detail.
Syntax: Createa class named "Main" with a variablex:
public class Studen{
//fields(or instancevariable)
String StudName;
int StudAge;
// constructor
Studen(String name, int age){
this.StudName=name;
this.StudAge=age;
}
public static void main(String args[]){
//Creating objects
Studenobj1= new Studen("Arafat", 20);
Studenobj2 = new Studen("Abir", 18);
System.out.println(obj1.StudName+" "+obj1.StudAge);
System.out.println(obj2.StudName+" "+obj2.StudAge);
}
}
Getter –Setter Methods
Here “Main” is a Java class.Setter Method & Getter Method:
Encapsulation provide public get and set methods to access and update the
value of a Private variable
We learned that, private variables can only be accessed within the same
class and outside class has no access to it. However, it is possible to access
them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the
value.
Syntax for both is that they start with either get or set, followed by the name
of the variable, with the first letter in upper case:
Example:
classEmployee
{
// classmember variable
privateinteId;
privateStringeName;
privateStringeDesignation;
privateStringeCompany;
public intgetEmpId()
{
return eId;
}
public voidsetEmpId(finalinteId)
{
this.eId= eId;
}
public String getEmpName()
{
return eName;
}
public voidsetEmpName(finalString eName)
{
// Validating theemployee'snameand
// throwing an exception if thenameisnullor itslength isless thanor equal
to 0.
if(eName== null|| eName.length()<=0)
{
thrownewIllegalArgumentException();
}
this.eName= eName;
}
public String getEmpDesignation()
{
return eDesignation;
}
public voidsetEmpDesignation(final String eDesignation)
{
this.eDesignation = eDesignation;
}
public String getEmpCompany()
{
return eCompany;
}
publicvoid setEmpCompany(final String eCompany)
{
this.eCompany= eCompany;
}
@Override
public String toString()
{
String str = "Employee: [id = " + getEmpId()+ ", name= " + getEmpName()+
", designation= " + getEmpDesignation() + ", company= " + getEmpCompany() +
"]";
return str;
}
}
// Mainclass.
publicclassGetterSetterExample1
{
// main method
public static voidmain(Stringargvs[])
{
// Creating an objectof theEmployeeclass
finalEmployeeemp= newEmployee();
// theemployeedetailsaregetting setusingthesetter methods.
emp.setEmpId(3971);
emp.setEmpName("Arafat");
emp.setEmpDesignation("SoftwareTester");
emp.setEmpCompany("XYZCorporation");
System.out.println(emp.toString());
}
}
Output
Constructor:
In Java, a constructor is a block of codes similar to the method. It is called when an instance
of the class is created. At the time of calling the constructor, memory for the object is
allocated in the memory. It is a special type of method which is used to initialize the object.
Code:
/Let us see example of default constructor
class Student3{
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student3 s1=new Student3();
Student3 s2=new Student3();
s1.display();
s2.display();
}
}
Output
Constructor Overloading:
The constructor overloading can be defined as the concept of having more
than one constructor with different parameters so that every constructor can
performa different task. Consider the following Java program, in which we
have used different constructors in the class.
Constructors can be overloaded in a similar way as function overloading.
Overloaded constructors havethe samename (name of the class) butthe
different number or arguments. Depending upon the number and type of
arguments passed, the corresponding constructor is called.
Example:
//Java programto overload constructors class
class StudentData
{
privateint stuID;
privateString stuName;
privateint stuAge;
StudentData()
{
//Default constructor
stuID =3971;
stuName= "Arafat";
stuAge= 21;
}
StudentData(intnum1, String str, int num2)
{
//Parameterized constructor
stuID =num1;
stuName= str;
stuAge= num2;
}
//Getter and setter methods
public int getStuID() {
return stuID;
}
public void setStuID(intstuID) {
this.stuID =stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName= stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge= stuAge;
}
public static void main(String args[])
{
StudentData myobj = new StudentData();
System.out.println("StudentNameis:
"+myobj.getStuName());
System.out.println("StudentAgeis:
"+myobj.getStuAge());
System.out.println("StudentID is:
"+myobj.getStuID());
StudentData myobj2 = new
StudentData(3975, "Wahid", 20);
System.out.println("StudentNameis:
"+myobj2.getStuName());
System.out.println("StudentAgeis:
"+myobj2.getStuAge());
System.out.println("StudentID is:
"+myobj2.getStuID());
}
}
Output
Inheritance:
Inheritance:
Inheritance is one of the key featuresof OOP that allows us to create a new
class from an existing class. The new class that is created is known as
subclass (child or derived class) and the existing class from where the child
class is derived is known assuperclass(parentor base class).
Code:
class Teacher {
private String designation = "Teacher";
private String collegeName = "DffodilInternatinalUnivercity";
public String getDesignation() {
return designation;
}
protected void setDesignation(String designation) {
this.designation = designation;
}
protected String getCollegeName() {
return collegeName;
}
protected void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
void does(){
System.out.println("Teaching");
}
}
public class JavaExampleextendsTeacher{
String mainSubject= "OOP";
public static void main(String args[]){
JavaExampleobj = new JavaExample();
System.out.println(obj.getCollegeName());
System.out.println(obj.getDesignation());
System.out.println(obj.mainSubject);
obj.does();
}
}
Output :
Single Inheritance: When a class is extended by only one class, it is
called single-level inheritance in java or simply single inheritance.
In other words, creating a subclass from a single superclass is called single
inheritance. In single-level inheritance, there is only one base class and can be
one derived class.
The derived class inherits all the properties and behaviors only from a single
class. Itis represented as shown in the below figure:
Code:
package singleLevelInheritance;
// Create a base class or superclass.
public class A
{
// Declare an instance method.
public void methodA()
{
System.out.println("Base class method");
}
}
// Declare a derived class or subclass and extends class A.
public class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
}
public class Myclass
{
public static void main(String[] args)
{
// Create an object of class B.
B obj = new B();
obj.methodA(); // methodA() of B will be called because by default, it is available in
B.
obj.methodB(); // methodB() of B will be called.
}
}
Output :
Output:
Base class m
ethod
Hierarchical Inheritance: A class that is inherited by many subclasses is
known as hierarchical inheritance in java. In other words, when oneclass is
extended by many subclasses, itis known as hierarchical inheritance..
Code:
package hierarchicalInheritanceEx;
public class A
{
public void msgA()
{
System.out.println("Method of class A");
}
}
public class B extends A
{
// Empty class B, inherits msgA of parent class A.
}
public class C extends A
{
// Empty class C, inherits msgA of parent class A.
}
public class D extends A
{
// Empty class D, inherits msgA of parent class A.
}
public class MyClass
{
public static void main(String[] args)
{
// Create the object of class B, class C, and class D.
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
// Calling inherited function from the base class.
obj1.msgA();
obj2.msgA();
obj3.msgA();
}
}
Output:
Multilevel Inheritance: A class that is extended by a class and that class is
extended by another class forming chain inheritance is called multilevel
inheritance in java.
In multilevel inheritance, there is one baseclass and one derived class at one
level. At the next level, the derived class becomes the baseclass for the next
derived class and so on. This is as shown below in the diagram.
Code:
packagemultilevelInheritance;
public class Bus{
public Bus ()
{
System.out.println("ClassBus");
}
public void vehicleType()
{
System.out.println("VehicleType: Bus");
}
}
class TATA extends Bus{ public TATA()
{
System.out.println("ClassTATA");
}
public void brand()
{
System.out.println("Brand: TATA");
}
public void speed()
{
System.out.println("Max: 70Kmph");
}
}
public class TATA80 extends TATA{
public TATA80()
{
System.out.println("TATAModel: 80");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
TATA80 obj=new TATA80();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Output:

More Related Content

What's hot

What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaEdureka!
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptxRAGAVIC2
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Presentation on Flip Flop
Presentation  on Flip FlopPresentation  on Flip Flop
Presentation on Flip FlopNahian Ahmed
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in cniyamathShariff
 
Type conversions
Type conversionsType conversions
Type conversionssanya6900
 
Explain Half Adder and Full Adder with Truth Table
Explain Half Adder and Full Adder with Truth TableExplain Half Adder and Full Adder with Truth Table
Explain Half Adder and Full Adder with Truth Tableelprocus
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Sameer Rathoud
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
String in c programming
String in c programmingString in c programming
String in c programmingDevan Thakur
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 

What's hot (20)

What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Function in c
Function in cFunction in c
Function in c
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
inheritance
inheritanceinheritance
inheritance
 
Presentation on Flip Flop
Presentation  on Flip FlopPresentation  on Flip Flop
Presentation on Flip Flop
 
Python : basic operators
Python : basic operatorsPython : basic operators
Python : basic operators
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Type conversions
Type conversionsType conversions
Type conversions
 
Explain Half Adder and Full Adder with Truth Table
Explain Half Adder and Full Adder with Truth TableExplain Half Adder and Full Adder with Truth Table
Explain Half Adder and Full Adder with Truth Table
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
The string class
The string classThe string class
The string class
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 

Similar to OOP Lab Report.docx

Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfirshadkumar3
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining ClassesIntro C# Book
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo....NET Conf UY
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Constructor&method
Constructor&methodConstructor&method
Constructor&methodJani Harsh
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Assignment 7
Assignment 7Assignment 7
Assignment 7IIUM
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 

Similar to OOP Lab Report.docx (20)

Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Java2
Java2Java2
Java2
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
Unit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docxUnit-3 Practice Programs-5.docx
Unit-3 Practice Programs-5.docx
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Assignment 7
Assignment 7Assignment 7
Assignment 7
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Oop
OopOop
Oop
 
Java Concepts
Java ConceptsJava Concepts
Java Concepts
 

Recently uploaded

Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 

Recently uploaded (20)

(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 

OOP Lab Report.docx

  • 1. Lab Report: Object-oriented programming Lab Arafat Sahin Afridi PC-A ID: 211-15-3971 Course Code: CSE 222 Course Title: Object-oriented programming Lab Department of Computer Science and Engineering Daffodil International University
  • 2. Class and Object: OBJECT: An object is an identifiable entity with some characteristics, stateand behaviour. Understanding the concept of objects is much easier when we consider real-life examples around us because an objectis simply a realworld entity. Example Object: House State: Address, Color, Area Behavior: Open door, close door Class: A class is a group of objects that share common properties and behavior. For example, we can consider a car as a class that has characteristics like steering wheels, seats, brakes, etc. And its behavior is mobility. But we can say Honda City having a registration number: 4654 is an ‘object’ that belongs to the class ‘car’. It was a brief description of objects and classes. Now we will understand the Java class in detail. Syntax: Createa class named "Main" with a variablex: public class Studen{ //fields(or instancevariable) String StudName; int StudAge; // constructor Studen(String name, int age){ this.StudName=name;
  • 3. this.StudAge=age; } public static void main(String args[]){ //Creating objects Studenobj1= new Studen("Arafat", 20); Studenobj2 = new Studen("Abir", 18); System.out.println(obj1.StudName+" "+obj1.StudAge); System.out.println(obj2.StudName+" "+obj2.StudAge); } } Getter –Setter Methods Here “Main” is a Java class.Setter Method & Getter Method: Encapsulation provide public get and set methods to access and update the value of a Private variable We learned that, private variables can only be accessed within the same class and outside class has no access to it. However, it is possible to access them if we provide public get and set methods. The get method returns the variable value, and the set method sets the value. Syntax for both is that they start with either get or set, followed by the name of the variable, with the first letter in upper case:
  • 4. Example: classEmployee { // classmember variable privateinteId; privateStringeName; privateStringeDesignation; privateStringeCompany; public intgetEmpId() { return eId; } public voidsetEmpId(finalinteId) { this.eId= eId; } public String getEmpName() { return eName;
  • 5. } public voidsetEmpName(finalString eName) { // Validating theemployee'snameand // throwing an exception if thenameisnullor itslength isless thanor equal to 0. if(eName== null|| eName.length()<=0) { thrownewIllegalArgumentException(); } this.eName= eName; } public String getEmpDesignation() { return eDesignation; } public voidsetEmpDesignation(final String eDesignation) { this.eDesignation = eDesignation; }
  • 6. public String getEmpCompany() { return eCompany; } publicvoid setEmpCompany(final String eCompany) { this.eCompany= eCompany; } @Override public String toString() { String str = "Employee: [id = " + getEmpId()+ ", name= " + getEmpName()+ ", designation= " + getEmpDesignation() + ", company= " + getEmpCompany() + "]"; return str; } } // Mainclass. publicclassGetterSetterExample1 { // main method
  • 7. public static voidmain(Stringargvs[]) { // Creating an objectof theEmployeeclass finalEmployeeemp= newEmployee(); // theemployeedetailsaregetting setusingthesetter methods. emp.setEmpId(3971); emp.setEmpName("Arafat"); emp.setEmpDesignation("SoftwareTester"); emp.setEmpCompany("XYZCorporation"); System.out.println(emp.toString()); } } Output
  • 8. Constructor: In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Code: /Let us see example of default constructor class Student3{ int id; String name; void display(){System.out.println(id+" "+name);} public static void main(String args[]){ Student3 s1=new Student3(); Student3 s2=new Student3(); s1.display(); s2.display(); } } Output
  • 9. Constructor Overloading: The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can performa different task. Consider the following Java program, in which we have used different constructors in the class. Constructors can be overloaded in a similar way as function overloading. Overloaded constructors havethe samename (name of the class) butthe different number or arguments. Depending upon the number and type of arguments passed, the corresponding constructor is called. Example: //Java programto overload constructors class class StudentData { privateint stuID; privateString stuName; privateint stuAge;
  • 10. StudentData() { //Default constructor stuID =3971; stuName= "Arafat"; stuAge= 21; } StudentData(intnum1, String str, int num2) { //Parameterized constructor stuID =num1; stuName= str; stuAge= num2; } //Getter and setter methods public int getStuID() { return stuID; }
  • 11. public void setStuID(intstuID) { this.stuID =stuID; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName= stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge= stuAge; } public static void main(String args[]) {
  • 12. StudentData myobj = new StudentData(); System.out.println("StudentNameis: "+myobj.getStuName()); System.out.println("StudentAgeis: "+myobj.getStuAge()); System.out.println("StudentID is: "+myobj.getStuID()); StudentData myobj2 = new StudentData(3975, "Wahid", 20); System.out.println("StudentNameis: "+myobj2.getStuName()); System.out.println("StudentAgeis: "+myobj2.getStuAge()); System.out.println("StudentID is: "+myobj2.getStuID()); } } Output
  • 13. Inheritance: Inheritance: Inheritance is one of the key featuresof OOP that allows us to create a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from where the child class is derived is known assuperclass(parentor base class). Code: class Teacher { private String designation = "Teacher"; private String collegeName = "DffodilInternatinalUnivercity"; public String getDesignation() { return designation; } protected void setDesignation(String designation) { this.designation = designation; } protected String getCollegeName() { return collegeName; } protected void setCollegeName(String collegeName) { this.collegeName = collegeName; } void does(){ System.out.println("Teaching");
  • 14. } } public class JavaExampleextendsTeacher{ String mainSubject= "OOP"; public static void main(String args[]){ JavaExampleobj = new JavaExample(); System.out.println(obj.getCollegeName()); System.out.println(obj.getDesignation()); System.out.println(obj.mainSubject); obj.does(); } } Output : Single Inheritance: When a class is extended by only one class, it is called single-level inheritance in java or simply single inheritance. In other words, creating a subclass from a single superclass is called single inheritance. In single-level inheritance, there is only one base class and can be one derived class. The derived class inherits all the properties and behaviors only from a single class. Itis represented as shown in the below figure: Code: package singleLevelInheritance;
  • 15. // Create a base class or superclass. public class A { // Declare an instance method. public void methodA() { System.out.println("Base class method"); } } // Declare a derived class or subclass and extends class A. public class B extends A { public void methodB() { System.out.println("Child class method"); } } public class Myclass { public static void main(String[] args) { // Create an object of class B. B obj = new B(); obj.methodA(); // methodA() of B will be called because by default, it is available in B. obj.methodB(); // methodB() of B will be called. } } Output : Output: Base class m ethod
  • 16. Hierarchical Inheritance: A class that is inherited by many subclasses is known as hierarchical inheritance in java. In other words, when oneclass is extended by many subclasses, itis known as hierarchical inheritance.. Code: package hierarchicalInheritanceEx; public class A { public void msgA() { System.out.println("Method of class A"); } } public class B extends A { // Empty class B, inherits msgA of parent class A. } public class C extends A { // Empty class C, inherits msgA of parent class A. } public class D extends A { // Empty class D, inherits msgA of parent class A. } public class MyClass { public static void main(String[] args) { // Create the object of class B, class C, and class D. B obj1 = new B(); C obj2 = new C(); D obj3 = new D(); // Calling inherited function from the base class. obj1.msgA();
  • 17. obj2.msgA(); obj3.msgA(); } } Output: Multilevel Inheritance: A class that is extended by a class and that class is extended by another class forming chain inheritance is called multilevel inheritance in java. In multilevel inheritance, there is one baseclass and one derived class at one level. At the next level, the derived class becomes the baseclass for the next derived class and so on. This is as shown below in the diagram. Code: packagemultilevelInheritance; public class Bus{ public Bus () { System.out.println("ClassBus"); } public void vehicleType() { System.out.println("VehicleType: Bus"); } } class TATA extends Bus{ public TATA() { System.out.println("ClassTATA");
  • 18. } public void brand() { System.out.println("Brand: TATA"); } public void speed() { System.out.println("Max: 70Kmph"); } } public class TATA80 extends TATA{ public TATA80() { System.out.println("TATAModel: 80"); } public void speed() { System.out.println("Max: 80Kmph"); } public static void main(String args[]) { TATA80 obj=new TATA80(); obj.vehicleType(); obj.brand(); obj.speed(); } } Output: