Posts

Showing posts with the label object oriented programming

Example Programs using Real world Classes and Objects

Image
this post contains example programs of real world classes and objects. Comments in these code will help you more to relate programs with real world conditions. It is advised to go through comments. In Last two examples you will notice that two output are almost same but the code of both of them is different. one of them uses constructor to initialize the class and other uses method to do the same work This comparision will help you to understand constructors 1. Example program of initializing Watch objects using constructors. 2. Program of initializing Car objects without using constructor 3. Program of Initializing Car objects using constructor You can also create many such programs just by getting the idea of real-world examples. If you don't know real world examples just go through my post. 10 real-world examples of classes Now create your own programs

Creating Objects

Image
As we have seen earlier a class provides the blueprint for objects. A class creates a logical framework that defines the relationship between its members. An object in Java is essentially a block of memory that contains space to store all the instance variables. It can also be considered as a variable. Obtaining objects of a class is a two-step process   Declaring  variable of the class type Acquiring an actual, physical copy of the object and assign it to that variable. Box mybox = new Box(); The above line creates an object of the Box class. Here it combines two statements into a single statement. It can be rewritten as - Box mybox; //declare reference to object mybox = new Box(); //allocate a box object Here, first line declares mybox as a reference to an object of type Box . After this line executes, mybox contains the value null. Any attempt to use mybox at this point may cause a compile-time error. S...

Introduction to classes with real-world Examples

10. Real world examples of classes, methods and Objects in Comparision with Object Oriented Programming A class is like a blueprint for creating similar objects. Let's take a real-world example A car company has a blueprint of any car model and based on that they create similar kind of cars. Here, every car is an object and the blueprint of car is like a class.  every car object has it's own attributes such as weight, color, seating capacity, etc. and methods such as driving, applying breaks, turning on headlights, etc. A class defines a new data type. Once defined, this new type can be used to create objects of that type. Means a class is a template for an object, and an object is an instance of an class. In other terms an Object is a specific case of a class. The General form of a class When we define a class we declare its exact form and nature. This is done by specifying the data that it contains and the code that operates...

Hello world program of Java -Explained

Hello world //program that every developer loves to execute class Hello { public static void main(String args[]){ System.out.println("Hello world"); } } When you compile and run your program you will get the output as  Hello world //program that every developer loves to execute This is a comment. Like most other programming language java also allows us to write comment which will not be executed while running the program. Java supports three types of comments single line comment , multiline comment s and documentation comment. Above comment is single line comment. class Hello { Here class is a keyword which declares the new class. Hello is the name of class which is also known as identifier. The entire class definition, including all of its members, will be between the curly braces {} . public static void main(String args[]){ This is the line at which the program will begin executing All java applications begin execution by calling ...

Why my java programs are executing finely without using javac command?

I was having a question that why my programs are getting executed fine without using javac command which was told to use in my classroom lectures. I searched for it and got exact reason behind it. So I had a thought to share it with you friends. The main reason behind that is the version of java which I am using. I am having the latest version of java that is java SE 15.0.1  (Here is the step by step guide to install it) In all the versions earlier to java 11 you have to compile your code and then you can run it. here is the commands. javac Example.java java Example.java But if version of java is java 11 or later you can run java by itself to compile and auto-run your code. It will work well if your code is not having any error. java Example.java Now the question comes in mind that if we are able to run it just by using the java command then what is the use of javac ? I got the same, here is the answer The work of javac command is to compil...

Introduction to Java, classes and objects

Image
What is flow chart? ➡     A flowchart is a type of diagram that represents a workflow or process. Also It can be defined as a diagrammatic representation of  any process. Advantages of C language ➡ Speed of compilation is comparatively high ➡ Powerful and efficient language Problems with C programming ➡ It is a structured Programming Language ➡ C is middle level language ➡ Does not offers the concept of Object oriented programming ➡ We have to write too long code for simple programs ➡ It is complex to understand. ➡ It is also complex to code. Introduction to Java It is a programming language created by James Gosling from Sun Microsystems(SUN). It is object oriented programming language. Creator of java :- James Gosling Where Java can be used? ➡ Mobile applications ( mainly android apps) ➡ Desktop applications ➡ Web servers and application servers ➡ Web applications and much more.... Advantages of using Ja...

YouTube