What is inheritance in object oriented programming?
1. Inheritance is one of the building blocks of Java and it is used extensively in all java based applications. Inheritance is everywhere in Java. I can't think of one java project that I worked on, where inheritance wasn't used. In order to explore this topic we're going to use the instanceof operator. For now, just remember that instanceof returns true if the reference variable being tested is of the type being compared to. This code:
class InstanceTest {
public static void main(String [] args) {
InstanceTest t1 = new InstanceTest ();
InstanceTest t2 = new InstanceTest ();
if (!t1.equals(t2))
System.out.println("they're not equal");
if (t1 instanceof Object)
System.out.println("t1's an Object");
}
}
Produces the output:
they're not equal
t1's an Object
You are now probably thinking, where this equals method came from? Remember my standard statement? "Everything in Java is an Object"? Ah Yes, now you remember. Every class that you create invariably (and automatically) extends from java.lang.Object. And this Object class contains the equals method which automatically becomes available for all classes. Cool right? The creators of Java assumed (correctly of course) that all programmers would require some sort of functionality to check if two objects are equal and hence this method is placed in the Object class so that it becomes available to all java classes automatically.
The equals method checks if the two java objects under consideration are equal. By default this method checks for the object instance and since in this cast t1 and t2 are created out of two different new InstanceTest() invocations, they are not equal.
If you wanna argue you can say that since they are both just a new instance of the InstanceTest class, shouldn't they be equal? Yes, you are absolutely right. That is why java gives us the feature wherein you can override the equals() method inside your class and add the checks you want to place in order to decide if two objects are equal.
Now, the second if condition checks if t1 is an instance of Object (which it automatically is) it succeeds and prints the message on the console.
For the exam you'll need to know that you can create inheritance relationships in Java by extending a class. It's also important to understand that the two most common reasons to use inheritance are
• To promote code reuse (You don't want your colleague sitting right next to you to write the same validation code in his class while he can obviously use the better code you have written :-) )
• To use polymorphism
Let's begin with reuse.
A Class can re-use or in java terms inherit features/code of another class by just extending it.
Ex:
public class Child extends Parent {
…
}
In the above example by virtue of the extends keyword, all the public code inside the parent class is available for use inside the child class.
A common design approach is to create a fairly generic version of a class with the intention of creating more specialized subclasses that inherit from it. For example:
class Car {
public void goForADrive() {
System.out.println("driving a car");
}
// more code
}
class Ferrari extends Car {
public void takeFerrariForADrive() {
System.out.println("driving a Ferrari");
}
// more code
}
public class TestCars {
public static void main (String[] args) {
Ferrari car = new Ferrari();
car.goForADrive();
car.takeFerrariForADrive();
}
}
Outputs:
driving a car
driving a Ferrari
Notice that the Ferrari class inherits the generic goForADrive() method from the less-specialized class Car, and also adds its own method, takeFerrariForADrive(). Code reuse through inheritance means that methods with generic functionality that could apply to a wide range of different kinds of cars -don't have to be reimplemented. That means all specialized subclasses of Car are guaranteed to have the capabilities of the more generic superclass. You don't want to have to rewrite the goForADrive() code in each of your specialized car classes of a racing game.
But you probably already knew that. You've experienced the pain of duplicate code when you make a change in one place and have to track down all the other places where that same piece code exists. So it is logically and obviously easier to manage changes if all common code is available at one single place.
The second use of inheritance is to allow your classes to be accessed polymorphically-a capability provided by interfaces as well, but we'll get to that in a minute. Let's say that you have a CarLauncher class that wants to loop through a list of different kinds of Car objects, and invoke goForADrive() on each of them. At the time you write this class, you don't know every possible kind of Car subclass that anyone else will ever write. And you sure don't want to have to redo your code just because somebody decided to build a new Car six months later.
The beautiful thing about polymorphism is that you can treat any subclass of Car as a Car. In other words, you can write code in your CarLauncher class that says, "I don't care what kind of object you are as long as you inherit from (extend) Car. And as far as I'm concerned, if you extend Car then you've definitely got a goForADrive() method, so I know I can call it."
Imagine we now have two specialized subclasses that extend the more generic Car class, Ferrari and Porsche:
class Car {
public void goForADrive() {
System.out.println("driving a car");
}
// more code
}
class Ferrari extends Car {
public void takeFerrariForADrive() {
System.out.println("driving a Ferrari");
}
// more code
}
class Porsche extends Car {
public void takePorscheForADrive() {
System.out.println("driving a Porsche");
}
// more code
}
Now imagine a test class has a method with a declared argument type of Car, that means it can take any kind of Car. In other words, any subclass of Car can be passed to a method with an argument of type Car. This code
public class TestCars {
public static void main (String[] args) {
Ferrari ferrari = new Ferrari();
Porsche Porsche = new Porsche();
goForADrive(ferrari);
goForADrive(Porsche);
}
public static void doDrive(Car car) {
car.goForADrive();
}
}
Outputs:
driving a car
driving a car
The key point is that the doDrive() method is declared with a Car argument but can be passed any subtype (in this example, a subclass) of Car. The method can then invoke any method of Car, without any concern for the actual runtime class type of the object passed to the method. There are implications, though. The doDrive() method knows only that the objects are a type of Car, since that's how the parameter is declared. And using a reference variable declared as type Car-regardless of whether the variable is a method parameter, local variable, or instance variable-means that only the methods of Car can be invoked on it. The methods you can call on a reference are totally dependent on the declared type of the variable, no matter what the actual object is, that the reference is referring to. That means you can't use a Car variable to call, say, the takePorscheForADrive() method even if the object passed in is of type Porsche.
Write a program in java to get String contents of a string?
import java.io.*;
class StringContents
{
protected static void main()throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the String: ");
String st=in.readLine();
short u=0,l=0,sp=0;
for(short i=0;i<st.length();i++)
{
char ch=st.charAt(i);
if(ch>=65&&ch<91)
u++;
else if(ch>=97&&ch<123)
l++;
else if(ch==' ')
continue;
else
sp++;
}
System.out.println("No. of lower case characters: "+l);
System.out.println("No. of upper case characters: "+u);
System.out.print("No. of special characters: "+sp);
}}
Variable declarations are made in what section of a program?
Variable declarations are made within the scope in which they are used and, preferably, at the precise point they are used, not before.
What is the difference between inheritance and packages?
Inheritance is a means where a class borrows ("inherits") functionality from a base class, usually as a means of code reuse; the subclass will often add additional functionality or modify the behavior of the base class. Packages are simply a way of organizing code into namespaces, such that: Only classes that are actually part of the application logic are included, to prevent conflicts from classes that have the same name, and to logically organize code so that similar functionality can be grouped together.
If you are applying for this study grant, YOU should think what sets YOU apart, and write about it. It just doesn't make sense to gather, and list, a number of reasons that set OTHER people apart.
What happen when a java keyword new is used in an application?
When the new operator is used, a new object is created, based on the specified class.
When the new operator is used, a new object is created, based on the specified class.
When the new operator is used, a new object is created, based on the specified class.
When the new operator is used, a new object is created, based on the specified class.
What is toString and when you use it?
The toString method is used to create a string representation of the given object. This is often used to give diagnostic output to the programmer.
Write a Java program to generate a copy of itself?
public class S{public static void main(String[]a){String o="public class S{public static void main(String[]a){String o=%c%s%c;System.out.printf(o,34,o,34);}}";System.out.printf(o,34,o,34);}}
What is the difference between advanced java J2EE?
J2EE stands for Java 2 Enterprise Edition. It is used to create enterprise class web applications that can be used by large enterprises and corporations. Most websites of major companies are created using j2ee. Some of the technologies used in J2ee are:
a. Struts
b. Hibernate
c. JSP
d. Servlets
e. Spring
f. etc
Whereas Advanced Java refers to the advanced topics of java that can be used in regular java programs. Some topics under Advanced Java are:
a. Exception Handling
b. Threading/Multi-Threading
c. Remote Method Invocation
d. Serialization
e. etc
How do you make an antivirus program using Java?
A running java program only has access to the memory allocated to it by the Java Runtime Environment. Java by design does not allow access to absolute memory locations which is required to scan for memory resident viruses. A Java-based anti-virus program can still read a virus-signature file and use it to scan for those patterns in files to identify possible infected files.
What happens if a class doesn't implement all the methods defined in the interface?
It depends on how the class is declared.
What would be the best java or net?
Java is always the best.
Java is great for Enterprise while desktop is the place for .Net
Most programming languages have some loop construct, that make it possible to repeat commands several times. The available commands vary, depending on the language; as an example, Java has the "for", "while", and "do...while" keywords.
use the jar command like this:
jar tvf mypackage.jar
What are real constants in java?
"real" numbers, in any programming language, are actually approximations of what is called a "real number" in math. Basically, a number that can handle decimals - but unlike the actual real numbers, of limited precision.
In Java, the "real" data types are float, and double. double has greater precision.
A "constant" in Java is similar to a variable, but its value can't be changed after it has been assigned a value.
What parameters decide the size of data type for a processor?
Some bits of machine instruction code. Of course it is platform-dependent.
Read a string and rewrite in alphabetical order?
Well, I don't know how to rewrite a string in alphabetical order with a function, but I think I know a way to get around it. Try it first before you put it in something. Also, if you need to do it often, make it into a function...
//is an example string
var myName = "jake";
//makes myName into an array that is "j", "a", "k", "e"
var myName_array = myName.split("");
//this changes it into "a", "e", "j", "k";
var myName_array_alphabetical = myName_array.sort();
//and this makes it back into a string."aejk"
var myName_alphabetical = myName_array_alphabetical.join("")
How do you make software from a simple java program?
You compile it.
You compile it.
You compile it.
You compile it.