UrbanPro
true

Take BTech Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Object - Oriented Programming

Priydarshani S.
25/01/2017 0 0

As we know already JAVA is object oriented language. Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OO programming, computer programs are designed by making them out of objects that interact with one another.[1][2] There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type..An object-based Java application is a Java application whose design is based on declaring classes, creating objects from them, and designing interactions between those objects.

We already know five fundamentals of JAVA Abstraction, Encapsulation, Inheritance, Polymorphism, Class and Object. 

class is a template or blueprint for manufacturing objects. In technical word Class is a template which describe what are all state or behavior an instance of this class can have. Class is like you are creating a prototype of something and Object is actual implementation of that prototype. You declare a class by specifying the class keyword followed by a non-reserved identifier that names it. A pair of matching open and close brace characters ({ and }) follow and delimit the class's body. Let's take a look at the standard syntax for declaring a Java class:

By convention, the first letter of a class's name is uppercased and subsequent characters are lowercased (for example,Employee). If a name consists of multiple words, the first letter of each word is uppercased (such as SavingsAccount). This naming convention is called camelcasing.

In general, class declarations can include these components, in order:

  1. Modifiers such as publicprivate, and a number of others that you will encounter later.
  2. The class name, with the initial letter capitalized by convention.
  3. The class body, surrounded by braces, {}.

A class in java can contain:

  • data member
  • method
  • constructor
  • block
  • class and interface
  • What is Object

Let us now look deep into what are objects. If we consider the real-world we can find many objects around us, Cars, Dogs, Humans, etc. All these objects have a state and behavior.

If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging, running

If you compare the software object with a real world object, they have very similar characteristics.

Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.

So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

  • We always get confused with Class and Objects. And people always ask question what is difference between Class and Object. Class is like you are creating a prototype of something and Object is actual implementation of that prototype. In technical word Class is a template which describe what are all state or behavior an instance of this class can have. And Object is actual implementation of that class which have some state & behavior in form of variable and methods and it acquire some memory space in heap.

Example of Object and class that maintains the records of students

In this example, we are creating the two objects of Student class and initializing the value to these objects by invoking the insertRecord method on it. Here, we are displaying the state (data) of the objects by invoking the displayInformation method.

  1. classStudent2{  
  2. int rollno;  
  3. String name;  
  4. void insertRecord(int r, String n){  //method  
  5. rollno=r;  
  6. name=n;  
  7. }  
  8. voiddisplayInformation(){System.out.println(rollno+" "+name);}//method  
  9. publicstatic void main(String args[]){  
  10. Student2 s1=newStudent2();  
  11. Student2 s2=newStudent2();  
  12. insertRecord(111,"Karan");  
  13. insertRecord(222,"Aryan");  
  14. displayInformation();  
  15. displayInformation();  
  16. }  
  17. }  

 

Public : It is an Access Modifier, which(AM) defines who can access this method(In Java World). Public means that this method will be accessible to any class(If other class can access this class.).
Static : Keyword which identifies the class related this. It means that this class is not instance related but class related. It can be accessed without creating the instance of Class.
Void : Return Type, It defined what this method can return. Which is void in this case it means that this method will not return any thing.
main: Name of the method. This method name is searched by JVM as starting point for an application.
String args[] :  Parameter to main method.

Constructors:

When discussing about classes, one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class the Java compiler builds a default constructor for that class.

Each time a new object is created, at least one constructor will be invoked. Constructor is a block of code, which runs when you use new keyword in order to instantiate an object. It looks like a method, however it is not a method. Methods have return type but constructors don’t have any return type.

How to call a constructor? The constructor gets called when we create an object of a class (i.e. new keyword followed by class name). For e.g. Demo obj =  new Demo(); (here Demo() is a default constructor of Demo class).

Default constructor: It is also known as no-arg constructor. Constructor with no arguments is known as default constructor.

class Demo

{

     public Demo()

     {

         System.out.println("This is a default constructor");

     }

}

Parameterized constructor: Constructor with argument list is known as parameterized constructor.

class Demo

{

      public Demo(int num, String str)

      {

           System.out.println("This is a parameterized constructor");

      }

}

The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.

Example of a constructor is given below:

class Example2{      private int var;      public Example2()      {             //code for default one             var = 10;      }      public Example2(int num)      {             //code for parameterized one             var = num;      }      public int getValue()      {              return var;      }      public static void main(String args[])      {              Example2 obj2 = new Example2();              System.out.println("var is: "+obj2.getValue());      }}

Output:

var is: 10Example2 obj2 = new Example2(77); System.out.println("var is: "+obj2.getValue());

Output:

var is: 77Example2 obj3 = new Example2(); Example2 obj2 = new Example2(77); System.out.println("var is: "+obj3.getValue());

Output:

var is: 10

Object Oriented Concepts

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. is the inclusion of behavior (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. Inheritance can also be defined as the process whereby one object acquires characteristics from one or more other objects the same way children acquire characteristics from their parents.

The idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritance.Inheritance represents the IS-A relationship, also known as parent-child relationship.e.g Building ß- house house is a building Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses

extends key word. Composition

class Building{

.......

}

class House extends Building{       

.........}

Why use inheritance in java

  • For Method Overriding (so runtime polymorphism can be achieved).

Since in method overriding both the classes(base class and child class) have same method, compile doesn’t figure out which method to call at compile-time. In this case JVM(java virtual machine) decides which method to call at runtime that’s why it is known as runtime or dynamic polymorphism.

  • For Code Reusability.

Syntax of Java Inheritance

  1. classSubclass-name extends Superclass-name  
  2. {  
  3. //methods and fields  
  4. }  
  5. Theextends keyword indicates that you are making a new class that derives from an existing class.
  6. In the terminology of Java, a class that is inherited is called a super class. The new class is called a subclass.

Example :

class A {

 

2

    //Members and methods declarations.

 

3

}

 

4

 

 

5

class B extends A {

 

6

    //Members and methods from A are inherited.

 

7

    //Members and methods declarations of B.

 

8

}

 

Java supports only public inheritance and thus, all fields and methods of the superclass are inherited and can be used by the subclass. The only exception are the private members of the superclass that cannot be accessed directly from the subclass. Also, constructors are not members, thus they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. In order to call the constructor of the superclass Java provides the keyword super, as shown below:

 

class A {

 

02

    public A() {

 

03

        System.out.println("New A");

 

04

    }

 

05

}

 

06

 

 

07

class B extends A {

 

08

    public B() {

 

09

        super();

 

10

        System.out.println("New B");

 

11

    }

 

12

}

 

public class Animal {

 

02

    public Animal() {

 

<
0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Glimpse on Lean Manufacturing
Value Stream Mapping: Value stream mapping (VSM) is one of the lean manufacturing techniques used to identify waste, reduce process cycle times, and implement process improvement. VSM is, also known as...

Sathish Kumar | 14 Feb

2 0
0

Theoretical & Practical Aspect of FEA
Methods to Solve any Engineering Problems: - Analytical Method - Experimental Method - Numerical Method Different Numerical Merhods: - Finite Element Method - Finite Volume Method - Boundary...

Sathish Kumar | 28 Jan

1 1
0

Guidelines to Prepare Engineering Drawings
GUIDELINES: Follow the angle of Projection. Not necessary to provide 3-views, if two views are sufficient to cover all details The orientation of the parts shall be shown inline concerning actual...

Sathish Kumar | 24 Jan

2 0
0

Lets Talk About Software Design-patterns
What are Design Patterns? Design Pattern is a used and tested solution for a known problem. In simple words, you can say a general reusable solution to a commonly occurring problem within a given context...

Abhinaw Tripathi | 31/05/2021

1 0
0

Material Science and Metallurgy
Hello, I would like to show very first how to visualize the subject. For this, we should have total concentration. To avoid obstacle coming in front of us, we will follow the following steps. Close your...

Yogesh Dhage | 26/05/2021

2 0
0

03

Please enter your full name.

Please enter institute name.

Please enter your email address.

Please enter a valid phone number.

Please enter a pincode or area name.

Please enter category.

Please select your gender.

Please enter either mobile no. or email.

Please enter OTP

Please enter Password

By signing up, you agree to our Terms of Use and Privacy Policy.

Already a member?

X

Looking for BTech Tuition Classes?

The best tutors for BTech Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take BTech Tuition with the Best Tutors

The best Tutors for BTech Tuition Classes are on UrbanPro

Book a Free Demo

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more