Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts
Inheritance in Java
6:56 AM
Posted by Sai Subramanyam
In the previous posts you have learned about the Object Oriented Programming(OOP) Principles( you can refer them here ).So,one of the brightest theories of OOP is the inheritance,we can use the information by acquiring from one class to another and in this way we can manage our data in a hierarchial order.
Now based on above we can come to these conclusions:
Here we can see that Kitten class is creating objects for Animal , Cat , Kitten classes.We are testing whether Kitten object is an instance of Animal and Cat by using an instanceof operator.We will get the output as:
So,here we covered the inheritance with extends keyword,we will see implements concept in detail in Interface concept.
We can also see examples like:
So,in this way we can explain the inheritance concepts.The main advantages if Inheritance are Code Reusability and ability to support polymorphism.
In the next posts more and more concepts will be covere,Stay Tuned!!
Au Revoir!!
Mainly we can use inheritance in Java by using extends and implements keywords extends use mainly normal single inheritance where as implements is used for multiple inheritance which cannot be implemented directly in Java using extends keyword.
So,in Java we have two relationships in Inheritance.They are :
- IS-A relationship.
- HAS-A relationship.
- IS-A relationship : We can explain the inheritance actually like,eg: Dog IS-A Animal , BMW IS-A Car.Here we have a simple example.
class Animal{
}
class Dog extends Animal{
}
class Cat extends Animal{
}
public class Kitten extends Cat{
}
Now based on above we can come to these conclusions:
- Animal is the superclass of Dog and Cat.
- Dog and Cat are subclasses of Animal.
- Kitten is the subclass of Cat and Animal.
These conclusions can be mentioned in IS-A form as:
- Dog IS-A Animal.
- Cat IS-A Animal.
- Kitten IS-A Cat.
- So,we can say that Kitten IS-A Animal.
public class Kitten extends Cat{
public static void main(String args[]){
Animal a = new Animal();
Cat c = new Cat();
Kitten k = new Kitten();
System.out.println(c instanceof Animal);
System.out.println(k instanceof Cat);
System.out.println(k instanceof Animal);
}
}Here we can see that Kitten class is creating objects for Animal , Cat , Kitten classes.We are testing whether Kitten object is an instance of Animal and Cat by using an instanceof operator.We will get the output as:
true true true
So,here we covered the inheritance with extends keyword,we will see implements concept in detail in Interface concept.
- HAS-A relationship : The HAS-A relationship mainly deals with the usage of a certain code used in one class which is extended from an another class.In other words,if a class Vehicle has a subclass class Car and class B has a variable called myBrake which belongs to Brake class,then we can say Car HAS-A Brake.
public class Vehicle { }
public class Car extends Vehicle {
private Brake myBrake;
}
We can also see examples like:
- class Room { .... }
- class Bathroom extends Room{ private Tub myTub; }
So,in this way we can explain the inheritance concepts.The main advantages if Inheritance are Code Reusability and ability to support polymorphism.
In the next posts more and more concepts will be covere,Stay Tuned!!
Au Revoir!!
Object Oriented Programming Basics
2:48 AM
Posted by Sai Subramanyam
In the previous posts we have learned about the Characeteristics of Java (Refer here).One of the characteristics are Java is Object Oriented Programming(OOP's) Language.
Now before we take a dive into Java language we have to know the basic OOP's.
- Object Oriented Programming is the technique to create programs based on the real time entities like objects and data.We can summarize it as the way of real world modeling.
- Unlike procedural programming here the programs are constructed around objects and data.
- Objects are the basic real world runtime entites which have certain behaviour,properties,type,and identity.
Fundamentals of OOP
Before understanding Java we must understand OOP and its basic features.They are:
- Class is the central point of OOP and it is collection of objects and data and methods exhibiting certain kind of behaviour.In the classname every word must start with a "capital letter" Ex: HelloWorldJava.
Example: public class Box{
// your code here
/* This is a basic class with data and methods.
private int a;
private int b;
public void setValue(int width){
a = width;
}
public void setValue(int length){
b = length;
}
public void displayValues(){
System.out.println(" width is " + a " length is " + b);
}
}
- Objects are the basic unit of OOP and basic runtime entities expressed as variables and methods.
- Methods in Java determine the behaviour of the object.The methodName must start with a ''lowercase letter'' and the preceeding word in the method name must be a "capital letter".
Example : public void displayValues(){
//your code here.
}
- Abstraction is the process of hiding the unnecessary or secret details and exposing the essential features of an object.This concept is mainly used by classes.The above image shows the abstraction of a Car class which hides the unnecesarry details and exposes the details like Change gears,Brake,Change cadence.
- Encapsulation is the ability of an object to bind the data and methods together.The Accessor and Mutator methods exhibit encapsulation.The above image also demonstrates the Encapsulation process.
- Data Hiding is the ability of the objects to shield the variables from external access.
- Polymorphism.Let us make this straight.Polymorphism is the ability of an object to take one or more forms.
Polymorphism can be achieved by Overloading(method and operator) and Overriding( method).
Using the above diagram we can explain the concept easily.Here we have an Animal object which is classified into Dog,Duck,Cat which have different behaviours like doWoof for Dog,doQuack for Duck,doMeow for Cat.This will be explained in later posts in detai.
- Inheritance is the capability of a class to use properties of other classes by extending its features.The main idea behind the Inheritance is the code reusability.i.e. If I declare an Animal object it has methods like animalName(),animalSound().
If I want to write code for a Dog object then instead of writing all methods I can simply use the above code and methods by simply extending the Animal Class.This is known as Inheritance.
These are the basic fundamentals of the OOPs.
I found a nice video on YouTube on OOPs and here it is :
In the next posts we will dive into the Java Language Basics.Stay Tuned and Au Revoir!!
Subscribe to:
Posts (Atom)


