Can we use Java for OOP programming?

Asked By 0 points N/A Posted on -
qa-featured

As object oriented programming is very popular and easy to use. Is it possible to use Java for object oriented programming.If possible how to do so?

SHARE
Best Answer by aishkarwa
Answered By 0 points N/A #116950

Can we use Java for OOP programming?

qa-featured

hello kenneth yes you can use java for object oriented programming.java is a fully object oriented language.in java you cant not create programs without using classes and objects.other languages like c++ and c# are partially object oriented but java is fully object oriented.you can learn java classes syntax and use them in every program

example

 

classjava

{

public static void main(string [] args)

{

system.out.prinln("this is kenneth's first program");

}

}

 

 

this is as simple as any other programming language.if you know the basic programming then you can easily learn and use java.

all the best 

Best Answer
Best Answer
Answered By 0 points N/A #116951

Can we use Java for OOP programming?

qa-featured

Hi,

Java is a object oriented programming language.Java works on objects only.In Java Programming we can use many OOPS Concepts

like – Inheritance, encapsulation, abstraction, polymorphism etc.

1) Inheritance – Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword.

Eg:- public class Parent{

public String parentName;
public int parentage;
public String familyName;
}

public class Child extends Parent{

public String childName;
public int childAge;

public void printMyName(){
System.out.println(“ My name is “+ chidName+” “ +familyName)
}

}

In above example the child has inherit its family name from the parent class just by inheriting the class.

2) Encapsulation – Encapsulation is also known as information hiding, it is to protect data from the client using the classes

but still allowing the client to access the data, but not modify it. Through a public interface, the private data can be used by

the client class without the worry of the user messing with the private data.

eg:- Declaring an instance variable private, and having an access or method that allow access to the variable.

3) Abstraction – An abstract class is something which is incomplete and you can not create instance of it for using it.if

you want to use it you need to make it complete by extending it.

Eg:-abstract class sum
{

public int a;
public int b;

abstract void add(); \declare only
}

\ A class contain one or more abstract method called abstract

class

class showsum
{
public static void main( argv [ ] )
{
sum s = new sum();
s.add()
{
s.a = argv[0];
s.b = argv[1];
return (s.a+s.b);
}
int addi = s.add();
System.out.println(addi);
}

4) Polymorphism – Polymorphism is the capability of an action or method to do different things based on the object that it is

acting upon. In other words, polymorphism allows you define one interface and have multiple implementation.

The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.

Let's take a look at the following example:

class Animal {
void whoAmI() {  
System.out.println("I am a generic Animal.");
}
}
class Dog extends Animal {
void whoAmI() {  
System.out.println("I am a Dog.");
}
}
class Cow extends Animal {
void whoAmI() {  
System.out.println("I am a Cow.");
}
}
class Snake extends Animal {
void whoAmI() {  
System.out.println("I am a Snake.");
}
}class RuntimePolymorphismDemo {
public static void main(String[] args) {  
Animal ref1 = new Animal();  
Animal ref2 = new Dog();  
Animal ref3 = new Cow();  
Animal ref4 = new Snake();  
ref1.whoAmI();  
ref2.whoAmI();  
ref3.whoAmI();  
ref4.whoAmI();
}
}

The output is

I am a generic Animal.
I am a Dog.
I am a Cow.
I am a Snake.

 

Thanks,

Aishwariya

Login/Register to Answer

Related Questions