A Brief Introduction of Java
Last Updated on: 2nd Jan 2025 16:31:55 PM
This core java toturial is designed for student and profestional also.we are covering complete core java toturial from begining to advance. java is such a powerful programming language .
What is java ?
Java is a high level object-oriented programming language developed by james gosling in the year 1995 at Sun Microsystems . but now it is leading by Oracle Corporation.
Java is the most used in the world by developers.it is used for web application, system application, mobile application, games, embaded system and enterprise application.
class and object
#include<iostream.h>
using namespace std;
const int size=5;
class student {
int roll_no;
int total=0;
int marks[size];
public:
void getdata ();
void tot_marks ();
} ;
void student :: getdata () {
cout<<"\nEnter roll no: ";
cin>>roll_no;
for(int i=0; i<size; i++) {
cout<<"Enter marks in subject"<<(i+1)<<": ";
cin>>marks[i] ;
}
void student :: tot_marks() //calculating total marks {
for(int i=0; i<size; i++)
total + = marks[i];
cout<<"\n\nTotal marks "<<total;
}
int main() {
student stu;
stu.getdata() ;
stu.tot_marks() ;
return 0;
}
single inharitance in cpp
#include<iostream>
using namespace std;
class student
{
// single inheritance
int roll;
char name[10];
double marks;
public:
void read();
void display();
};
void student::read()
{
cout<<"enter your roll = ";
cin>>roll;
cout<<"enter your name = ";
cin>>name;
cout<<"enter your marks = " ;
cin>>marks;
}
void student::display()
{
cout<<"
your name is "<<name<<"
your roll is = " <<roll<<"
your marks is "<<marks;
}
class stud: public student
{
double mob;
char address[20];
public:
void read1()
{
//read();
cout<<"enter mobile no";
cin>>mob;
cout<<"Enter your Address";
cin>>address;
}
void display1()
{
//display();
cout<<"
your mob is " <<mob;
cout<<"
and address is "<<address;
}
} ;
int main ()
{
stud s;
s.read();
s.read1();
system("cls");
cout<<"____________________";
s.display();
s.display1();
return 0;
}
Multilevel
#include<iostream>
using namespace std;
class University{
string universityName;
public:
University(string uName):universityName(uName){
}
void displayUniversityDetails() {
cout << "University: " << universityName << endl;
}
};
class Collage: public University
{ public:
string collage_name;
Collage(string uName, string cName) : University(uName), collage_name(cName) {}
void displayCollegeDetails() {
displayUniversityDetails();
cout << "College: " << collage_name << endl;
}
};
class Department : public Collage {
public:
string departmentName;
Department(string uName, string cName, string dName)
: Collage(uName, cName), departmentName(dName) {}
void displayDepartmentDetails() {
displayCollegeDetails();
cout << "Department: " << departmentName << endl;
}
};
int main()
{
Department d("TNB","SSB","BCA");
d.displayDepartmentDetails();
return 0;
}
Multiple Inharitance in cpp
#include <iostream>
using namespace std;
class Animal // multiple inharitance
{
public:
void eat()
{
cout<<"animal is eating\n";
}
};
class Bird{
public:
void fly()
{
cout<<"Bird is flying\n";
}
};
class Parrot : public Animal, public Bird
{
public:
void speak()
{
cout<<"Parrot is speaking..\n";
}
};
int main()
{
Parrot p ;
p.eat();
p.fly();
p.speak();
return 0;
}
hiracr
#include <iostream>
using namespace std;
class person
{
char name[100],gender[10];
int age; //hierarchical inharitance
public:
void getdata()
{
cout<<"Name: ";
fflush(stdin); /*clears input stream*/
gets(name);
cout<<"Age: ";
cin>>age;
cout<<"Gender: ";
cin>>gender;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
}
};
class student: public person
{
char institute[100], level[20];
public:
void getdata()
{
person::getdata();
cout<<"Name of College/School: ";
fflush(stdin);
gets(institute);
cout<<"Level: ";
cin>>level;
}
void display()
{
person::display();
cout<<"Name of College/School: "<<institute<<endl;
cout<<"Level: "<<level<<endl;
}
};
class employee: public person
{
char company[100];
float salary;
public:
void getdata()
{
person::getdata();
cout<<"Name of Company: ";
fflush(stdin);
gets(company);
cout<<"Salary: Rs.";
cin>>salary;
}
void display()
{
person::display();
cout<<"Name of Company: "<<company<<endl;
cout<<"Salary: Rs."<<salary<<endl;
}
};
int main()
{
student s;
employee e;
cout<<"Student"<<endl;
cout<<"Enter data"<<endl;
s.getdata();
cout<<endl<<"Displaying data"<<endl;
s.display();
cout<<endl<<"Employee"<<endl;
cout<<"Enter data"<<endl;
e.getdata();
cout<<endl<<"Displaying data"<<endl;
e.display();
return 0;
}
encapsulation
#include <iostream>
using namespace std;
class Student
{ // encapsulation
private:
string name;
int account_number;
public:
Student(string nm, int ac)
{
name=nm;
account_number=ac;
}
string getName()
{
return name;
}
int getAccount()
{
return account_number;
}
};
int main()
{
Student s("aarti",5675) ;
cout<<"My Name is "<<s.getName()<<"Account Number is "<<s.getAccount();
return 0;
}
Become a first user to comment