SlideShare a Scribd company logo
2
Most read
13
Most read
18
Most read
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

PPTX
Dynamic method dispatch
PPTX
User defined functions
PPT
Class and object in C++
PPTX
Java(Polymorphism)
PPTX
Oop c++class(final).ppt
PDF
Constructor and Destructor
PPT
Java Streams
PPTX
Operators in java
Dynamic method dispatch
User defined functions
Class and object in C++
Java(Polymorphism)
Oop c++class(final).ppt
Constructor and Destructor
Java Streams
Operators in java

What's hot (20)

PPT
Recursion in c
PDF
Function overloading
PPTX
Inner classes in java
PDF
Data Structures Practical File
PPT
Function overloading(c++)
PPTX
classes and objects in C++
PDF
C++ tokens and expressions
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
NFA & DFA
PPTX
Command line arguments
DOC
Final JAVA Practical of BCA SEM-5.
PPTX
Inheritance in java
PPTX
Java string , string buffer and wrapper class
PPT
Control structures i
PPTX
Unit 5 java-awt (1)
PPTX
Single inheritance
PPTX
Template C++ OOP
PPTX
Static keyword ppt
PPTX
Conditional statement c++
PPTX
Java – lexical issues
Recursion in c
Function overloading
Inner classes in java
Data Structures Practical File
Function overloading(c++)
classes and objects in C++
C++ tokens and expressions
Unit ii chapter 2 Decision making and Branching in C
NFA & DFA
Command line arguments
Final JAVA Practical of BCA SEM-5.
Inheritance in java
Java string , string buffer and wrapper class
Control structures i
Unit 5 java-awt (1)
Single inheritance
Template C++ OOP
Static keyword ppt
Conditional statement c++
Java – lexical issues
Ad

Similar to OOP Lab Report.docx (20)

PDF
Hello. Im currently working on the last section to my assignment a.pdf
PPT
14. Defining Classes
PPT
Defining classes-and-objects-1.0
PPTX
PPTX
Inheritance.pptx
PPT
Java tutorial for Beginners and Entry Level
PPTX
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
DOCX
Unit-3 Practice Programs-5.docx
PDF
Object Oriented Solved Practice Programs C++ Exams
PPTX
Constructor&method
PDF
Java Basic day-2
PPT
Inheritance, polymorphisam, abstract classes and composition)
PDF
OOPs & Inheritance Notes
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
DOCX
Assignment 7
PPTX
Detailed_description_on_java_ppt_final.pptx
PPTX
class object.pptx
PPT
Object Oriented Programming with Java
PDF
Object Oriented Programming in PHP
PPT
Java Generics
Hello. Im currently working on the last section to my assignment a.pdf
14. Defining Classes
Defining classes-and-objects-1.0
Inheritance.pptx
Java tutorial for Beginners and Entry Level
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Unit-3 Practice Programs-5.docx
Object Oriented Solved Practice Programs C++ Exams
Constructor&method
Java Basic day-2
Inheritance, polymorphisam, abstract classes and composition)
OOPs & Inheritance Notes
Object Oriented Programming (OOP) using C++ - Lecture 4
Assignment 7
Detailed_description_on_java_ppt_final.pptx
class object.pptx
Object Oriented Programming with Java
Object Oriented Programming in PHP
Java Generics
Ad

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
UNIT 4 Total Quality Management .pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
Geodesy 1.pptx...............................................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPT
Project quality management in manufacturing
PDF
PPT on Performance Review to get promotions
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
Internet of Things (IOT) - A guide to understanding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
UNIT 4 Total Quality Management .pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Current and future trends in Computer Vision.pptx
Sustainable Sites - Green Building Construction
Geodesy 1.pptx...............................................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Categorization of Factors Affecting Classification Algorithms Selection
Project quality management in manufacturing
PPT on Performance Review to get promotions
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Internet of Things (IOT) - A guide to understanding

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: